Society of Robots - Robot Forum

Software => Software => Topic started by: offy on May 16, 2010, 09:34:43 PM

Title: Arduino serial
Post 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

Code: [Select]
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
  }
}
Title: Re: Arduino serial
Post by: Razor Concepts on May 16, 2010, 09:44:57 PM
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.
Title: Re: Arduino serial
Post by: Actives on May 17, 2010, 03:27:32 AM
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
Title: Re: Arduino serial
Post by: offy on May 17, 2010, 01:10:12 PM
the thing is, i'm going to be running 6+ servo's, and the servo libaray doesn't support that for the atmega 168
Title: Re: Arduino serial
Post by: Razor Concepts on May 17, 2010, 02:23:00 PM
You could use the software servo library instead.
http://www.arduino.cc/playground/ComponentLib/Servo (http://www.arduino.cc/playground/ComponentLib/Servo)
Title: Re: Arduino serial
Post by: offy on May 17, 2010, 06:52:04 PM
the PWM isn't a problem. I have moving the servo's just fine, i just need serial help.