Example:
{123,321,456,654,789,987}
{123,321,456,654,789,987}
{123,321,456,654,789,987}
{123,321,456,654,789,987}
{123,321,456,654,789,987}
{123,321,456,654,789,987}
{123,321,456,654,789,987}
{123,321,456,654,789,987}
{123,321,456,654,789,987}
{,,,,,}
{,,,,,}
{,,,,,}
{,,,,,}
{,,,,,}
{,,,,,}
{,,,,,}
When I look at the receiver just plugged into the XBee XCTU, the data never appears to stop getting sent. I can leave it on for minutes and it will always show what I'm expecting.
Receiver code
Code: Select all
#include <string.h>
boolean cfound = false; // complete command found
boolean cstart = false; // start of command
int cp = 0; // char pointer
char stringIn[50]; // commandbuffer
char *parsed[10]; //parsed strings
int counter = 0; //initialise the counter
#define SOC '{'
#define EOC '}'
void setup() {
Serial.flush();
Serial.begin(9600);
}
void loop() {
int sa;
char bt;
sa = Serial.available(); // count serial buffer bytes
if (sa > 0) { // if buffer not empty process buffer content
for (int i=0; i < sa; i++){
bt = Serial.read(); // read one char from the serial buffer
if (cstart ) {
stringIn[cp] = bt;
cp++;
//Serial.print(bt);
}
if (bt == SOC) { // got a start of command
cstart = true;
//Serial.println("start");
}
if (bt == EOC && cstart) { // check for last command char )
cfound = true;
stringIn[--cp] = NULL;
break; // end for-loop
}
}
}
if (cfound) {
for (int i=0; i<cp; i++){
//Serial.print(stringIn[i]); //data is correct at this point
}
counter = 0; //initialise the counter
char *token = strtok(stringIn, ",");
if(token)
{
//Serial.println("Token!");
parsed[counter++] = strdup(token); // You've got to COPY the data pointed to
token = strtok(NULL, ","); // Keep parsing the same string
while(token)
{parsed[counter++] = strdup(token); // You've got to COPY the data pointed to
token = strtok(NULL, ",");
if(counter == 6)
{
Serial.print("{");
Serial.print(parsed[0]);
Serial.print(",");
Serial.print(parsed[1]);
Serial.print(",");
Serial.print(parsed[2]);
Serial.print(",");
Serial.print(parsed[3]);
Serial.print(",");
Serial.print(parsed[4]);
Serial.print(",");
Serial.print(parsed[5]);
Serial.println("}");
}
}
cp = 0;
stringIn[cp] = '\0';
parsed[0] = '\0';
token = "";
counter = 0;
cstart = false;
cfound = false;
}
}
}
Code: Select all
#include <Nunchuk.h>
#include <Wire.h>
int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
int r_en = 8;
int l_en = 9;
int16_t x_right, x_left, y_forward, y_reverse, tZ, tC;
int m, fwd,rvs,lft,rght,u,dwn;
void setup() {
//Serial
Serial.begin(9600);
//Nunchuck
Wire.begin();
nunchuk_init();
rst();
delay(1000);
}
void loop() {
if (nunchuk_read())
{
// Work with nunchuk_data
//Left / Right
x_right = nunchuk_joystickX();
if(x_right < 0)
{
x_left = x_right*-1;
}
if(x_right >= 10)
{
right();
}
else
{
//Serial stop
rght = 0;
//analogWrite(LPWM_Output, 0);
}
if(x_left >= 10)
{
left();
}
else
{
//Serial stop
lft = 0;
//analogWrite(RPWM_Output, 0);
}
//Forward / Reverse
y_forward = nunchuk_joystickY();
if(y_forward < 0)
{
y_reverse = y_forward *-1;
}
if(y_reverse >= 30)
{
reverse();
}
else
{
//Serial stop
rvs = 0;
//analogWrite(LPWM_Output, 0);
}
if(y_forward >= 30)
{
forward();
}
else
{
//Serial stop
fwd = 0;
//analogWrite(RPWM_Output, 0);
}
tC = nunchuk_buttonC();
if(tC == 1)
{
up();
}
else
{
u = 0;
}
tZ =nunchuk_buttonZ();
if(tZ == 1)
{
down();
}
else
{
dwn = 0;
}
if((tZ == 1) && (tC == 1))
{
rst();
}
}
delay(5);
Serial.print("{");
Serial.print(fwd);
Serial.print(",");
Serial.print(rvs);
Serial.print(",");
Serial.print(lft);
Serial.print(",");
Serial.print(rght);
Serial.print(",");
Serial.print(u);
Serial.print(",");
Serial.print(dwn);
Serial.println("}");
}
void forward()
{
m = map(y_forward, 10, 110, 0, 1024);
fwd = m;
//analogWrite(RPWM_Output, m);
//Serial.print("F,");
//Serial.println(y_forward, DEC);
}
void reverse()
{
m = map(y_reverse, 10, 110, 0, 1024);
rvs = m;
//analogWrite(LPWM_Output, m);
//Serial.print("B,");
//Serial.println(y_reverse, DEC);
}
void left()
{
m = map(x_left, 10, 110, 0, 1024);
lft = m;
//analogWrite(RPWM_Output, m);
//Serial.print("L,");
//Serial.println(x_left, DEC);
}
void right()
{
m = map(x_right, 10, 110, 0, 1024);
rght = m;
//analogWrite(LPWM_Output, m);
//Serial.print("R,");
//Serial.println(x_right, DEC);
}
void up()
{
//Serial.println("U");
u = 255;
}
void down()
{
//Serial.println("D");
dwn = 255;
}
void rst()
{
x_right = 0;
x_left = 0;
y_forward = 0;
y_reverse = 0;
}