The code below should transform a number into a binary code, then connect with the arduino-nano and depending on which binary code, 4 lights should light up. All the lights should only light up, if the binary code is a 1. In the console it works, but my function doesn´t get the connection to the arduino-nano.
The Code on my Arduino-nano, works perfectly fine, while entering manually.
Can someone help me?
import time
import serial
ser = serial.Serial('COM6',115200, timeout=0.5)
number = input('A decimal number please:',)
def decimalToBinary(num): # decimal into a binary code
return bin(num)
#[2:] zur entfernung von 0b
while True:
data = ser.readline() # try to get the connection to arduino nano
index = 0
while index < len(bin): # separate the binary code in individual numbers
binary = bin[index]
print(binary)
def light_up():
if index[0] == 1:
ser.write(b'u') #light up the first light
if index[1] == 1:
ser.write(b'd') # light up the second light
if index[2] == 1:
ser.write(b't') # light up the third light
if index[3] == 1:
ser.write(b'c') # light up the fourth light
light_up()