Page 1 of 1

Getting Python script to respond to Arduino

Posted: Tue Nov 21, 2017 7:44 pm
by Pomgonewalkabout
Hi
I have an Arduino Uno connected to a Rasperry Pi via a USB cable and as I turn a rotary encoder on the Arduino the position of the encoder ( 0 to 79) is being sent to the Raspberry Pi.

As the encoder is rotated one of 80 neo pixels leds connected to the Arduino light up corresponding to the position of the encoder.

A script running on the Pi opens up a terminal window and the positions are being printed and correspond to those in the serial window on the Arduino.

Depending on the position I want a certain thing to happen in the Python script such as playing a music file.

Nothing happens in my script and the number being printed is because I am led to believe that the data being received by the Pi is a character and not an integer?

Can someone please explain why the data sent or received is not an actual number and how can I can convert the data so that the Pi can respond ?

I did read somewhere that the numerical character has to be converted into a numerical number by using -0 ?

But as a beginner I have code flying over the top of my head ?

Cheers

Re: Getting Python script to respond to Arduino

Posted: Tue Nov 21, 2017 7:58 pm
by gordon77
Can you show us your python code?

Sounds like you are getting a string, and testing for a number.

Try using int() or float() to convert them

Re: Getting Python script to respond to Arduino

Posted: Tue Nov 21, 2017 8:20 pm
by Pomgonewalkabout
Here is my basic code that prints out the encoder positions in a terminal window.

Code: Select all

import serial


ser = serial.Serial('/dev/ttyACM0', 115200)         # enable the serial port


while True:
	read_serial = ser.readline()
	print read_serial

Re: Getting Python script to respond to Arduino

Posted: Tue Nov 21, 2017 8:36 pm
by gordon77
Convert read_serial to a number with number = int(read_serial)

Re: Getting Python script to respond to Arduino

Posted: Tue Nov 21, 2017 10:04 pm
by Pomgonewalkabout
Thanks Gordon
I am making some progress but now getting intermittent errors

Number = int(read_serial)

ValueError : invalid letter for int() with base 10: '24\r24\r|n'

Cheers