May-18-2019, 06:30 PM
Hi,
I'm following the book Beginning Robotics with Raspberry Pi & Arduino.
I'm familiar with the Arduino and C++, but learning Python along with the book.
I'm at the part where I'm sending Distance Measurements from an Arduino hooked up to an HC-SR04 sensor over the USB serial connection to be displayed by the Python program.
This is the code that I have
I've tried int(float(recSer + '0') but still no joy.
I am using Python 3.5.3 on the Raspberry Pi,
Any help appreciated,
I'm following the book Beginning Robotics with Raspberry Pi & Arduino.
I'm familiar with the Arduino and C++, but learning Python along with the book.
I'm at the part where I'm sending Distance Measurements from an Arduino hooked up to an HC-SR04 sensor over the USB serial connection to be displayed by the Python program.
This is the code that I have
# Import serial and time libraries import serial import time ser = serial.Serial('/dev/ttyACM0',9600,timeout=1) while 1: recSer = ser.readline().decode('utf-8') recSer.rstrip() distance = int(recSer + '0')/10 print("Distance: " + str(distance) + "cm", end = '\r') time.sleep(0.5)Here is the error:
Error:Distance: 0.0cm
Traceback (most recent call last):
File "/home/bkmypie/Documents/Robotics-Pie-Arduino/serial_test.py", line 10, in <module>
distance = int(recSer + '0')/10
ValueError: invalid literal for int() with base 10: '342\r\n0
I have googled around the net and I understand it is to with trying to display a float as an int in a string, but I'm at a loss on how to fix it.I've tried int(float(recSer + '0') but still no joy.
I am using Python 3.5.3 on the Raspberry Pi,
Any help appreciated,