piccolo firmata

Salve sto facendo questo codice:

#define PIN_MODE 100
#define DIGITAL_WRITE 101
#define DIGITAL_READ 102
#define ANALOG_WRITE 103
#define ANALOG_READ 104
              
void setup() {
  char c;
  Serial.begin(500000);
  Serial.flush();
  c = 0;
  Serial.write(c);
  c = 255;
  Serial.write(c);
  c = 0;
  Serial.write(c);
}
              
void loop() {
  char cmd;  
  if (Serial.available()>0) {
    cmd = Serial.read();
    switch (cmd) {
      case PIN_MODE: 
      cmd_pin_mode();
      break;
      case DIGITAL_WRITE:
      cmd_digital_write();
      break;
      case DIGITAL_READ:
      cmd_digital_read();
      break;
      case ANALOG_WRITE:
      cmd_analog_write();
      break;
      case ANALOG_READ:
      cmd_analog_read();
      break;
    }
  }
  
}
              
void cmd_pin_mode() {
    char pin,mode;
    while (Serial.available()<2);
    pin = Serial.read(); // pin number
    mode = Serial.read(); // 0 = INPUT, 1 = OUTPUT
    pinMode(pin,mode);
}
              
void cmd_digital_write() {
   char pin,output;
   while (Serial.available()<2);
   pin = Serial.read(); // pin number
   output = Serial.read(); // 0 = LOW, 1 = HIGH
   digitalWrite(pin,output);
}
              
void cmd_digital_read() {
   char pin,input;
   while (Serial.available()<1);
   pin = Serial.read(); // pin number
   input = digitalRead(pin);
   Serial.write(input);
}
              
void cmd_analog_write() {
   char pin,output;
   while (Serial.available()<2);
   pin = Serial.read(); // pin number
   output = Serial.read(); // PWM value between 0 and 255
   analogWrite(pin,output);
}
              
void cmd_analog_read() {
   char pin;
   int value;
   while (Serial.available()<1);
   pin = Serial.read(); // pin number
   value = analogRead(pin);
   Serial.write((value>>8)&0xFF); // 8 bits de poids fort
   Serial.write(value & 0xFF); // 8 bits de poids faible
}

non capisco per provandolo con la mia classe arduino.py,

il digitalread prende da solo i valore ed accende il led a random.

ora vi posto il codice python 3.7:

vi posto arduino.py

# -*- coding: utf-8 -*-
import serial

class Arduino():
    def __init__(self,port):
        self.ser = serial.Serial(port,baudrate=500000)
        self.PIN_MODE = 100
        self.DIGITAL_WRITE = 101
        self.DIGITAL_READ = 102
        self.ANALOG_WRITE = 103
        self.ANALOG_READ = 104
        self.INPUT = 0
        self.OUTPUT = 1
        self.LOW = 0
        self.HIGH = 1

    def close(self):
        self.ser.close()

    def pinMode(self,pin,mode):
        self.ser.write(chr(self.PIN_MODE).encode())
        self.ser.write(chr(pin).encode())
        self.ser.write(chr(mode).encode())

    def digitalWrite(self,pin,output):
        self.ser.write(chr(self.DIGITAL_WRITE).encode())
        self.ser.write(chr(pin).encode())
        self.ser.write(chr(output).encode())

    def digitalRead(self,pin):
        self.ser.write(chr(self.DIGITAL_READ))
        self.ser.write(chr(pin))
        x = self.ser.read(1)
        return ord(x)

    def analogWrite(self,pin,output):
        self.ser.write(chr(self.ANALOG_WRITE).encode())
        self.ser.write(chr(pin).encode())
        self.ser.write(chr(output).encode())

    def analogRead(self,pin):
        self.ser.write(chr(self.ANALOG_READ).encode())
        self.ser.write(chr(pin).encode())
        c1 = ord(self.ser.read(1).strip().decode("UTF-8"))
        c2 = ord(self.ser.read(1))
        return c1*0x100+c2
# -*- coding: utf-8 -*-
import time
from arduino import Arduino

ard = Arduino("COM4")
led = 9
button = 2
ard.pinMode(led,ard.OUTPUT)
ard.pinMode(button,ard.INPUT)

elif command == 'button':
        try:
            while True:
                  btn = ard.digitalRead(button)
                if btn == 1:
                    ard.digitalWrite(led,ard.HIGH)
                    print("premuto")
                else:
                    ard.digitalWrite(led,ard.LOW)
        except KeyboardInterrupt:
            time.sleep(0.5)
            main()

Avete idea come perché anche se tocco qualcosa nella breadboard mi sente e si accende da solo il led ed evitando il codice scritto in python.
grazie mille e buona serata.

Sto imparando sia software che programmazione python anche se hanno già fatto ma mi piace ricreare le cose.

Evidente ci sono dei "problemi" a livello di circuito su breadboard. Posta lo schema che proviamo a capirci qualcosa.