Pomgonewalkabout
Posts: 18
Joined: Tue Jun 21, 2016 5:33 am

Getting Python script to respond to Arduino

Tue Nov 21, 2017 7:44 pm

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

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: Getting Python script to respond to Arduino

Tue Nov 21, 2017 7:58 pm

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

Pomgonewalkabout
Posts: 18
Joined: Tue Jun 21, 2016 5:33 am

Re: Getting Python script to respond to Arduino

Tue Nov 21, 2017 8:20 pm

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

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: Getting Python script to respond to Arduino

Tue Nov 21, 2017 8:36 pm

Convert read_serial to a number with number = int(read_serial)

Pomgonewalkabout
Posts: 18
Joined: Tue Jun 21, 2016 5:33 am

Re: Getting Python script to respond to Arduino

Tue Nov 21, 2017 10:04 pm

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

Return to “Beginners”