Read Analog data and send from one arduino Xbee to another

I’m new to this and a bit lost. Any help is very much appreciated.

Trying to get one Arduino Xbee to send the data read from Analog input to another Arduino Xbee

I use two Arduino Uno with wireless proto shield and wireles SD shield.

My code for the sender :

int port = A0;
float val = 0;
float offset = 0.05;
void setup()
{
Serial.begin(9600);
}

void loop()
{
val = analogRead(port);
float voltage = offset + 5.0*val/1023.0;
Serial.println(voltage);
delay(1000);
}

And for the receiver:

#include <SD.h>

File myFile;

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

}

void loop()
{
float data = Serial.read();
Serial.println(data);
delay(1000);
}

When looking at the serial monitor in the sender the data is fine but at the receiver I only get repetitive data, exactly this:52.00 51.00 13.00 10.00 52.00 51.00 13.00 10.00 52.00 51.00 13.00 10.00. Like this all the time. Even if I vary the input the received data remains the same

However if I send simple H and L signals the data is received perfectly fine. I don’t understand why I have this problem with numbers.

Any ideas? ty

Any ideas? ty

The below looks strange as you are sending the single captured byte as a decimal float. Is that what you intend to do? The "13.00 10.00" appears to be the terminating carriage return and line feed at the end of the data.

void loop()
{
  float data = Serial.read();
  Serial.println(data);
  delay(1000);
}

zoomkat: The below looks strange as you are sending the single captured byte as a decimal float. Is that what you intend to do? The "13.00 10.00" appears to be the terminating carriage return and line feed at the end of the data.

Not sure really. What I want to do is to send the read voltage to the other Xbee with Arduino and him to receive it correctly. I made that code because I saw it in an example but maybe its better to send it as byte as you say. How could I do that? I have been reading that I should use API and send it as two bytes. But working on it. If you have an example of how to do it I would very much appreciate it.

ty

Below is some serial test code for sending data from one arduino to another arduino. You could just send the raw voltage value to the second arduino and do the voltage conversion on the second arduino.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Hi, thank you for your answer.

If I understand this is done in only one Arduino. I would prefer to do it from one Arduino to the other.

What code would I have to use in each Arduino or could I use the same code you gave me for both. I would prefer if you could send me a code in which you read from A0 and send the integer value and then read on the other Arduino. The strange thing is that I send characters fine and even sentences but with numbers it does not work well. I don’t know why.

thank you for your time

or could I use the same code you gave me for both.

That is correct. I suggest you establish basic communication between two arduinos, then work on the details of your project. Try sending numbers like 769 from one arduino to the other, then convert the 769 to its represenative voltage value.

Ok, so I finally solved the problem. I will copy the code I used in both Arduinos for future reference:

Arduino which reads analog0 and sends through serial with a one second delay:

void setup()
{
Serial.begin(9600);
}

void loop()
{
int dato = analogRead(A0);
Serial.println(dato);
delay(1000);
}

And the receiving Arduino. The thing here is that the data is read as char and converted to integer and then to voltage. Carriage return is chosen as the separator between different data values:

float offset = 0.0;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop(){
if(Serial.available() > 0)
{
static char input[16];
static uint8_t i;
char c = Serial.read ();

if ( c != ‘\r’ && i < 15 ) // assuming “Carriage Return” is chosen in the Serial monitor as the line ending character
input[i++] = c;

else
{
input = ‘\0’;

  • i = 0;*

  • int dato = atoi(input);*
    _ float voltage = 5.000*dato/1023.000+offset;_

  • Serial.println(voltage,3);*
    }
    }
    }

Thank you. I’m trying to do the same but I keep on getting error when input =’\0’;

Can you clarify more on how to assign input array

Hel

solar82:
Ok, so I finally solved the problem. I will copy the code I used in both Arduinos for future reference:

Arduino which reads analog0 and sends through serial with a one second delay:

void setup()
{
Serial.begin(9600);
}

void loop()
{
int dato = analogRead(A0);
Serial.println(dato);
delay(1000);
}

And the receiving Arduino. The thing here is that the data is read as char and converted to integer and then to voltage. Carriage return is chosen as the separator between different data values:

float offset = 0.0;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop(){
if(Serial.available() > 0)
{
static char input[16];
static uint8_t i;
char c = Serial.read ();

if ( c != ‘\r’ && i < 15 ) // assuming “Carriage Return” is chosen in the Serial monitor as the line ending character
input[i++] = c;

else
{
input = ‘\0’;

  • i = 0;*

  • int dato = atoi(input);*
    _ float voltage = 5.000*dato/1023.000+offset;_

  • Serial.println(voltage,3);*
    }
    }
    }
    [/quote]