How to read data from Arduino board and use it in python code

Hello,
In Arduino code, I have

const int buttonPin = 4;
const int buttonPin2 = 9;
const int ledPin = 6;
const int ledPin2 = 12;
int buttonState = 0;  

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(115200);
  Serial.flush();
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH )
  {
    digitalWrite(ledPin, LOW);  
  }
  else
  {
    digitalWrite(ledPin, HIGH);
    Serial.write("BUTTON1PRESSED");  
  }

  buttonState = digitalRead(buttonPin2);
  if (buttonState == HIGH )
  {
    digitalWrite(ledPin2, LOW);  
  }
  else
  {
    digitalWrite(ledPin2, HIGH);
    Serial.write("BUTTON2PRESSED\n");     
  }  
}

==========
I'd like to detect that a button is pressed and do some action in my Python code, like this:

# Importing Libraries
import serial
import time

arduino = serial.Serial(port='COM4', baudrate=115200, timeout=.1)

def readFromButton():
    value = arduino.readline()
    print(value) 
       
    time.sleep(1)
    if ( value == "BUTTON1PRESSED\n"):
        print('button1 pressed\n')
        
    elif ( value == "BUTTON2PRESSED\n"):
        print('button2 pressed\n')
  
while True:
    readFromButton()

=======
The problem is that when I push a button , I got a lot of either BUTTON2PRESSED\n or BUTTON1PRESSED\n printed and I don't know how to get a single output from Arduino et take action wrt the output value, i,e, when I press the button, I change something in my Python code. Here, I don't have print('button1 pressed\n') nor print('button1 pressed\n') printed.
Also, is my test comparing value correct ?

Thank you very much,
Best regards

Read the built in example state change detection or use a button library, there are many.

Thank you for replying.
I will check such library.
My other question is about reading value from Arduino in my python function.
I send, say an int value from Arduino, how can I read abd convert it into an int value as well in my python code ?
Could you give an exemple that works ?
Thank you very much

My experience is that I found best to use tasks in python to listen to the Serial port, I gave an example in this post

Hi,
I send an integer from Arduino and I'd like to read and convert it in integer value in my python code.
How could I do that?

Do you want to do something like this?
https://create.arduino.cc/projecthub/ansh2919/serial-communication-between-python-and-arduino-e7cce0

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Thank you for the warning.

if you are interested in Python have you looked at MicroPython on the ESP32

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.