Question:
I am trying to build an object counter with a raspberry pi 4, getting the data from an optical sensor connected to an arduino board. I run this python script to read the sensors and tell it works in the terminal.
import pyfirmata
placa = pyfirmata.Arduino('/dev/ttyACM0')
pyfirmata.util.Iterator(placa).start()
pin2 = placa.get_pin('d:2:i')
pin3 = placa.get_pin('d:3:i')
pin4 = placa.get_pin('d:4:i')
pin5 = placa.get_pin('d:5:i')
pin6 = placa.get_pin('d:6:i')
pin7 = placa.get_pin('d:7:i')
pin2.enable_reporting()
pin3.enable_reporting()
pin4.enable_reporting()
pin5.enable_reporting()
pin6.enable_reporting()
pin7.enable_reporting()
try:
while True:
contador6 = 0
contador2 = 0
estadoant2 = 0
estadoant6 =0
while True:
if pin2.read() != estadoant2: #lee el estado anterior y si es distinto continua
if pin2.read() ==1:
contador2 +=1
print ("Piezas procesadas c2: ", contador2)
estadoant2 = pin2.read()
placa.pass_time(0.09)
if pin6.read() != estadoant6: #lee el estado anterior y si es distinto continua
if pin6.read() ==1:
contador6 +=1
print ("Piezas procesadas c6: ", contador6)
estadoant6 = pin6.read()
placa.pass_time(0.09)
except KeyboardInterrupt:
# Terminar programa cuando se presione Ctrl-C.
pass
finally:
placa.exit()
so far everything works ok. I try to take it further to have a graphical output but I'm sure I'm doing something very wrong. Well, I can't get the part of the code that counts to run. If anyone has any ideas or another point of view I would greatly appreciate it.
This is the code with which I am trying to graphic output
from tkinter import *
import time
import pyfirmata
placa = pyfirmata.Arduino('/dev/ttyACM0')
pyfirmata.util.Iterator(placa).start()
pin2 = placa.get_pin('d:2:i')
pin3 = placa.get_pin('d:3:i')
pin4 = placa.get_pin('d:4:i')
pin5 = placa.get_pin('d:5:i')
pin6 = placa.get_pin('d:6:i')
pin7 = placa.get_pin('d:7:i')
pin2.enable_reporting()
pin3.enable_reporting()
pin4.enable_reporting()
pin5.enable_reporting()
pin6.enable_reporting()
pin7.enable_reporting()
#FUNCION PARA ACTUALIZAR LA HORA
def times():
current_time=time.strftime("%H:%M:%S")
clock.config(text=current_time,bg="black",fg="green",font="Arial 20 bold")
clock.after(200,times)
global estadoant2
estadoant6 = 0
contador6 = 0
global contador2
def conta():
estadoant2 = 0
contador2 = 0
while True:
if pin2.read() != estadoant2: #lee el estado anterior y si es distinto continua
if pin2.read() ==1:
contador2 +=1
#print ("Piezas procesadas c2: ", contador2)
estadoant2 = pin2.read()
placa.pass_time(0.09)
cont.config(text=contador2,bg="black",fg="green",font="Arial 20 bold")
cont.after(200,times)
#VENTANA
root=Tk()
root.geometry("485x250")
root.title("Contador Cepilladora Weinig")
clock=Label(root,font=("times",24,"bold"))
clock.grid(row=2,column=1,pady=25,padx=100)
times()
digi=Label(root,text=" Hora Actual",font="times 12 bold",fg="red")
digi.grid(row=0,column=1)
digi2=Label(root,text=" Piezas Procesadas",font="times 12 bold",fg="red")
digi2.grid(row=0,column=2)
cont=Label(root,font=("times",24,"bold"))
cont.grid(row=2,column=2,pady=25,padx=100)
conta()
root.mainloop()
Thanks
Answer:
I have achieved a breakthrough including the full exception. The Problem now is that I have to interrupt the counter loop to see the result in the graphics window. If I don't interrupt it (ctrl+c in the terminal) the window is not shown.
The code goes like this.
import time
import pyfirmata
placa = pyfirmata.Arduino('/dev/ttyACM0')
pyfirmata.util.Iterator(placa).start()
pin2 = placa.get_pin('d:2:i')
pin3 = placa.get_pin('d:3:i')
pin4 = placa.get_pin('d:4:i')
pin5 = placa.get_pin('d:5:i')
pin6 = placa.get_pin('d:6:i')
pin7 = placa.get_pin('d:7:i')
pin2.enable_reporting()
pin3.enable_reporting()
pin4.enable_reporting()
pin5.enable_reporting()
pin6.enable_reporting()
pin7.enable_reporting()
#FUNCION PARA ACTUALIZAR LA HORA
def times():
current_time=time.strftime("%H:%M:%S")
clock.config(text=current_time,bg="black",fg="green",font="Arial 20 bold")
clock.after(200,times)
global estadoant2
global estadoant6
global contador6
global contador2
def conta():
try:
while True:
contador6 = 0
contador2 = 0
estadoant2 = 0
estadoant6 =0
while True:
if pin2.read() != estadoant2: #lee el estado anterior y si es distinto continua
if pin2.read() ==1:
contador2 +=1
# print ("Piezas procesadas c2: ", contador2)
estadoant2 = pin2.read()
placa.pass_time(0.09)
#if pin6.read() != estadoant6: #lee el estado anterior y si es distinto continua
# if pin6.read() ==1:
# contador6 +=1
#print ("Piezas procesadas c6: ", contador6)
#estadoant6 = pin6.read()
#placa.pass_time(0.09)
except:
# Terminar programa cuando se presione Ctrl-C.
pass
finally:
placa.exit()
cont.config(text=contador2,bg="black",fg="green",font="Arial 20 bold")
cont.after(200,times)
#VENTANA
root=Tk()
root.geometry("485x250")
root.title("Contador Cepilladora Weinig")
clock=Label(root,font=("times",24,"bold"))
clock.grid(row=2,column=1,pady=25,padx=100)
times()
digi=Label(root,text=" Hora Actual",font="times 12 bold",fg="red")
digi.grid(row=0,column=1)
digi2=Label(root,text=" Piezas Procesadas",font="times 12 bold",fg="red")
digi2.grid(row=0,column=2)
cont=Label(root,font=("times",24,"bold"))
cont.grid(row=2,column=2,pady=25,padx=100)
conta()
root.mainloop()