I am making a script in Python in which I change the number of the maximum_persons variable and now I am doing it so that from the arduino it reads what is entered by python
The code I have is
import serial, time
arduino=serial.Serial("/dev/ttyACM0",9600)
enviar=raw_input("Introduce un numero de aforo máximo")
arduino.write(str(enviar).encode())
arduino.write('\n'.encode())
ARDUINO
if (Serial.available()>0){
int maximo_personas = 15;
int enviado;
enviado=Serial.parseInt();
maximo_personas==enviado;
}
python
On the Python side the script is simpler:
The function
input
gives me a unicode string that includes the ENTER at the end. I just need to applyenconde()
before I send it.Arduino
On the Arduino side we are going to use the LED mounted on the board to check the reception of data: we will make it blink
n / 2
times, wheren
is the value sent by Python:The caution in using
parseInt()
is that it doesn't read the ENTER at the end of the number, so we have to add aSerial.read()
to get rid of that character.Since
enviado
it is a global variable, it retains its value across successive iterations ofloop
.In each iteration the state of the LED is changed only once. This is the standard programming pattern in Arduino: each pass through
loop
should be as short as possible, in order to allow time to process other events on the board.