Go Down

Topic: Controlling a continuous rotation servo motor (Read 633 times) previous topic - next topic

HollowRaccoon

Oct 14, 2019, 10:15 am Last Edit: Oct 15, 2019, 11:45 am by HollowRaccoon
So I have a problem. I need to write a program to control the continuous rotation servo from serial input. I need to use the Servo library to control the servo. I ran into a problem, that first of all, my if statements don´t work, so like if the first if statement is true, it just doesn't read the second if statement, but I need to have them because I don't want the user input to be more than 1700 and less than 1300. Another problem is, that If I input 1700 as my input, then it rotates for a second in another direction and then stops, I need it to rotate in the direction constantly and change it when I type 1300.

Here´s my code:

Code: [Select]
#include <Servo.h>

Servo myservo;

void setup() {
myservo.attach(9);
Serial.begin(9600);
Serial.println("Please enter a number and press ENTER.");
}
int x;

void loop() {
x=Serial.parseInt();
if(x>1700){
  Serial.println("Too high of a number");
}

else{
  myservo.writeMicroseconds(x);
}

if(x<1300){
  Serial.println("Too low of a number");
}

else{
  myservo.writeMicroseconds(x);
}
}

jackthom41

Use proper code tags to place your code. I have read your code and it doesn't make any sense. In order to move the servo continuously, you need to use for loop.
My collection of Arduino Projects ====> https://www.theengineeringprojects.com/2015/03/arduino-projects.html

slipstick

A continuous mode servo, as the name suggests. rotates continuously when you send it a signal, generally 1500 is stop, 1300 will be one direction, 1700 the other direction.

But the problem is in the code.
Code: [Select]

if(x>1700){
  Serial.println("Too high of a number");
}

else{
  myservo.writeMicroseconds(x);
}

What happens if x comes in as 1000? It's not higher that 1700 so it is written too the motor. Not what I think was intended!

Steve

Go Up