问题
I am learning about interfaces on Python Tkinter. I was testing before sending random numbers from my arduino to my Python interface, It worked (It updates all the time), so I thought it was going to work when sending data from a sensor, but it did not.
So, this is a test code, if I am pressing the button, I would have to send 3 variables with the number 1, and if I am not, I will get 2 variables with the number 2 and one random variable.
So, I never get the values that I should get when pressing the button. I am always getting: random number, 2 and 2.
Arduino:
void setup() {
Serial.begin (9600);
pinMode(i_presion, INPUT);
}
void loop()
{
if(digitalRead(i_presion) == HIGH )
{
Serial.print(1);
Serial.print(" ");
Serial.print(1);
Serial.print(" ");
Serial.print(1);
Serial.print(" ");
}
else
{
Serial.print(random(3,8));
Serial.print(" ");
Serial.print(2);
Serial.print(" ");
Serial.print(2);
Serial.print(" ");
}
}
Python:
import serial
import time
from Tkinter import *
root = Tk()
ser = serial.Serial("/dev/cu.usbmodem1411", 9600, timeout=1)
flagCharacter = 'k'
canvas = Canvas(root, width=1920, height=1080)
canvas.pack()
photo = PhotoImage(file= r"ANDREA-FIORI2.gif")
label=Label(root, image=photo)
photo = PhotoImage(file= r"ANDREA-FIORI2.gif")
canvas.pack(side='top', fill='both', expand='yes')
canvas.create_image(0, 0, image=photo, anchor='nw')
def sensores(planeado, producido, alertas):
canvas.create_text(390, 430, text=planeado, fill="gray", font="Helvetica 100 bold",tag="T1")
canvas.create_text(650, 430, text=producido, fill="gray", font="Helvetica 100 bold",tag="T2")
canvas.create_text(900, 430, text=alertas, fill="gray", font="Helvetica 100 bold",tag="T3")
#root.after(1000,sensores)
def borrar():
canvas.delete("T1")
canvas.delete("T2")
canvas.delete("T3")
def do_update():
ser.write(flagCharacter)
borrar()
allitems=ser.readline(6)
x, y, z = allitems.split()
sensores(x, y, z)
root.after(1000, do_update)
do_update()
root.mainloop()
Any ideas of why it is updating the function random, and not information from my sensors?
回答1:
I'm pretty sure that if you change this
pinMode(i_presion, INPUT);
Into this
pinMode(i_presion, INPUT_PULLUP);
It will start working....
The reason is that you are not pulling up the pin when the button is not pressed.
Or, at least, this is the most common reason for the...
I never get the values that I should get when pressing the button. I am always getting: random number, 2 and 2
...problem.
来源:https://stackoverflow.com/questions/33471037/cannot-update-sensor-data-in-python-tkinter-from-my-arduino-sensors