I've been working over a week on this, there is a lot to find on the internet, but I cannot seem to get this to work. I'm overlooking a major point.
I'm trying to get communication between two Xbee S1's (terminal of X-ctu receives the packets send by the Arduino so the link works!)
On the sender side have a LCD to monitor the values. The receiver side uses softwareserial to debug.
Sender code:
// Transmitter
//LCD Library
#include "U8glib.h"
U8GLIB_PCD8544 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, Reset = 8
//declarations array
int arBytes = 10;
int val;
int inData[3]; // 2 values
int index;
int inInt;
boolean conf = false;
byte first = 0;
byte second = 1;
//array stuff
int controlArray[5];
char inChar = '!';
byte test = 0;
boolean comm = false;
//LCD Draw screen
void draw(void)
{
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_6x13);
u8g.setPrintPos(0, 10);
u8g.print(inData[0], DEC);
u8g.setPrintPos(25, 10);
u8g.print(inData[1], DEC);
//Draw array new line
u8g.setPrintPos(0, 22);
u8g.print("Array");
u8g.setPrintPos(35, 22);
u8g.print(controlArray[0], DEC);
u8g.setPrintPos(60, 22);
u8g.print(controlArray[1], DEC);
//ArraySize new line
u8g.setPrintPos(0, 34);
u8g.print("Size");
u8g.setPrintPos(60, 34);
u8g.print(sizeof(controlArray), DEC);
/* //Message new line
if(comm == true)
{
u8g.setPrintPos(0, 46);
u8g.print('XBEE ON', DEC);
}
else
{
u8g.setPrintPos(0, 46);
u8g.print('ERROR', DEC);
}
*/
}
void setup(void)
{
// flip screen, if required
// u8g.setRot180();
// set SPI backup if required
//u8g.setHardwareBackup(u8g_backup_avr_spi);
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(3); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
// Start serial comm with xbee
Serial.begin(9600);
}
void loop()
{
// picture loop
u8g.firstPage();
do
{
draw();
}
while( u8g.nextPage() );
//Read analog values
for (int count=0;count<2;count++)
{
int b1 = analogRead(count);
b1 = map(b1, 0, 1023, 0, 255);
constrain(b1, 0, 255);
controlArray[count] = b1;
}
//SEND DATA PACKAGE
Serial.write("<");
for (int count=0;count<2;count++)
{
if(count<2)
{
Serial.write(",");
}
Serial.write(controlArray[count]);
}
Serial.write(">");
delay(500); //Wait for receiver to
}
it is sending packets like: <,255,255>
This seems to work fine!
Receiver code:
//Receiver
//SOFTWARE SERIAL
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int val = 0;
char inChar;
byte inByte;
char inData[10]; // 2 values
int index;
boolean started = false;
boolean ended = false;
int LED1 = 6;
int LED2 = 9;
void setup(void)
{
// Start serial comm with PC
Serial.begin(19200);
// Start serial comm with xbee
mySerial.begin(9600);
//declare Led pins
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.println("Setup done!");
}
void loop(void)
{
if(mySerial.available() > 0);
{
inChar = mySerial.read();
if(inByte == '<')
{
started = true;
index = 0;
}
else if(inByte == '>')
{
ended = true;
}
else if(inByte == ',')
{
inData[index] = mySerial.read();
index++;
}
else if(started && ended) // all bytes are in the array, let's do something!
{
for (int count=0;count<2;count++)
{
Serial.print(inData[count], DEC);
Serial.print(",");
}
analogWrite(LED1, inData[0]);
analogWrite(LED1, inData[1]);
// reset statements
started = false;
ended = false;
delay(5);
}
}
}
It tries to parse the code into an array to use later for motor control. It just does nothing. I tried working with zoomkat's code, but it just gives me errors on the string- end: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285370583