Hey there,
I am trying to set up a communication between a python script on my pc and my Arduino.
I'm already able to turn a LED on and off with two keyboard shortcuts.
But I encountered a problem when l tried to toggle the LED with one shortcut
because the boolean saving the current led state ('led') is always false when serialEvent() starts.
Here's my Arduino code:
unsigned int led_pin = 13;//the pin of the integrated led
boolean led = false;//the boolean for the led state
String msg = "";
void setup() {
Serial.begin(9600);
pinMode(led_pin,OUTPUT);
digitalWrite(led_pin,false);
}
void serialEvent() {
while(Serial.available()){
char inChar= (char) Serial.read();//add all received chars to a string
if(inChar=='\n'){//python finished sending
msg+='\0';//null-terminate the string
if(msg=="led_on"){
led=true;
digitalWrite(led_pin,led);
Serial.println(msg);
msg="";
}else if(msg=="led_off"){
Serial.println("led_off");
led=false;
digitalWrite(led_pin,led);
Serial.println(msg);
msg="";
}else if(msg=="led"){ //MY PROBLEM IS HERE:
Serial.print(led);//print the value of led
led= !led;//change it
Serial.println(led);//print it again; it should have changed
digitalWrite(led_pin,led);//toggle the LED
msg="";
}
}else{
msg+=inChar;
}
}
}
void loop(){}
And here's the python script although it doesn't seem relevant for me:
import pythoncom,pyHook,sys,logging,serial,time
port="COM5"
def OnKeyboardEvent(event):#handles keyboard events
print("Key: ",event.Key)#print the key pressed
ctrl=pyHook.GetKeyState(pyHook.HookConstants().VKeyToID('VK_CONTROL'))#true if control is down
if event.Key == "L" and ctrl:#Ctrl+L: turn LED on
try:
arduino= serial.Serial(port,9600,timeout=.1)
time.sleep(3)
arduino.write(b"led_on"+b'\n')
data = arduino.readline().decode().rstrip('\n')
if data:
print(data)
except serial.serialutil.SerialException:
print("ERROR")
return False
elif event.Key=="K" and ctrl:#Ctrl+K: turn LED off
try:
arduino= serial.Serial(port,9600,timeout=.1)
time.sleep(3)
arduino.write(b"led_off"+b'\n')
data = arduino.readline().decode().rstrip('\n')
if data:
print(data)
except serial.serialutil.SerialException:
print("ERROR")
return False
elif event.Key=="J" and ctrl:#Ctrl+J: toggle LED
try:
arduino= serial.Serial(port,9600,timeout=.1)#open the port
time.sleep(3)#give it some time
arduino.write(b"led"+b'\n')#write 'led\n' to the port
data = arduino.readline().decode().rstrip('\n')#get the line the arduino answers
if data:
print(data)#print it
except serial.serialutil.SerialException:
print("ERROR")
return False
return True
hm=pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
The console output in python if I press Ctrl+J(the shortcut for toggling the LED) twice:
Key: Lcontrol
Key: J
01
Key: J
01
The first numer after the key is always the value of 'led' at the start of serialEvent() and the second is the value at the end of it. As you see the value is 0(false) the second time Ctrl+J is pressed, which it shouldn't be because it was 1(true) after it was pressed the first time.
Does anybody have an idea on how to fix this issue?
I've been trying to find my mistake for several hours now and I can't find it.
I also tested this code with two different boards.