0

Can Anyone Please Convert the following Arduino Code to Embedded c code? I am very thankful to the one who converts this to an embedded c code. (this code is for Arduino lcd interfacing with Ultrasonic sensor)

    #include <LiquidCrystal.h>
      int inches = 0;
      int cm = 0;

      // initialize the library with the numbers of the interface pins
      LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

      void setup() {
        // set up the LCD's number of columns and rows:
        lcd.begin(16, 2);
        // Print a message to the LCD.
        pinMode(7, INPUT);
      }

      void loop() {
        lcd.clear();
        cm = 0.01723 * readUltrasonicDistance(7);
        inches = (cm / 2.54);
        if (cm<40){
        lcd.setCursor(0, 0);
        // print the number of seconds since reset:
        lcd.print("Caution: ");
        lcd.setCursor(0,1);
        lcd.print("Objects Nearby");
        delay(1000);
        }
      }

      long readUltrasonicDistance(int pin)
      {
        pinMode(pin, OUTPUT);  // Clear the trigger
        digitalWrite(pin, LOW);
        delayMicroseconds(2);
        // Sets the pin on HIGH state for 10 micro seconds
        digitalWrite(pin, HIGH);
        delayMicroseconds(10);
        digitalWrite(pin, LOW);
        pinMode(pin, INPUT);
        // Reads the pin, and returns the sound wave travel time in microseconds
        return pulseIn(pin, HIGH);
      }
5
  • 1
    Hello and welcome to Stackoverflow! Please be aware that this site does not offer any code translation service. Have a look at the FAQ on how to ask good questions so that someone would be able to help you. Commented Apr 14, 2020 at 10:53
  • 1
    Arduino is already embedded C code - just with some handy libraries... Are you trying to find the source code for these Arduino standard functions? You already have them - \arduino\hardware\arduino\cores\arduino look in the directory. Commented Apr 14, 2020 at 11:12
  • I Just Want to convert this code to atmega328p code Commented Apr 14, 2020 at 12:11
  • 1
    It already is code for the atmega328P. You don't need to change anything. Commented Apr 14, 2020 at 17:06
  • 1
    If you want the machine code for atmega328p code, just compile it using Arduino IDE for instance. Commented Apr 15, 2020 at 8:13

2 Answers 2

1

I'm sorry but you can't because the dependencies of this code are LiquidCrystal.h (written in C++ and it contains dependencies like Arduino.h or Wire.h, libraries exclusive of Arduino software) and because methods like pinMode(int,int),digitalWrite(int,int),delayMicroseconds(int) comes from Arduino.h.
You can make your LiquidCrystal library rewriting it from the original.
Here some resources: C header and source, compile and upload, setup, standard avr libs.
I hope this can help. Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1

You can do that but is bit painful. Convert the class based function into c type functions. Remove dependant functions and replace the function by your own. and use int main(void) instead of void loop().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.