Hi! I am trying to get these scripts to co-operate with each other using UART, but both of my scripts seem to think otherwise, My Arduino is trying to add the received data from python by one, and also displaying the incremented value onto python.
anyways, here is my code:
Arduino,
int x;
void setup() {
pinMode(12, OUTPUT);
Serial.begin(115200);
Serial.setTimeout(1000); // Set a timeout of 1 second
}
void loop() {
if (Serial.available()) {
x = Serial.readString().toInt();
Serial.print("Received value: ");
Serial.println(x);
x++;
Serial.print("Incremented value: ");
Serial.println(x);
// Blink the LED x times
for(int i = 0; i < x; i++) {
digitalWrite(12, HIGH);
delay(2000);
digitalWrite(12, LOW);
delay(2000); // Wait for 2 seconds before turning on again
}
}
}
import serial
import time
# Connect to the Arduino (adjust the port name as needed)
arduino = serial.Serial(port='COM5', baudrate=115200, timeout=.1)
def write_read(x):
arduino.write(bytes(x, 'utf-8'))
time.sleep(0.05)
data = arduino.readline().decode().strip() # decode and strip newline characters
return data
while True:
num = input("Enter a number: ") # Taking input from the user
time.sleep(1)
value = write_read(num)
print("Received value from Arduino:", value) # Printing the received value