Pages: [1]   Go Down
Author Topic: serial communication for two servos  (Read 408 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/* 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);
  }
}
« Last Edit: May 06, 2012, 06:54:48 pm by wrightone » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're semicolons are all over the place. You're missing tons from assignment statements and have extraneous ones after selection statements.
« Last Edit: May 07, 2012, 09:30:40 am by Arrch » Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

These lines need semicolons:
Code:
...
  int i=0
...
        break
...
      buf[i]=inByte
      i++
...
  int k = 2
...

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

Pages: [1]   Go Up
Jump to: