Sending Variables over Serial
Posted: Thu Mar 30, 2017 10:04 am
I am doing a project where i need to send the direction and speed over serial. If you look at the below code i send 'L095' meaning: Direction 'L' Left @ Speed '095' 95%. this code below works fine but the issue i have is that i first need to do a calculation for speed that will be constantly changing and can't seem to figure out how to send it over serial.
As i said the above code works with constant values but i need it to work with variables. The only thing that will remain constant is the 'L'
The code below is what i have come up with to send over serial but i can't seem to join the first character L with the value obtained from the speed calculation. Not sure if i'm explaining this very well but the end goal would be for example:
speed = 1.11 * (80-10) = 78 therefore the value i need to send is 'L078' meaning 'L' Left @ '078' 78%
I somehow need to get 'L078' into ser.write('????')
Any help would be appreciated, Thanks
Code: Select all
# Left state
if state == 1:
ser.write('L095')
# Change state?
if ((int(x) > w/3) & (int(x) < (w/3)*2)): state = 2 # Change to 'middle' state
if ((int(x) > (w/3)*2) & (int(x) < w)): state = 3 # Change to 'Right' state
The code below is what i have come up with to send over serial but i can't seem to join the first character L with the value obtained from the speed calculation. Not sure if i'm explaining this very well but the end goal would be for example:
speed = 1.11 * (80-10) = 78 therefore the value i need to send is 'L078' meaning 'L' Left @ '078' 78%
I somehow need to get 'L078' into ser.write('????')
Code: Select all
# Left state
if state == 1:
int(speed) = 1.11 * (radius - 10) # Speed calculation
if speed > 100: speed = 100 # Clip speed at max 100%
if speed < 0: speed = 0 # Clip speed at min 0%
ser.write('????') # Write value over serial ie. 'L078' Left at 78% speed
# Change state?
if ((int(x) > w/3) & (int(x) < (w/3)*2)): state = 2 # Change to 'middle' state
if ((int(x) > (w/3)*2) & (int(x) < w)): state = 3 # Change to 'Right' state