Hello, I am sending a servo position dat from the Pi to an Arduino. The
on the Pi side-the sender:
#!/usr/bin/python3
from time import sleep
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600) # Establish the connection on a specific port
while True:
deg=180
deg_b=deg.to_bytes(1, 'little')
ser.write(deg_b) #send the degree in one byte to Aruino
print (ser.readline()) # Read the newest output from the Arduino
sleep(.1) # Delay for one tenth of a second
On the receiver-Arduino
void setup() {
Serial.begin(9600); // set the baud rate
Serial.println("Ready"); // print "Ready" once
}
void loop() {
char inByte = ' ';
if(Serial.available()){ // only send data back if data has been sent
char inByte = Serial.read(); // read the incoming data
if(inByte) my_blink(12);
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
}//if
}//loop
void my_blink(int pin) {
digitalWrite(pin, HIGH);
delay(100);
digitalWrite(pin, LOW);
delay(100);
}
This send deg=180 from Pi to Arduino in one single byte, Arduino reads it the send it back to the Pi, I get b'\xb4\r\n', but how about the char inByte = ' '; ? What is the format in inByte = ' ';? how do I convert this inByte back to an integer 180 ? Thanks.