arduino reading serial from pyserial for stepper motor setspeed

Hey everyone,

I have written the following arduino code to try and read a serial value sent over from pyserial (python). I want the ascii character that I sent over by python to be read by the arduino as an integer value which will then be used as the speed value for the stepper motor.

#include <Stepper.h>

int s = 0;
const int stepsPerRevolution = 48;
Stepper myStepper = Stepper(stepsPerRevolution, 8,9,10,11);

void loop() {
myStepper.step(stepsPerRevolution);
}

void setup() {
Serial.begin(9600);
s = Serial.read()-'NUL'; //convert ascii to int. read from serial port and substract serial 0
myStepper.setSpeed(s);
}

Here is my python code which sends over the speed setting in ascii:

import sys
import serial
import time

ser = serial.Serial('/dev/ttyACM5', 9600)
ser.timeout = 2
ser.open()

s = input('Enter a speed from 0-70')
ser.write('s')

However nothing happens on the arduino front. I have compiled the arduino code which seems to work out fine.

Any suggestions would be greatly appreciated. Much thanks,

Tom

s = Serial.read()-'NUL';

That's not sensible.
What are you trying to do?

i want to use python to set the speed of my stepper motor. so basically i want to use pyserial:

serial.write( "number - which will be then be sent to arduino as the speed value for the stepper motor")

Thank you

Tom

Mostly, Serial.read is going to return -1.
It's always best to check there's something available to read, before reading it.

so all i would need to do is add one more line of code in python to verify what i am sending the correct ascii character?

No, I'd add Arduino code to check that there's something available to read, before reading it.
I'm pretty sure it is set out in the examples.

so then otherwise everything should look okay? and if there isn't anything sent, what could that possibly mean? what would i then need to do?

I will check the example arduino code as well.

Thanks

so then otherwise everything should look okay?

No. That 'NUL' is still wrong. 'NUL' is a multibyte character constant which will not have the value you think it does (whatever it is you think it does).