Currently, I'm using an arduino to read a joystick position, which outputs 3 values. A switch button output (1 or 0), x coord (0 - 1023), and y coord (0 - 1023). I use Serial.print to print the values to the serial monitor and using Raspberry Pi's grabserial, I get the serial data to the pi. However, I'm using ser.readline().decode('utf-8')[:-2] and I can't seem to be able to assign data to a variable. I'm trying to store the 3 most recent data values (switch, x coord, y coord) into 3 separate variables so that I can say if 'switch' is less than [something] and greater than [something] then play some command. How do I store the 3 most recent data values into 3 variables?
I have tried to use something like 'switch' = ser.readline() if 'switch' == 1: then print("switch is not pressed") which should be printing but it says 'switch' is not equal to 1 so the data is not correctly assigned into a variable.
#Arduino
// Arduino pin numbers
const int SW_pin = 2; // connected to digital pin 2
const int X_pin = 0; // connected to analog pin 0
const int Y_pin = 1; // connected to analog pin 1
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(9600);
}
void loop() {
Serial.println(digitalRead(SW_pin));
Serial.println(analogRead(X_pin));
Serial.println(analogRead(Y_pin));
delay(500);
}
# Raspberry Pi
import serial
ser = serial.Serial("/dev/ttyACM0", 9600, timeout = 0.5)
While True:
Switch = ser.readline().decode('utf-8')[:-2]
if Switch == 1:
print ("Switch is not pressed")
I expected that it would print "switch is not pressed" every 3 values, but it just prints "1". Right now I'm trying to make one reading work which it does not, but I need all three of them working at the same time.