I have a Program that reads Digital PINS of Arduino UNO. I have turned on all the Digital PINS by default by using "PULLUP" keyword. Now if I insert "Jumper Wire" into any Digital PIN it is PULLDOWN(Keyword). I am reading the states of Digital PINS continuously and it gives me "1" and "0", if Jumper wire is pulled in it will give "0" else it will give "1".
Then these values are stored in two arrays namely "predata for maintaining previous state, newdata for maintaining current state" of the Digital PINS and Displays the states on GUI Screen. On Running Arduino UNO first time the "newdata" and "predata" are Same. Then i have an Infinite While loop that reads Digital PINS every 0.5ms and stores them in an "curdata" array and then compares it with "predata" array. If "predata" and "curdata" arrays does not match it stores the all the values of "curdata" array into the "predata" and display the state on GUI Screen. All the Code is working properly except this:
if (curdata[a7] != predata[a7]):
predata[a7]=curdata[a7]
oldstatus()
newstatus()
*Complete Code:
from serial import Serial
import time
from tkinter import *
import tkinter as tk
#Making Connection with Arduino
arduinodata = Serial("COM6",9600)
#Reading Data from Arduino
data1 = arduinodata.readline()
#Configuring GUI Screen
win = Tk()
win.title("Arduino")
win.geometry("800x600+50+50")
win.config(bg='white')
#Heading Label
label1=Label(win, text="Digital PIN Status", font=("Calibri",24,"bold"), bg='white', borderwidth=1, relief="solid", padx=20, pady=20) #"flat", "raised", "sunken", "ridge", "solid", and "groove"
label1.pack(pady=(15,60))
#Converting Digital PINS data into string and storing them into firstdata array
firstdata=str(data1)
#Inintailizating Arrays
predata=[]
newdata=[]
curdata=[]
#Storing First state of first ten Digital PIINS into the "predata" and "curdata" array.
i=2
for a0 in range(10):
predata.append(firstdata[i])
newdata.append(firstdata[i])
i=i+2
#Displaying Digital PIN Number on GUI Screen
lblframe = tk.Frame(win)
for a1 in range(10):
pre1=Label(lblframe, text=("PIN",(a1+2)), font=("Calibri",12, "bold"), bg="white", borderwidth=1, relief="solid", padx=5, pady=2)
pre1.grid(row=0, column=a1)
#Displaying data of "predata" array on GUI Screen
for a2 in range(10):
binary1 = predata[a2]
if ( binary1 == "1" ):
pre2=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
pre2.grid(row=1, column=a2, sticky="nw")
else:
pre2=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
pre2.grid(row=1, column=a2, sticky="nw")
#Displaying data of "curdata" on GUI Screen.
for a3 in range(10):
binary2 = newdata[a3]
if (binary2 == "1"):
pre3=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
pre3.grid(row=2, column=a3, sticky="nw")
else:
pre3=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
pre3.grid(row=2, column=a3, sticky="nw")
lblframe.pack()
#This Function is to change value of predata on GUI Screen
def oldstatus():
for a4 in range(10):
binary=predata[a4]
if(binary=="1"):
pre2.config(text="OFF")
else:
pre2.config(text="ON")
#This Function is to change value of curdata on GUI Screen
def newstatus():
for a5 in range(10):
binary=curdata[a5]
if(binary=="1"):
pre3.config(text="OFF")
else:
pre3.config(text="ON")
#This Function is to read Digital PINS continuously and if found Change then update predata and
#curdata array and it run only when button is pressed.
def allstatus():
while True:
data2 = arduinodata.readline()
seconddata=str(data2)
j=2
for a6 in range(10):
curdata.append(seconddata[j])
j=j+2
for a7 in range(10):
if (curdata[a7] != "1"):
predata[a7]=curdata[a7]
oldstatus()
newstatus()
#It is to run allstatus function.
button1=Button(win,text="Start",width=10,height=2, font=("Calibri",16,"bold"), bg="black",fg="white", command=allstatus)
button1.pack(pady=(30,0))
win.mainloop()
Output:
First Output:
On Pressing Start Button:


Aucun commentaire:
Enregistrer un commentaire