Send and read out two numbers from Arduino to Raspberry

Hej guys,

me and my friend are currently working on a small project we wanted to realize, including an Arduino, an ultrasonic sensor and a connected Raspberry Pi via an ZigBee XBee.

The idea is quite simple: The ultrasonic sensor recognises movement in a room and sends either a 1 or a 0 to a RaspberryPi, that is able to read out the data.

This is our code so far:

#define echoPin 11
#define trigPin 12
#define greenLED 13
#define redLED 10
#define yellowLED 4 //define shortcuts for pins
long duration, distance; //variable for reading and writing dauration and distance
int readingspeed = 50; // <-- set reading speed here in ms
int triggerdistance =100; // <-- set trigger distance here in cm

void setup() {
  Serial.begin(9600); //initiate serial connection to XBee module
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); //set sensor pins
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(13, OUTPUT); //set pin modes
}

char a;

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW); //trigger sensor 
  duration = pulseIn(echoPin, HIGH); //determine the duration of the pulse in 
  distance = duration / 58.2; //calculated distance from duration of pulse in

  if (distance <= triggerdistance) { //write "1" to serial if distance >= triggerdistance, trigger LEDs accordingly

    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
    Serial.write(1);
  }
  else { //write "0" to serial if distance =< trriggerdistance

    digitalWrite(greenLED, HIGH); 
    digitalWrite(redLED, LOW);
    Serial.write(0);
  }

  delay(readingspeed); //stop code for readingspeed amount of time

}

This worked really fine for us, using a bit of Python code to read the data out, giving us a list of 1s and 0s in a row.

import serial

while True:
	ser = serial.Serial(“/dev/ttyAMA0”, 9600, timeout=1)
	response = ser.read()
	print response

But now we would like to introduce a second sensor into our system and since we would have several ones now, I’d like to give each of them a serial number, so I can distinguish movement in one room from the other.

My idea was to just alter the lines handling the serial connection to something like

  digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
    Serial.printIn(1);
    Serial.printIn(0001);

and

digitalWrite(greenLED, HIGH); 
    digitalWrite(redLED, LOW);
    Serial.printIn(0);
    Serial.printIn(0001);

but this gave us a very strange looking output, as you may be able to see in the attachments.

Maybe one of you guys know how to do the transfer of a “serial number” and the status in a better way.

Best regards

Screen Shot 2016-02-28 at 17.40.35.png

Have a look at this Python - Arduino demo. I think it will provide a basis for what you want.

The code in Serial Input Basics is similar but was written more recently.

…R