Society of Robots - Robot Forum

Mechanics and Construction => Mechanics and Construction => Topic started by: Eithman on December 08, 2012, 02:34:01 PM

Title: Xbee servo issue
Post by: Eithman on December 08, 2012, 02:34:01 PM
I am trying to make a remote control car with a couple of XBees, full rotation Servos and Arduinos.  Heres the code.

#include <Servo.h>
Servo Left; // servo on the left hand side
int one = 1;

void setup() {
        Left.attach(8);
   Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
   
    int data = Serial.read() - '0';
   
      if ( data == 1)
         {
   Left.write(180);    
          }
      }
   }

The servo is set up to pin 8. When the if statement reads that a 1 has been sent over serial and assigned to the int 'data', the servo should start spinning. But it spins regardless. Even if no data is being sent. If I keep the same code and only change it so that when the if statement is true, it turns on an led, it works fine. But for some reason, when it is supposed to turn the servo, it always reads true and turns the servo. Any ideas?
Title: Re: Xbee servo issue
Post by: jwatte on December 08, 2012, 09:02:37 PM
Try adding Left.write(0) to the setup function.
Also, try different values, both for setup and for "getting 1," as the zero calibration may be slightly off.
Finally, try printing something to the serial port after the Serial.read() function, to see if and how often you actually get there.