Operation Manual
Human-computer interfacing
135
Notes:
Code for the Arduino:
// set up the serial connection speed
void setup()
{
Serial.begin(9600);
}
void loop()
{
int inByte;
if (Serial.available()>0)
{
// read data from the Raspberry Pi
inByte = Serial.read();
// send data to the Raspberry Pi
Serial.write(inByte);
Serial.print(" = ");
Serial.println(inByte);
}
}
Python code for the Raspberry Pi:
import serial
# set up the serial connection speed
ser = serial.Serial('/dev/ttyACM0', 9600)
# main loop
while 1:
c = input('Enter a char: ')
if len(c) == 1:
# send data to the Arduino
ser.writ e(c.enc o d e())
# receive data from the Arduino
response = ser.readline()
print(response.decode().strip())