If Statement is not executing?

Muhammad Asim :

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 after an interval of 0.5ms (This Interval is coded in Arduino IDE) and it gives me "1" and "0", if Jumper wire is pulled into any Digital PIN it will give "0" else it will give "1".

Then the "0" and "1" 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 continuously and stores them in an "curdata" array and compares it with "predata" array. If "predata" and "curdata" arrays does not match it stores the all the values of "curdata" array are written into the "predata" and Displayed on GUI Screen.

All the Code is working properly except this if statement, it is not executing. Due to this the program stucks:

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:

First Output

On Pressing Start Button:

On Pressing Start Button

Kashif Iftikhar :

Try this:

from serial import Serial
import time
from tkinter import *
import tkinter as tk

#Reading Arduino
arduinodata = Serial("COM6",9600)

#Storing Arduino Data in data1 Array
data1 = arduinodata.readline()

#Creating GUI Screen    
win = Tk()
win.title("Arduino")
win.geometry("800x600+50+50")
win.config(bg='white')

#Displaying Heading on GUI Screen    
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 Datatype of data1 array from binary to string.  
firstdata=str(data1)

#Initiliazating Array to store Values of Current and Previous state of 
#Arduino Digital Pins.    
predata=[]
newdata=[]

#Storing Arduino data into arrays.
i=2
for a0 in range(10):
    predata.append(firstdata[i])
    newdata.append(firstdata[i])
    i=i+2

lblframe = tk.Frame(win)
#Displaying Digital PIN heading on GUI Screen.
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)

#mylabels function will display values of predata and newdata array on GUI 
#Screen
def mylabels():
    for a2 in range(10):
        #Displaying Data of predata array on GUI Screen
        if ( int(predata[a2]) == 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 newdata array on GUI Screen   
    for a3 in range(10):
        if (int(newdata[a3]) == 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()

#Calling mylabel() function automatically when the GUI Screen is created.  
mylabels()

#Statuschanger function to change the status of predata and newdata arrays on 
#GUI and in array. 
def statuschanger():
    #Clearing predata and newdata array to store new values in it.
    predata.clear()
    newdata.clear()
    #Again Reading the Digital PINS of Arduino Board.
    data2 = arduinodata.readline()
    #Converting data of arduino Digital PINS from binary to string.
    seconddata=str(data2)
    #Storing Arduino data into arrays.
    j=2
    for a6 in range(10):
        predata.append(seconddata[j])
        newdata.append(seconddata[j])
        j=j+2
    #Calling mylabel function to Updata Values on GUI Screen.
    mylabels()
    #Calling statuschanger function again and again after every 100msec.
    win.after(100, statuschanger)

#Button1 to run the statuschanger function.
button1=Button(win,text="Start",width=10,height=2, font=("Calibri",16,"bold"), bg="black",fg="white", command=statuschanger)
button1.pack(pady=(30,0))

win.mainloop()

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=5314&siteId=1