Hi,
I have series 2 xbee radio. (Xbee Adapter
I am actually building a follow-line robot (using ir sensors).
This robot can turn left/right on junctions and I currently I can control the robot turns using input letters through the terminal.
I'm attaching the code:
// analog sensors
int FrontSensorR = 0;
int FrontSensorL = 0;
int BackSensorR = 0;
int BackSensorL = 0;
//motors
int RMotorDir = 12; // Direction pin for motor B is Digital 12
int LMotorDir =12;// Direction pin for motor A is Digital 12
int VelMotorR = 9; // Speed pin for motor B is Digital 9 (PWM)
int VelMotorL = 10; // Speed pin for motor B is Digital 9 (PWM)
int speed=50,S=50;//Speed;
int OutLine=50,OnLine=100;
int sf=0,delay1=300,timeflag=0,timeflag2=0,code=0,checking=0,k1=0,sum=0,Asum=0;
int flag1=1,AR=1,ActiveSensor=0,L;
int q=0,q1=0;// Junction Number
void setup() {
Serial.begin(9600);
pinMode(RMotorDir, OUTPUT);
pinMode(LMotorDir, OUTPUT);
pinMode(0, INPUT); // digital sensor is on digital pin 0
pinMode(1, INPUT); // digital sensor is on digital pin 1
pinMode(2, INPUT); // digital sensor is on digital pin 2
pinMode(3, INPUT); // digital sensor is on digital pin 3
}
void loop()
{
if (Serial.available())
{
L = Serial.read();
for(int i=1;i<=31;i+=1)
{
if(L==64+i || L==64+32+i)//big or small, ASCI letter
{
q=i;
}
}
}
///////////////////////////////////////
if(q!=0)
{
checking=q1;
FrontSensorL = analogRead(0);
FrontSensorR = analogRead(1);
BackSensorL = analogRead(2);
BackSensorR= analogRead(3);
// send sensor values for printig
Serial.print(FrontSensorL, DEC);
Serial.print(",");
Serial.print(FrontSensorR, DEC);
Serial.print(",");
Serial.print( BackSensorL, DEC);
Serial.print(",");
Serial.print( BackSensorR, DEC);
Serial.print(",");
Serial.print(",AR=");
Serial.print( AR, DEC);
Serial.print(",checking=");
Serial.println( checking );
speed=S;
if( q1==q+1||q1==q+2) //wait around your letter
{
if(q1==q+1)
{
extern volatile unsigned long timer0_millis;
timer0_millis = 0;
timeflag2=millis();
q1++;
}
speed=0;
if(millis()>=timeflag2+5000)
{
q1++;
speed=S;
}
//delay(5000);
}
if (AR==1) //Active Regime 1,Follow Line
{
code=0;
ActiveSensor=BackSensorR;
if (FrontSensorR < OutLine && FrontSensorL < OutLine )// Go Straight
{
digitalWrite(RMotorDir, 12); // set direction
analogWrite(VelMotorR, speed); // set speed (PWM)
digitalWrite(LMotorDir, 12); // set direction
analogWrite(VelMotorL, speed); // set speed (PWM)
sf=0;
}
if(FrontSensorL < OutLine && FrontSensorR > OnLine)// Correction to Right
{
digitalWrite(RMotorDir, 12); // set direction
analogWrite(VelMotorR, speed-speed); // set speed (PWM)
digitalWrite(LMotorDir, 12); // set direction
analogWrite(VelMotorL, speed+sf); // set speed (PWM)
sf=+10;
}
if(FrontSensorR < OutLine && FrontSensorL > OnLine)//Correction to Left
{
digitalWrite(RMotorDir, 12); // set direction
analogWrite(VelMotorR, speed+sf); // set speed (PWM)
digitalWrite(LMotorDir, 12); // set direction
analogWrite(VelMotorL, speed-speed); // set speed (PWM)
sf=+10;
}
}
//////////////////////////////////
if(FrontSensorL > OnLine && FrontSensorR > OnLine && (q1==q ||q1==q+3) )
{
AR=2;//go to new Active Regime
}
//////////////////////////////////
if(FrontSensorL > OnLine && FrontSensorR > OnLine && q1==q+4 )
{
AR=3;//go to new Active Regime
q1=0;
}
//////////////////////////////////
if(AR==2)// Active Regime 2: Turn Left on Junction
{
ActiveSensor=FrontSensorR;
if(code==1)
{
AR=1;
ActiveSensor=BackSensorR;
}
digitalWrite(RMotorDir, 12); // set direction
analogWrite(VelMotorR,speed-speed); // set speed (PWM)
digitalWrite(LMotorDir, 12); // set direction
analogWrite(VelMotorL, speed); // set speed (PWM)
//sf=+10;
}
////////////////////////////////////
if(AR==3)// Active Regime 2: Turn Left on Junction
{
ActiveSensor=FrontSensorL;
if(code==1)
{
AR=1;
ActiveSensor=BackSensorR;
}
digitalWrite(RMotorDir, 12); // set direction
analogWrite(VelMotorR, speed); // set speed (PWM)
digitalWrite(LMotorDir, 12); // set direction
analogWrite(VelMotorL,speed-speed); // set speed (PWM)
}
/////////////////////////////
//GetCode
if ( ActiveSensor > OnLine && k1==0 ) //
{
extern volatile unsigned long timer0_millis;
timer0_millis = 0;
timeflag=millis();
k1=1;
Asum=0;
// code=0;
}
if (millis()<timeflag+delay1)// Scanning Time
{
flag1++; // Scanning Number kamut srikot
sum=sum+ActiveSensor;//
}
if (millis()>timeflag+delay1)// Scanning Result
{
Asum=sum/flag1;
flag1=1;
sum=0;
//code=0;
if(ActiveSensor < OutLine)
{
k1=0;
}
}
if (Asum > OnLine)
{
if(millis()>timeflag+delay1 && ActiveSensor==BackSensorR && BackSensorL < OutLine)
{
q1++;// placing code
}
code++; // service code
delay(100);//go out from junction
}
}
}
I got lost with the all information on how to connect the xbee. I just need to send a data (one letter) to the arduino.