Reading a character using Serial.Read

Hi there I am building a wireless sensor network for carbon monoxide detection using some xbees. I have been able to get two xbees working with two arduinos where one arduino reads the incoming byte using serial.read.

I am trying to add some more sensors and another xbee but i need the arduino to be able to differentiate what sensor the data is coming from. Currently the codes look something like this:

int analogPin = A0;
int analogPin2 = A1;
int ledPin = 13;

int sensorValue = 0;
int sensorValue1 = 0;

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);
pinMode  (analogPin, INPUT);
pinMode  (analogPin2, INPUT);
pinMode  (ledPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

sensorValue = analogRead(analogPin);
sensorValue1 = analogRead(analogPin2);

digitalWrite (ledPin, HIGH);

Serial.print("Gas level1=");
Serial.println(sensorValue);

delay(1000);

Serial.print("Gas level2=");
Serial.println(sensorValue1);

delay(1000);

This is one of the sensor nodes and it prints the value of each sensor connected to the analogPin. The code on the display arduino looks like this:

char incomingByte;
char incomingByte2;
char incomingByte3;
char incomingByte4;



void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

if (Serial.available() > 0) {
  
    incomingByte == Serial.read("Gas level1=");
    incomingByte2 == Serial.read("Gas level2=");
    incomingByte3 == Serial.read("Gas level3=");
    incomingByte4 == Serial.read("Gas level4=");

   
    Serial.print("Gas level1=");
    Serial.println(incomingByte);
    delay(1000);
    
   Serial.print("Gas level2=");
   Serial.println(incomingByte2);
   delay(1000);
   
   Serial.print("Gas level3=");
   Serial.println(incomingByte3);
   delay(1000);
   
   Serial.print("Gas level4=");
   Serial.println(incomingByte4);
   delay(1000);
   

}
    
}

I hope you get my idea/thinking on this. I would like the serial to read the characters and print it but i am currently having some difficulties. Thanks for your help.

Update: I keep getting this error message:

sketch_apr07b.ino: In function 'void loop()': sketch_apr07b.ino:19:46: error: no matching function for call to 'HardwareSerial::read(const char [12])' sketch_apr07b.ino:19:46: note: candidate is: In file included from /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino/Arduino.h:223:0

Pretty cool! Where did you find a Serial.read() function that takes a string argument?

    incomingByte == Serial.read("Gas level1=");

I think thats my main problem. With one sensor and one xbee, the code goes as:

 incomingByte == Serial.read();

that works but the addition of more sensors makes it difficult. at the moment, it is going to read everything on the serial and print without the user knowing where its coming from. i want to be able to differentiate it

Do you think you could instantiate more than one Serial object, like Serial1, Serial2, etc.?

If you are expecting to receive multiple characters look at the 2nd and 3rd examples in serial input basics

Perhaps you can send the data in the style <a,123> where ‘a’ identifies where it comes from and 123 is the value. The <> are start and end markers so the Arduino can know it has the correct data.

…R

Thanks I'm looking at that now. Still a little complicated but hopefully i'll get some results.

If you go with packets like Robin suggests, also check out Serial.readBytesUntil(). If you're not using interrupts, it works pretty good with packet data.