Hi -
I figured out how to send numbers from 0-9 to the arduino from python using Serial.read()-'0' . Is there a simple way to instead send a value larger than 0-9 to the arduino. Right now I turn on an LED to monitor whether the it is working. My plan is to turn on a stepper motor for a certain number of iterations. I want python to send the number of iterations to the arduino. Then I can assign that value (such as 32500) to the iteration counter that controls the motor. FYI I am a new to programming and dont have a great grasp of integers,bytes,strings, etc.
Heres the python code:
import serial
import time
arduino = serial.Serial(2, 9600, timeout = 1)
time.sleep(2)
while 1:
command = str.encode('1')
arduino.write(command)
time.sleep(1.5)
and the Arduino Code:
int led = 4;
int state = 0;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
state = Serial.read()- '0';
if (state == 1){
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
Serial.println(state);
}
while (Serial.available() > 0)
{
Serial.read();
}
}
}