Reputation: 935
I'm trying to turn a continious rotation servo with an Arduino micro-controller.
I want to turn the servo 1 degree to the right when pushing the right arrow-key with a serial-connection. This is my code:
const int servoPin = 6;
int incomingByte;
Servo servo;
int pos;
void setup() {
Serial.begin(9600);
pos = 0;
servo.attach(servoPin);
servo.write(pos);
}
void loop() {
incomingByte = Serial.read();
if (incommingByte == 67) {
pos++;
servo.write(pos);
}
}
What do I have to do to make him turning? Because now, it doesn't move...
Thanks a lot!!
Upvotes: 0
Views: 1161
Reputation: 173
There are several things wrong with your code. You have several syntax errors going on.
First, you need to do a #include <Servo.h>
and declare incomingByte
as an int. You also have a typo in the if-condition line.
Also, you can't read from keyboard if the keyboard isn't connected to the Arduino board, unless you have something in the middle to relay keyboard data to the board. Here's the code you can use to start with:
#include <Servo.h>
int incomingByte;
Servo servo;
int pos;
int dir;
void setup() {
Serial.begin(9600);
Serial.print("Test\n");
pos = 90;
dir = 1;
servo.attach(9);
servo.write(pos);
}
void loop() {
if (pos >= 180 || pos <= 0) { dir = -dir; }
pos += dir;
Serial.print(pos);
Serial.println();
servo.write(pos);
delay(50);
}
Upvotes: 1