Hi guys, i am typically new to arduino and i am trying to complete my first project for my university using temperature, humidity sensors and xbee to transfer data from a standalone arduino uno to my “main” arduino Mega. I have managed to make my 2 xbees to communicate but i don’t understand exactly how to pass data from my remote arduino uno to Mega. Below is the code that is already in arduino uno memory and next is the code that i should fix in order to send data correctly. I don’t know exactly how to use a buffer to save data as char and then convert them to integer.
Arduino uno code
#include <dht11.h>
dht11 DHT11;
void setup()
{
DHT11.attach(13);
Serial.begin(9600);
}
void loop()
{
int chk = DHT11.read();
int sensorValue;
Serial.print((float)DHT11.humidity);
Serial.print(",");
Serial.print((float)DHT11.temperature);
Serial.print(",");
Serial.print(DHT11.fahrenheit());
sensorValue = analogRead(A0); // read analog input pin 0
Serial.print(",");
Serial.print(sensorValue, DEC); // prints the value read
Serial.println("\n");
delay(1000);
}
which produces the following result:
40.00,24.00,75.20,110
which is in order: humidity (%), temp (c), temp (F), analog value of a smoke detector.
Below is the problematic code that i use to transfer my data to Arduino Mega using buffer.
Arduino Mega code
void setup() {
// initialize both serial ports:
Serial.begin(9600); // Arduino Mega Serial
Serial3.begin(9600); // Arduino Uno Serial
int temp;
char buffer[32];
}
void loop() {
byte pos; // position in read buffer
char buffer[32];
// send data only when you receive data:
if (Serial3.available() > 0)
{
// read the incoming byte:
char inByte = Serial3.read();
// add to our read buffer
buffer[pos] = inByte;
pos++;
// check for delimiter
if (inByte = ',')
{
buffer[pos-1] = 0;
int temp = atoi(buffer);
Serial.println(temp);
}
else
{
}
delay(100);
}
}
Just need a short example of how to store them and convert them properly in order to be in as mentioned above. Thank you very much