Hi,
So I know there are similar issues in forum like this but I still couldn't resolve my issue.
Objective:
-Enter Servo Angle in Serial monitor (e.g. 90 deg)
-Servo motor should go to that angle incrementally
-Enter another angle after loop ends (e.g. 45 deg)
-Servo motor should go back to 45 incrementally
Circuit setup pic is attached. So do I need to supply even more current for this motor to work properly or is my code causing an issue?
Code is here:
#include <SoftwareSerial.h>
#include <Servo.h>
Servo servo01;
int servo1Pos;
int servo1PPos;
int speedDelay = 20;
void setup() {
Serial.begin(9600);
Serial.println("Hello");
servo01.attach(5);
}
void loop() {
Serial.println("Servo Angle?");
while (Serial.available()==0){
}
servo1Pos=Serial.parseInt(); //e.g.90
Serial.println(servo1Pos);
if (servo1PPos > servo1Pos){
for (int j = servo1PPos; j >= servo1Pos; j--){
servo01.write(j);
delay(100);
}
}
if (servo1PPos < servo1Pos){
for (int j = servo1PPos; j >= servo1Pos; j++){
servo01.write(j);
delay(100);
}
}
servo1PPos = servo1Pos; // 90
}
The following is what I get in sequence when I start Serial monitor: (refer to attached image)
- After I start serial monitor
- After I type in 90
- After servo motor physically reaches 90 deg (Code asks for servo angle)
- I type in 45, but physically the servo motor goes back to zero position and then goes to 45 deg.
So if anyone can help me out here, I am pretty new to arduino so probably doing some basic mistake or sth?
Extra Note:
Added Hello to test if board resets, it doesn't.