Society of Robots - Robot Forum
Software => Software => Topic started by: offy on May 16, 2010, 09:34:43 PM
-
I have arduino code that reads serial. It reads what servo to write to, and what pulse. I can get it to pick the servo by input of a, s, or d. But I can't get it to change pulse. How can I have 2 different types of serial input.
Current Code
int servoPin[] = {4,7};
int pos[] = {1800, 1800};
long lastPulse = 0;
int refreshTime = 20;
char val;
int i = 0;
void setup()
{
pinMode(servoPin[0], OUTPUT);
pinMode(servoPin[1], OUTPUT);
Serial.begin(9600);
}
void loop()
{
val = Serial.read();
if(val == 'a')
{
i = 0;
}
if(val == 's')
{
i = 1;
}
if (millis() - lastPulse >= refreshTime)
{
digitalWrite(servoPin[i], HIGH); // start the pulse
delayMicroseconds(pos[i]); // pulse width
digitalWrite(servoPin[i], LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
}
-
Serial.read() returns a byte that corresponds to a character or numeral of the ACSII table - see here:
http://arduino.cc/en/Tutorial/ASCIITable (http://arduino.cc/en/Tutorial/ASCIITable)
So you must come up with some kind of communication protocol to determine the servo position.
If you are using Hyperterminal, I don't think you can send strings via serial, it only does it one character at a time. I reccomend realterm, so you can actually send long messages.
For example, you can make code that receives the string
"1180" and interprets that is servo 1 goes to 180, "2000" as servo 2 goes to 0, "3090" as servo 3 goes to 90, etc etc.
-
why did you not used servo library ? it available here http://www.arduino.cc/en/Reference/Servo (http://www.arduino.cc/en/Reference/Servo) seem to be more simple
i saw the serial code it use to be working
-
the thing is, i'm going to be running 6+ servo's, and the servo libaray doesn't support that for the atmega 168
-
You could use the software servo library instead.
http://www.arduino.cc/playground/ComponentLib/Servo (http://www.arduino.cc/playground/ComponentLib/Servo)
-
the PWM isn't a problem. I have moving the servo's just fine, i just need serial help.