Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
#include <servo.h> //including the library of servo motor
Servo sg90; //initializing a variable for servo named sg90
int initial_position = 90; //Declaring the initial position at 90
int LDR1 = A0; //Pin at which LDR is connected
int LDR2 = A1; //Pin at which LDR is connected
int error = 5; //initializing variable for error
int servopin=9;
void setup()
{

sg90.attach(servopin); // attaches the servo on pin 9
pinMode(LDR1, INPUT); //Making the LDR pin as input
pinMode(LDR2, INPUT);
sg90.write(initial_position); //Move servo at 90 degree
delay(2000); // giving a delay of 2 seconds
}

void loop()
{
int R1 = analogRead(LDR1); // reading value from LDR 1
int R2 = analogRead(LDR2); // reading value from LDR 2
int diff1= abs(R1 - R2); // Calculating the difference between the LDR's
int diff2= abs(R2 - R1);

if((diff1 <= error) || (diff2 <= error)) {
//if the difference is under the error then do nothing
} else {
if(R1 > R2)
{
initial_position = --initial_position; //Move the servo towards 0 degree
}
if(R1 < R2)
{
initial_position = ++initial_position; //Move the servo towards 180 degree
}
}
sg90.write(initial_position); // write the position to servo
delay(100);
}

What I have tried:

the code is in c++. i need to convert it in C. thanks.
Posted
Updated 22-Mar-20 1:45am
Comments
Richard MacCutchan 22-Mar-20 4:16am    
The only part that is C++ is the Servo class, so you will need to contact the person who wrote it. Alternatively if you have the source code you can convert it yourself.
Rick York 22-Mar-20 12:51pm    
OK. So what is your question?

That is C: C++ is (as the name suggests) a superset of C, and C code will run under C++.

Conversely, unless it uses specifically C++ language features (i.e. classes, insertion operators, and such like) C++ code will run pretty much unchanged as C code.
That code uses no C++ language features: it is C code, unless the Servo instance you are declaring is a class (and we have no idea what that is: we don't have access to the rest of your code).
 
Share this answer
 
When you convert the code into C you can use structs to use the class design by defining a struct with all class member and redesign the called functions with an additional first parameter of the pointer to the struct. This avoids copying the data.

Read this struct tutorial and remember to work with pointes.
 
Share this answer
 
Arduino code is mostly written in C++ for the Arduino environment. The code is tightly interwoven and to refactor in C you would need to go down many levels and modify the code at each level, depending on complexity of code determines how hard to convert.

The code is available in various sub-directories where the Arduino IDE is installed.

For instance the servo library code is located;
<where ide="" installed="">\arduino-1.8.12\libraries\Servo\src\avr\Servo.cpp

It would probably be as easy to write the code from scratch as to refactor the Arduino code.

I have refactored some code for use in Atmel Studio but most the time I end up just writing the code from scratch using the Arduino code for reference.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900