Go Down

Topic: serial communication for two servos (Read 458 times) previous topic - next topic

wrightone

May 07, 2012, 01:10 am Last Edit: May 07, 2012, 01:54 am by wrightone Reason: 1
Does anyone have a code that would allow me to manually input the position of two servos using the serial monitor function? The code i an currently using is giving me:
servos2.cpp: In function 'void loop()':
servos2:27: error: expected ',' or ';' before 'while'
servos2:68: error: expected `}' at end of input

Can someone fix my code or provide me with one that works. Thanks. Here is what I have so far:

Code: [Select]

/* two servo controller
serial input format
  protocol;motor_number=angle
  ex: 1=180@ or 2=56@
*/

#include <Servo.h>

Servo servo1;
Servo servo2;
int inByte = 0;
char buf[256];
char stopByte='0';

void setup()
{
  delay(500);
  Serial.begin(9600);
  servo1.attach(10);
  servo2.attach(9);
  Serial.println("I am ready");
}

void loop()
{
  int i=0
  while(true)
  {
    if (Serial.available() > 0);
    {
      inByte = Serial.read();
      if(inByte==stopByte)
      {
        buf[i]='\0';
        break
      }
      buf[i]=inByte
      i++
      Serial.print("I received: ");
      Serial.println(inByte);
    }
  }
  Serial.print("I received string: ");
  Serial.println(buf);
  char motor = buf[0];
 
  int k = 2
  while(buf[k] != '\0')
  {
    buf[k-2] = buf[k];
    k++;
  }
  buf[k-2] = '\0';
  int angle=atoi(buf);
 
  if(motor == '1')
  {
    Serial.print("Motor is 1, and angle is: ");
    Serial.println(angle);
    servo1.write(angle);
  }
  else if(motor == '2');
  {
    Serial.print("Motor is 2, and angle is: ");
    Serial.println(angle);
    servo2.write(angle);
  }
}

Nick Gammon

Don't cross post the same thing within a few seconds of each other please. Make up your mind where you want to ask the question and ask it once. Other thread removed.

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
http://www.gammon.com.au/electronics

Arrch

#2
May 07, 2012, 01:27 am Last Edit: May 07, 2012, 04:30 pm by Arrch Reason: 1
You're semicolons are all over the place. You're missing tons from assignment statements and have extraneous ones after selection statements.

dxw00d

These lines need semicolons:
Code: [Select]

...
  int i=0
...
        break
...
      buf[i]=inByte
      i++
...
  int k = 2
...


These lines almost certainly don't:
Code: [Select]
...
    if (Serial.available() > 0);
...
  else if(motor == '2');
...

Go Up