Python-Arduino-Nano connection?

Viewed 105

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()

1 Answers

I think avoid characters send, rather send an array, with pin values. So can you for example analog values, and other complicated datas send.

If I don't understand your problem, wrtie a comment. Here is an example code:

//Arduino forum 2020 - https://forum.arduino.cc/index.php?topic=714968


int myArray[4]; //this value is the upgratable data !!lenght of data!!
byte* 

ddata = reinterpret_cast<byte*>(&myArray); // pointer for transferData()
size_t pcDataLen = sizeof(myArray);
bool newData=false;

void setup() {
    Serial.begin(115200);//baudrate
}

void loop() {
    checkForNewData();
    if (newData == true) {
        newData = false;
    }
    digitalWrite(2,myArray[0]);
    digitalWrite(3,myArray[1]);//etc
    }

void checkForNewData () {
    if (Serial.available() >= pcDataLen && newData == false) {
        byte inByte;
        for (byte n = 0; n < pcDataLen; n++) {
            ddata [n] = Serial.read();
        }
        while (Serial.available() > 0) { // now make sure there is no other data in the buffer
             byte dumpByte =  Serial.read();
             Serial.println(dumpByte);
        }
        newData = true;
    }
}

Python 3x:

import serial
from struct import *
import sys
import time
import random
import ast


try:
    ser=serial.Serial(baudrate='115200', timeout=.5, port='com8')    #!!!
except:
    print('Port open error')

time.sleep(5)#no delete!
while True:
    try:
        ser.write(pack ('4h',0,1,0,0))#the 4h is 4 element, and h is an int type data (read the documentation, and use bool variable, if not good work), and 0,1,0,0 will be the variables of myArray
        
        time.sleep(.1)#delay
    except KeyboardInterrupt:
        break
    except:
        print(str(sys.exc_info())) #print error
        break

#the delays need, that the bytes are good order
Related