SOLVED!!!! programming question : Arduino and xbee processing data

Good day,

I wanted to share my project, I am stuck in what is for me a big wall at the moment for me, I have tried different approaches and fail and wanted to see if a different approach should be taken in consideration or if what I am trying to achieve is feasible.

Basically trying to pass analog signal X&Y from a joystick from xbee to xbee and then render it in Arduino. I have tried to include the analog signal of the Y axe and I fail all the time.

Any assistance would be appreciated in completing this project which I would like to share once done.

Thank You

My settings:

Transmitter xbee series 1 settings

ATRE
ATMY1234
ATDL5678
ATDH0
ATID0
ATD02
ATIR64
ATWR

Receiver xbee series 1 settings:

ATRE
ATMY5678
ATDL1234
ATDH0
ATID0
ATWR

where I am at so far

sketch found in the Arduino receiver (no sketch needed in the transmitter Arduino)

/*
XBeeAnalogReceiveSeries1
 Read an analog value from an XBee API frame and set the value accordingly.
 */

#include <Servo.h>

int servoVal;      
int servoVal2;


Servo ST1, ST2; 


void setup() {

  // Servo
  ST1.attach( 6, 1000, 2000);
  ST2.attach(5, 1000, 2000);  

  Serial.begin(9600);
  configureRadio(); // check the return value if you need error handling
}

boolean configureRadio() {
  // put the radio in command mode:
  Serial.flush();
  Serial.print("+++");
  delay(100);
  String ok_response = "OK\r"; // the response we expect.
  // Read the text of the response into the response variable
  String response = String("");
  while (response.length() < ok_response.length()) {
    if (Serial.available() > 0) {
      response += (char) Serial.read();
    }
  }
  // If we got the right response, configure the radio and return true.
  if (response.equals(ok_response)) {
    Serial.print("ATAP1\r"); // Enter API mode
    delay(100);
    Serial.print("ATCN\r"); // back to data mode
    return true;
  } 
  else {
    return false; // This indicates the response was incorrect.
  }
}
void loop() {




  if (Serial.available() >= 14) { // Wait until we have a mouthful of data
    if (Serial.read() == 0x7E) { // Start delimiter of a frame
      // Skip over the bytes in the API frame we don't care about
      for (int i = 0; i < 10; i++) {
        Serial.read();
      }
      // The next two bytes are the high and low bytes of the sensor reading
      int analogHigh = Serial.read();
      int analogLow = Serial.read();

      int analogValue = analogLow + (analogHigh * 256);
      servoVal = map(analogValue, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)

      ST1.write(servoVal);                    	  	 // sets the servo position according to the scaled value  
    }
  }
}
  configureRadio(); // check the return value if you need error handling

No! No! No!

Use X-CTU to configure the radios (as you have apparently done), NOT the sketch!

  Serial.flush();

Just about the most useless, misused, function in the Arduino toolkit. Unless you can explain why you are calling this, DO NOT CALL IT!

}
void loop() {




  if (Serial.available() >= 14) { // Wait until we have a mouthful of data

No blank lines between functions. Instead, there are a bunch of useless blank lines at the start of the function. Work on your style. This sucks.

(no sketch needed in the transmitter Arduino

Bullshit!

Why are you putting the receiver in command mode, and then sending an API packet?

Good day,

Thank You for the feedback.

I have played around with the Arduino board and creating many projects, this is my first time embarking in a project and including xbees therefore I am a newb that is a fact.

ConfigureRadio function was to start the API mode via the board, I know I can do it via X-CTU by setting API mode to "1" but I was not sure therefore put both.

I will remove the ConfigureRadio function from the sketch and try by enabling via X-CTU

Why I put the Serial.Flush() function is because of the documentation I red on this function from this site.

"Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.) I will remove completely."

I will clean my sketch end remove any useless space and repost when I get home.

As for the Bull, The Arduino that is connected to the Xbee sending data only include the Setup and Loop , the analog data from the joystick never goes to the Arduino, the output of X axe is currently sent to pin 20 of the xbee series 1 which is analog input but again I might not even need to do this...still reading about that subject. i keep reading that xbees series 1 can only output pwm signal which makes sense but this the reason I opted for API mode instead.

My goal is to pass the analog signal of X&Y to the receiving Arduino and have the sketch do its thing and output to pin 5&6PWM from Arduino. If possible. Again it might not be the right approach but so far I was successful for 1 analog signal.

Thank You for your support I look forward for more help.

Fabiolus

I went back into my notes and will explain better.

Transmitter xbee Series 1 settings

ATRE RESET
ATMY1234 sets the identifier for an XBee
ATDL5678 ATDL and ATDH set the low byte and the high byte of the destination XBee
ATDH0 ATDL and ATDH set the low byte and the high byte of the destination XBee
ATID0 ATID sets the network ID (it needs to be the same for XBees to talk to one another).
ATD02 ATD02 configures pin 20 (analog or digital input 0)
ATD12 ATD12 configures pin 19 (analog or digital input 0)
ATIR64 tells the XBee to sample every 100 (64 hex) milliseconds and send the value to the XBee specified by ATDL and ATDH.
ATWR WRITE TO FIRMWARE AND SAVE

Receiver xbee Serie 1 settings:

ATRE
ATMY5678
ATDL1234
ATDH0
ATID0
ATWR

Sketch in the Arduino receiving

/*
Transmitter xbee settings

ATRE
ATMY1234
ATDL5678
ATDH0
ATID0
ATD02
ATD12
ATIR64
ATWR

Receiver xbee settings:

ATRE
ATMY5678
ATDL1234
ATDH0
ATID0
ATWR




XBeeAnalogReceiveSeries1
 Read an analog value from an XBee API frame and set the brightness
 of an LED accordingly.
 */

#include <Servo.h>

int servoVal;      
int servoVal2;

Servo ST1, ST2; 

void setup() {
  // Servo
  ST1.attach( 6, 1000, 2000);
  ST2.attach(5, 1000, 2000);  
  Serial.begin(9600);
  configureRadio(); // check the return value if you need error handling
}

boolean configureRadio() {
  // put the radio in command mode:
  Serial.flush();
  Serial.print("+++");
  delay(100);
  String ok_response = "OK\r"; // the response we expect.
  // Read the text of the response into the response variable
  String response = String("");
  while (response.length() < ok_response.length()) {
    if (Serial.available() > 0) {
      response += (char) Serial.read();
    }
  }
  // If we got the right response, configure the radio and return true.
  if (response.equals(ok_response)) {
    Serial.print("ATAP1\r"); // Enter API mode
    delay(100);
    Serial.print("ATCN\r"); // back to data mode
    return true;
  } 
  else {
    return false; // This indicates the response was incorrect.
  }
}
void loop() {
  if (Serial.available() >= 14) { // Wait until we have a mouthful of data
    if (Serial.read() == 0x7E) { // Start delimiter of a frame
      // Skip over the bytes in the API frame we don't care about
      for (int i = 0; i < 10; i++) {
        Serial.read();
      }
      // The next two bytes are the high and low bytes of the sensor reading
      int analogHigh = Serial.read();
      int analogLow = Serial.read();
      int analogValue1 = analogLow + (analogHigh * 256);
      servoVal = map(analogValue1, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)
      ST1.write(servoVal);                    	  	 // sets the servo position according to the scaled value  
       //     int analogValue2 = analogLow + (analogHigh * 256);
     // servoVal2 = map(analogValue2, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)
     // ST2.write(servoVal2);                    	  	 // sets the servo position according to the scaled value  
    }
  }
}

Code in the Arduino transmitting

void setup() {
}
void loop() {
}

NOTE: On the Series 1 XBees, the Arduino code needed to configure the radio
for API mode with an AT command (ATAP1). On Series 2 XBees, this is
accomplished by flashing the XBee with a different firmware version.
The reason for the return to data mode (ATCN) is because command mode
was entered earlier with +++ and a return to data mode to receive data
is required.

I dont see any problem with the sketch above. I am following an exercise from a book which are the same exact settings for xbees 1 and sketch to communicate via 2 arduino.

My goal is to pass the analog signal of X&Y to the receiving Arduino and have the sketch do its thing and output to pin 5&6PWM from Arduino. If possible. So far I was successful for 1 analog signal, need to include other analog signal from the joystick and this is where I am stuck.

Thank You for your support I look forward for more help.

Fabiolus

Any feedback help?

Any feedback help?

I suggested that you separate the configuration of the radios from the use for the radios. Use X-CTU to configure them. Use the sketch to read/write the data.

You ignored that suggestion.

I suggested that Serial.flush() was completely useless for what you are doing.

You ignored that suggestion.

I suggested (a little subtly, perhaps) that you should make up your mind whether you want to use AT mode (no automatic sending of data for sensors (if any) directly attached to the XBees) or API mode (such data, if any, will be sent).

You ignored that suggestion.

The "How to post a programming question" thread explains how to ask a question in this forum.

You have ignored that suggestion - no schematic to be seen.

So, no, no more feedback. At least, not from me.

Good day,

Sorry you are right in order to have proper guidance. After reflection I went back and revised everything again and included shematic of the controller and bot.

I have updated the settings made to the XBEES SERIES 1

Transmitter xbee settings

ATRE
ATDH0
ATID0
ATD02
ATD12
ATIR64
ATWR

Receiver xbee settings:

ATRE
ATDH0
ATID0
ATWR

I am getting no signal anymore…

XBeeAnalogReceiveSeries1
 Read an analog value from an XBee API frame and set the brightness
 of an LED accordingly.
 */

#include <Servo.h>

int servoVal;      
int servoVal2;

Servo ST1, ST2; 

void setup() {
  // Servo
  ST1.attach( 6, 1000, 2000);
  ST2.attach(5, 1000, 2000);  
  Serial.begin(9600);
}

      // The next two bytes are the high and low bytes of the sensor reading
      int analogHigh = Serial.read();
      int analogLow = Serial.read();
      int analogValue1 = analogLow + (analogHigh * 256);
      servoVal = map(analogValue1, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)
      ST1.write(servoVal);                    	  	 // sets the servo position according to the scaled value  
       //     int analogValue2 = analogLow + (analogHigh * 256);
     // servoVal2 = map(analogValue2, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)
     // ST2.write(servoVal2);                    	  	 // sets the servo position according to the scaled value  
    }
  }
}

please accept my sincere apology.
Fabiolus

Since I have research alternative for easier understanding and integration, i have found a library called “easytransfer”. I had somewhat some success via this new easier method, do you believe it would be easier for a novice?

currently one motor turns and I can somewhat control it via the joystick but its not 100% there…

xbees reset to factory default settings /\

remote control

#include <EasyTransfer.h>
EasyTransfer ET;

const int potpin1 = A0;
const int potpin2 = A1;

struct SEND_DATA_STRUCTURE{
  int servo1val;
  int servo2val;
};

SEND_DATA_STRUCTURE txdata;

void setup(){
  Serial.begin(9600);
  //  Serial.begin(115200);
  ET.begin(details(txdata), &Serial);
  // pinMode(potpin1, INPUT);
  // pinMode(potpin2, INPUT);

}

void loop(){

  int val1 = analogRead(potpin1);
  int val2 = analogRead(potpin2);
  val1 = map(val1, 0, 1023, 0, 179);
  val2 = map(val2, 0, 1023, 0, 179); 
  txdata.servo1val = val1;
  txdata.servo2val = val2;

  ET.sendData();
}

receiver

#include <Servo.h>

#include <EasyTransfer.h>
EasyTransfer ET;

Servo myservo1;
Servo myservo2;

Servo ST1, ST2; 

struct RECEIVE_DATA_STRUCTURE{
  int servo1val;
  int servo2val;
};

RECEIVE_DATA_STRUCTURE txdata;

void setup(){
  Serial.begin(9600);
  // Serial.begin(115200);

  ET.begin(details(txdata), &Serial);
  // Servo
  ST1.attach( 6, 1000, 2000);
  ST2.attach(5, 1000, 2000);
}

void loop(){
  if(ET.receiveData()){
    ST1.write(txdata.servo1val);
    ST2.write(txdata.servo2val);
    //ST1.write(map(txdata.servo1val, 0, 1023, 0, 180));
    //ST2.write(map(txdata.servo2val, 0, 1023, 0, 180));
  }
}

FirstBotController.fzz (8.01 KB)

FirstBotReceiver.fzz (9.75 KB)

FirstBotController.fzz (8.01 KB)

FirstBotReceiver.fzz (9.75 KB)

Good day,

Finally after many obstacles, faulty wires, wrong code etc… success :grin:.

I present to you communication between 2 arduinos with xbees series 1 with1 joystick passing analog X&Y to other arduino via xbees wireless!!! All using the EasyTransfer library from Bill Porter EasyTransfer Arduino Library « The Mind of Bill Porter

This sketch or method can be applied to many different project or other similar controlled bots with minimal coding expertise. I have search and red many books and none come even close to what you are about to see.

I hope you enjoy building your robots and making them controlled wireless.

Pre requisite:

2 xbees series 1 default settings(Out of the box!!) are ok but if you wish you can change the pan id but that is it!!!
1 joystick thumb joystick from ladyaya or equivalent 2 seperate joystick would work.

the way the sketch work you could have a mega arduino and include as much motors/servos/joystick/ button that you wish!!! have fun!!

2 arduino uno board
2 dc motors or servos
9 volts batterie
12/24 volts batterie

Please refer to schematic for wiring

Remote control Arduino sketch

#include <EasyTransfer.h>
EasyTransfer ET;

const int potpin1 = A0;
const int potpin2 = A1;

struct SEND_DATA_STRUCTURE{
int servo1val;
int servo2val;
};

SEND_DATA_STRUCTURE txdata;

void setup(){
  Serial.begin(9600);
  //  Serial.begin(115200);
  ET.begin(details(txdata), &Serial);
}

void loop(){

  int val1 = analogRead(potpin1);
  int val2 = analogRead(potpin2);
  val1 = map(val1, 0, 1023, 0, 180);
  val2 = map(val2, 0, 1023, 0, 180); 
  txdata.servo1val = val1;
  txdata.servo2val = val2;

  ET.sendData();
}

robot Arduino receiving

#include <Servo.h>
#include <EasyTransfer.h>
EasyTransfer ET;

Servo myservo1;
Servo myservo2;

Servo ST1, ST2; 

struct RECEIVE_DATA_STRUCTURE{
  int servo1val;
  int servo2val;
};

RECEIVE_DATA_STRUCTURE txdata;

void setup(){
  Serial.begin(9600);
  // Serial.begin(115200);

  ET.begin(details(txdata), &Serial);
  // Servo
  ST1.attach( 6, 1000, 2000);
  ST2.attach(5, 1000, 2000);
}

void loop(){
  if(ET.receiveData()){
    ST1.write(txdata.servo1val);
    ST2.write(txdata.servo2val);
  }
}

FirstBotController.fzz (8.17 KB)

FirstBotReceiver.fzz (7.88 KB)

If I want to connect the joystick directly to Uno what do I need to modify the receiving code for this to work without going wireless?

Thanks,

jimkan: If I want to connect the joystick directly to Uno what do I need to modify the receiving code for this to work without going wireless?

Thanks,

Why would you use this as a starting point, if you don't want to go wireless?