Hi , this is my last school year and i'm working with arduino and xbee for my project. ive got 2 arduino uno's and 2 xbee schields .
i'm making a boat , so i have a boat with 2 motors in it , and a remote control . each has 1 uno and 1 xbee.
i'll made 2 programs 1 program that reads 2 potentiometers , and calculates the speed.. this works pretty good , when i open serial monitor and i move one of the potentiometers its sends serial information : (L249R249)
L & R are the left and right motor , so this is what i wanted .
//Declaratie
int analogPin1 = 4;
int analogPin2 = 5;
int dL = 0;
int dR = 0;
int linksemotor = 0;
int rechtsemotor= 0;
int analogL = 5;
int analogR = 6;
int snelheidBegin = 0;
int richtingBegin = 0;
void setup() // toekennen functie van de I/O
{
pinMode(analogPin1, INPUT);
pinMode(analogPin2, INPUT);
pinMode(analogL,OUTPUT);
pinMode(analogR,OUTPUT);
Serial.begin(9600); // initializeer de seriele communicatie
} // einde van void setup
void loop()
{
int snelheid = map(analogRead(analogPin1), 0, 1023, 0, 499);
int richting = analogRead(analogPin2);
if (abs(snelheidBegin - snelheid)> 2 || abs(richtingBegin - richting)>2)
{
snelheidBegin = snelheid;
richtingBegin = richting;
if ((richting > 499) && (richting < 524)) {
dL = 0;
dR = 0;
}
else if (richting < 500) {
dL = 499 - richting;
}
else {
dR = richting - 524;
}
if (dL > snelheid){
dL = snelheid;
}
if (dR > snelheid){
dR = snelheid;
}
linksemotor = snelheid - dL;
rechtsemotor = snelheid - dR;
Serial.print("(");
Serial.print("L");
Serial.print(linksemotor/2);
Serial.print("R");
Serial.print(rechtsemotor/2);
Serial.print(")");
delay (50);
} // einde van if(snelheidBegin != snelheid || richtingBegin != richting)
} // einde van voidloop
in mij other arduino i have another program that must recieve this information and use it with PWM to control the motors of the boat
when i open serial monitor , and i type : (L0R249)
the left motor stand still , and the right motor is rotate at his max speed , i can type every singal , it works fine.
//Declaratie
#include <math.h>
int PWMout1 = 5;
int PWMout2 = 6;
String inputString = ""; //
String strL = ""; //
String strR = "";
int charCount = 0; //
unsigned long valL = 0; //
unsigned long valR = 0;
int L = 0; //
int R = 0; //
int E = 0;
boolean stringComplete = false; //
char STX = '(';
char ETX = ')';
void setup()
{
// toekennen functie van de I/O
pinMode(PWMout1, OUTPUT);
pinMode(PWMout2, OUTPUT);
// initializeer de seriele communicatie
Serial.begin(9600);
// reserveer 200 bytes voor de inkomende string
inputString.reserve(200);
}
void loop()
{
if (stringComplete)
{
Serial.println(inputString);
Serial.println(charCount);
L = inputString.indexOf("L");
R = inputString.indexOf("R");
E = inputString.indexOf(")");
Serial.print("posities LR: l");
Serial.print(L);
Serial.print("r");
Serial.print(R);
strL = inputString.substring(L+1,R);
strR = inputString.substring(R+1,E);
valL = convertString2int(strL);
valR = convertString2int(strR);
Serial.println(strL);
Serial.println(strR);
Serial.println(valL);
Serial.println(valR);
Serial.println("the end");
analogWrite(PWMout1,valL);
analogWrite(PWMout2,valR);
inputString = "";
strL = "";
strR = "";
charCount = 0;
valL = 0;
valR = 0;
stringComplete = false;
}
} // einde van void loop
void serialEvent ()
{
while (Serial.available())
{
// read the new byte
char inChar = (char)Serial.read();
// add the byte to inputString
inputString += inChar;
// increment charCount with 1
charCount = charCount+1;
// if the incoming character is a ')', set a flag
// so the main "loop" can start parsing inputString.
if (inChar == ETX)
{stringComplete = true;}
}
} // einde van void serialEvent
unsigned long convertString2int(String x)
{
Serial.println();
Serial.print ("start converting ");
Serial.print (x);
Serial.print (" to int - length =");
unsigned long addVal=0;
unsigned long sum=0;
unsigned long result=0;
unsigned long digit = 0;
unsigned long length = x.length();
unsigned long tenPow =1;
Serial.println (length);
for (unsigned long i=0; i<length ; i++)
{
Serial.print ("tenPow = ");
Serial.println (tenPow);
Serial.print ("loop: ");
Serial.print(i);
Serial.print (" - digitStr: ");
String digitStr = x.substring(length-(i+1),(length+1)-(i+1));
Serial.print(digitStr);
if(digitStr == "0")digit=0;
if(digitStr == "1")digit=1;
if(digitStr == "2")digit=2;
if(digitStr == "3")digit=3;
if(digitStr == "4")digit=4;
if(digitStr == "5")digit=5;
if(digitStr == "6")digit=6;
if(digitStr == "7")digit=7;
if(digitStr == "8")digit=8;
if(digitStr == "9")digit=9;
Serial.print (" - digit: ");
Serial.print (digit);
addVal = round (round (tenPow)*round(digit));
Serial.print (" - addVal: ");
Serial.print (addVal);
Serial.print (" - former result ");
Serial.print (sum);
sum = round (addVal + round(sum)); //
Serial.print (" - new result ");
Serial.println (sum);
tenPow = round(tenPow * 10); //
result = sum;
}
return result;
}
but when i put the jumpers on xbee , and try the programs , nothing moves , the recieve LED on the arduino with motors isn't on or blinking ...
i already configurated the xbee's with ctu and i tested with this program and its worked : http://arduino.cc/it/Guide/ArduinoXbeeShield
so i dont get it .. i got the right information to send , my recieve program works perfect , my xbee's are configurated perfect , the test program works but al together it dont ..
i hope someone knows why ..
already thanks & grtz from belgium !