I'm doing a python tutorial with arduino, with an exercise that tries to record the data of two sensors: a motion sensor (digital input), and a potentiometer sensor (analog input).
The problem I have is that although when I run the program it creates the .csv file and the table headers (these being defined in the code), but it does not record the data collected by the sensors.
I show the code that I am using:
import csv
import pyfirmata
from time import sleep
port = '/dev/ttyACM0'
board = pyfirmata.Arduino(port)
it = pyfirmata.util.Iterator(board)
it.start()
pirPin = board.get_pin('d:11:i')
a0 = board.get_pin('a:1:i')
with open('SensorDataStore.csv', 'w') as f:
w = csv.writer(f)
w.writerow(["Number", "Potentiometer", "Motion sensor"])
i = 1
pirData = pirPin.read()
potData = a0.read()
while i < 25 :
sleep(1)
if pirData is not None:
i += 1
print (i)
row = [i, potData, pirData]
w.writerow(row)
print "Done. CSV file is ready!"
board.exit()
The code performs the loop 25 times but does not capture any information from the sensors.
The operating system I use is a Ubuntu Studio 16.04. The file / dev / ttyACM0 has all the permissions.
I have not found anything on the web that can help me or my knowledge in programming are enough to find an alternative.