how to receive only 1 single value form python to arduino ?

python Code

i = results[1]
i *= 100
if i >= 75:
    print('Recognised as owner!')
    arduino.write(str.encode('1'))
else:
    print('not matches')

python code is about face recognition

serial monitor shows

11111111111111111111111111111111.....

arduino uno

#include <Servo.h>

Servo servo;

char value1;
void setup()
{
  servo.attach(8);
  servo.write(0);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0)

  value1 = Serial.read();
  Serial.print(value1);

if (value1 == '1') {
  digitalWrite(13, HIGH);
  servo.write(90);
  delay(100);
  }  
    for (int i = 10; i > 0; i--){
      Serial.println("go inside!!, hurry up !!");
      delay(1000);
    }
  servo.write(0);
  Serial.println("door is closed");
  delay(1000); 
}

my question is.
how can I get only a single "1" value from python? so it means there is no loop. for this code, I cant execute the door system properly because the Arduino assumes that there is a loop here, 1111.......

#include <Servo.h>

Servo servo;

char value1;
void setup()
{
  servo.attach(8);
  servo.write(0);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0){
    value1 = Serial.read();
    Serial.print(value1);
  }

if (value1 == '1') {
  digitalWrite(13, HIGH);
  servo.write(90);
  delay(100);
  } 
    for (int i = 10; i > 0; i--){
      Serial.println("go inside!!, hurry up !!");
      delay(1000);
    }
  servo.write(0);
  Serial.println("door is closed");
  delay(1000);
}

You were missing some curly braces around your first if-block.

That being said, you can pip install the pySerialTransfer package that allows you to send byte packets over USB from Python to an Arduino. The Arduino can then decode the packets using the SerialTransfer library.

To install pySerialTransfer:

pip install pySerialTransfer

To install SerialTransfer:

  • Open the Libraries Manager in the Arduino IDE
  • Search for SerialTransfer
  • Install the latest version of the library

Example Python code:

from pySerialTransfer import pySerialTransfer

if __name__ == '__main__':
    try:
        hi = pySerialTransfer.SerialTransfer(15)
    
        hi.txBuff[0] = 'h'
        hi.txBuff[1] = 'i'
        hi.txBuff[2] = '\n'
        
        hi.send(3)
        
        while not hi.available():
            print('Waiting for response')
            
            import time
            time.sleep(1)
            
        print('Response received:')
        
        recArray = []
        for char in range(hi.bytesRead):
            recArray.append(char)
        
        print(' '.join(recArray))
        
    except KeyboardInterrupt:
        pass

Example Arduino Code:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

Power_Broker:
That being said, you can pip install the pySerialTransfer package that allows you to send byte packets over USB from Python to an Arduino.

That looks neat.

...R

"I cant execute the door system properly because the Arduino assumes that there is a loop here, 1111......."

It looks like you may have a loop. If you don't clear value1 after you evaluate it, your code may run in a loop printing out the 1 continuously.

zoomkat:
"I cant execute the door system properly because the Arduino assumes that there is a loop here, 1111......."

It looks like you may have a loop. If you don't clear value1 after you evaluate it, your code may run in a loop printing out the 1 continuously.

I have no loop as the code shows above. Nah then how can I clear value1? any suggest ?

cefriandy:
I have no loop as the code shows above. Nah then how can I clear value1? any suggest ?

Have you carefully considered Reply #1?

...R

"I have no loop as the code shows above."

It just seems like in your code the first loop picks out 1 from the serial buffer and set value1 to 1. On the next loop if nothing is detected in the serial buffer, then the if statement is exited and the code continues to run in a loop with value1 set to 1 and is printed out as such. At the end of the code you might need to include something like value1 == '' to clear value1 before entering the next loop. Just a high level look.