无法从我的arduino传感器

时间:2015-11-02 04:58:02

标签: python user-interface tkinter arduino

我正在学习Python Tkinter的接口。我在从我的arduino到我的Python界面发送随机数之前进行了测试,它工作(它一直在更新),所以我认为从传感器发送数据时它会起作用,但事实并非如此。

所以,这是一个测试代码,如果我按下按钮,我将不得不发送3个数字1的变量,如果不是,我将得到2个变量,数字2和一个随机变量。

所以,我从来没有得到按下按钮时应该得到的值。我总是得到:随机数,2和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()

为什么它会更新函数随机而不是来自我的传感器的信息?

1 个答案:

答案 0 :(得分:0)

我很确定如果你改变这个

pinMode(i_presion, INPUT);

进入这个

pinMode(i_presion, INPUT_PULLUP);

它将开始工作....

原因是没有按下按钮时你没有拔出针脚。

或者,至少,这是...的最常见原因。

  

我从来没有得到按下按钮时应该得到的值。我总是得到:随机数,2和2

...问题。