39Views1Replies

Author Options:

Need help with Arduino. Xbees won't connect when I upload my code? Answered

I am working on a RC Gripper lifter that works with 2 Servos and 2 DC motors. For wireless control I am using a Series 1 configured Xbee. Now the problem is that the Xbees don't connect when I upload the code. They do connect if I upload some other small code. Here's the Sender Code

int potGripper = 1;
  int potLifter = 2;
  //int oldGripper = 0;
  int newGripper = 0;
  //int oldLifter = 0;
  int newLifter = 0;
  int midPin = 2;
  int leftPin = 3;
  int newGear =0;
  int oldGear=1;
void setup()
{
  Serial.begin(9600);
pinMode(midPin, INPUT);
pinMode(leftPin, INPUT);
pinMode(potGripper, INPUT);
pinMode(potLifter, INPUT);

}

void loop() {
  if ( digitalRead(midPin) == LOW)
  {
     oldGear = 1;
  }
  else if ( digitalRead(leftPin) == LOW)
  {
      oldGear = 2;
  }
 
  int oldGripper = analogRead(potGripper);
  int oldLifter = analogRead(potLifter);
  int Grippervalue = map(oldGripper,0,1023,0,9);
  int Liftervalue = map(oldLifter,0,1023,0,9);
  if(newGripper != Grippervalue)
  {
    Serial.println('!');
    Serial.println(Grippervalue);
    Serial.println('#');
    newGripper = Grippervalue;
  }
  else if(newLifter != Liftervalue)
  {
    Serial.println('@');
    Serial.println(Liftervalue);
    Serial.println('#');
    newLifter = Liftervalue;
  }
  else if( newGear != oldGear)
  {
    Serial.println(';');
    Serial.println(oldGear);
    Serial.println('#');
    newGear = oldGear;
  }
  delay(500);
}



And here's the receiver code-

#include <Servo.h>
//If serial dsn't work as expected, add  while(Serial.available()>0) Serial.read(); at end of each loop!
int gripperPin = 9;
int lifterPin = 10;
int rpmData = 0;
int motorLeft, motorRight, gripperData, lifterData, angle, flag;

Servo myGripper;
Servo myLifter;

void setup() {
  Serial.begin(9600);
  myGripper.attach(gripperPin);
  myLifter.attach(lifterPin);
  delay(500);
}

void loop() {
  while( Serial.available() == 0);
  int data = Serial.read() - '0';
if (data >=0)
  {
    if(data == ';') // For setting the motor RPM
    {
      rpmData= Serial.read() - '0';
      if (rpmData == 0)
      {
       motorLeft= 300;
       motorRight= 250;
      }
      else if (rpmData == 1)
      {
        motorLeft= 200;
        motorRight= 150;
      }
    }
    if (data == '!') // For the Gripper
    {
      gripperData = Serial.read() - '0';
      angle= map(gripperData, 0, 9, 0, 80);
      myGripper.write(angle);
     }
    if (data == '@') // For the Lifter
    {
      lifterData = Serial.read() - '0';
      angle= map(lifterData, 0, 9, 0, 160);
      myLifter.write(angle);
    }
    //if (data == '%') // For the Buttons
     if (data == '#') // For marking end of Serial Data
     {
      flag = 0;
     }
     }
    }

My main aim is to send a value between 0-9 from one arduino to another so that I can map it on the other arduino from 0-160 and 0-80 (I am using 2 servos). I have used a symbol to differentiate the values from each other so that the correct value reaches the desired servo/motor. Please help me figure out my mistake..

Discussions

None
steveastrouk

4 years ago

forget the xbee to begin with, use a serial emulator to talk to your devices. Your receiver loop is very wrong. You need to wait to make a string, parse the string, then act on it. Your packet would ideally be something like '!12<cr><lf>' your code would see the <lf> and say'ah, i have the start of New message, i shall erase my message queue, and records everything after, until i see a <cr>...then when a <cr> is received, the message is received, and the code THEN reads the content. .