Author Topic: Arduino serial  (Read 2119 times)

0 Members and 1 Guest are viewing this topic.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Arduino serial
« 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
  }
}

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Arduino serial
« Reply #1 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

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.

Offline Actives

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
Re: Arduino serial
« Reply #2 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 seem to be more simple

i saw the serial code it use to be working

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Arduino serial
« Reply #3 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

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Arduino serial
« Reply #4 on: May 17, 2010, 02:23:00 PM »
You could use the software servo library instead.
http://www.arduino.cc/playground/ComponentLib/Servo

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Arduino serial
« Reply #5 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.

 


Get Your Ad Here

data_list