Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I am facing a problem to control my 360 degree servo motor using arduino by "if statement" and the input will be potentiometer.

For and example, "if the analogRead<200" i want the servo motor to rotate clockwise. "If the analogRead>201" i want the servo motor to rotate anti clockwise.

P/S: i have tried a codings such as "Map". But i want to control my servo motor using "if statement".

Advance thanks for who ever will guide me.

Thank you.

share|improve this question

migrated from electronics.stackexchange.com Jul 2 at 16:58

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

    
Do you already have code that can make the servo motor rotate clockwise and counterclockwise? – Justin Jul 2 at 13:00
    
@justin yes i have, but one of the rotation is not working. – Jai Jai Jul 2 at 17:22
1  
then post the code! – CharlieHanson Jul 3 at 0:39

1 Answer 1

Are you developing on arduino? Seems like you are! So, I think I can assume that you can develop your firmware in C programming language.

"if the analogRead<200" i want the servo motor to rotate clockwise. "If the analogRead>201" i want the servo motor to rotate anti clockwise.

"What happens when value of analogRead is equal to 200 or 201?" Take care of the boundary conditions.

To achieve that syntactically,

//some code
if ((analogRead > 201) && (analogRead < MAX_ANALOG_READ)) {
    //Rotate anticlowise or donate cupcakes or whatever task A
}
else if (analogRead <= 200) {
    //Rotate clockwise or steal cupcakes or whatever task B
}
else{
    //Error handling in case analogRead value exceeds the maximum? -- Optional but recommended, keep cupcakes with yourself or whatever task Z
}
share|improve this answer
    
That's not quite right though. You definitely need a dead area (e.g. 195 to 205) or a hysteresis around 200. Otherwise if you get close to the 200 setting the servo will keep jerking. – Alex Jul 2 at 13:09
    
I will send the code that i typed, maybe will be easier for you to correct it. – Jai Jai Jul 2 at 13:27
    
@WedaPashi @Alex @Justin #include <Servo.h> const int analogPin = A0; Servo myservo; int pos1 = 0; int pos2 = 0; void setup() { myservo.attach(9); } void loop() { int analogValue = analogRead(analogPin); if(analogValue<400) { for (pos1 = 0; pos1 <= 180; pos1-=1) {myservo.write(pos1); } if(analogValue>400) { for (pos1 = 0; pos1>= 180; pos1+=1) {myservo.write(pos1); } } } } – Jai Jai Jul 2 at 13:58
    
@Alex: While you made the right point about hysteresis, I would like to say that my was answer was more towards syntax and not towards what goes in. Had this been the case that user wanted to either donate cupcakes or steal cupcakes depending upon the variable in if statement, my answer would still remain the same. Editing that way now! – WedaPashi Jul 2 at 14:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.