gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Wed Aug 14, 2019 9:56 am

I think this meets the list of requirements you posted, not sure where the 3rd variable is, and what the EXIT 0 comments mean.
I am not sure if it does what you expect.

Using pygame...

Code: Select all

import RPi.GPIO as GPIO
import pygame, sys
import time
from pygame.locals import *
from random import *
import os

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
switch1 = 11 # pin 11, switch to +3.3v
switch2 = 13 # pin 13, switch to +3.3v
switch3 = 15 # pin 15, switch to +3.3v
GPIO.setup(switch1,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(switch2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(switch3,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

speed = 0
datafile = "/home/pi/data.txt"

def update_display(msg,width,height):
   redColor = pygame.Color(255,0,0)
   greyColor = pygame.Color(0,0,0)
   pygame.draw.rect(windowSurfaceObj,greyColor,Rect(0,0,width,height))
   msgSurfaceObj = fontObj.render(msg, False,redColor)
   msgRectobj = msgSurfaceObj.get_rect()
   x = (width/2) - ((len(msg)/2)*(font_size/2))
   msgRectobj.topleft =(x,0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
   windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
   pygame.display.update()
   
pygame.init()
font_size = 250
width = font_size * 2
height = font_size
windowSurfaceObj = pygame.display.set_mode((width,height),pygame.FULLSCREEN,24)
pygame.display.set_caption('SPEED')
fontObj = pygame.font.Font('freesansbold.ttf',font_size)
msg = str(speed)
update_display(msg,width,height)

running = 0

while True :
   # check button 1
   if GPIO.input(switch1) == 1:
      if running == 1: # STOP and EXIT
         pygame.quit()
         sys.exit()
         
      elif running == 0:
         running = 1
         start1 = time.time()
         start10 = start1
         time.sleep(0.25) # debounce
      
      # delete old data file
      if os.path.exists(datafile) == True:
         os.remove(datafile) 
      speed = 55
      tcounter = 0
      fcounter = 0
      oldtcounter = 0
      msg = str(speed)
      update_display(msg,width,height)

   if running == 1:   
      now = time.time() - start1
   
      # every 100mS
      if now >= 0.1:
         start1 = time.time()
         tcounter +=1
         fcounter +=1
      
      # every 10 tcounters
      if fcounter == 10:
         fcounter = 0
         r = randint(0,100)
         if r > 66:
            speed +=1
            msg = str(speed)
            update_display(msg,width,height)

         elif r > 32 and r < 67:
            speed -=1
            msg = str(speed)
            update_display(msg,width,height)
         
      # check button 2      
      if GPIO.input(switch2) == 1 and oldtcounter != tcounter:
         with open(datafile, 'a') as f:
             f.write("tcounter: " + str(tcounter) + "  speed: " + str(speed) + "\n")
         oldtcounter = tcounter

      # check button 3        
      if GPIO.input(switch3) == 1 and oldtcounter != tcounter:
         with open(datafile, 'a') as f:
             f.write("tcounter: " + str(tcounter) + "\n" )
         oldtcounter = tcounter
   # EXIT
   for event in pygame.event.get():
      if event.type == QUIT or event.type == KEYDOWN or event.type == MOUSEBUTTONDOWN:
         pygame.quit()
         sys.exit()
or using tkinter...

Code: Select all

#!/usr/bin/env python3

from tkinter import *
import tkinter as tk
from tkinter import font
import time
import RPi.GPIO as GPIO
from random import *
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
switch1 = 17 # pin 11, switch to +3.3v
switch2 = 27 # pin 13, switch to +3.3v
switch3 = 22 # pin 15, switch to +3.3v
GPIO.setup(switch1,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(switch2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(switch3,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# set variables
running = 0
datafile = "/home/pi/data.txt"
speed = 0
tcounter = 0
fcounter = 0
oldtcounter = 0
        
def quit(*args):
    root.destroy()
	
def update_counter():
    global tcounter,fcounter,speed, running
    if running == 1:
        tcounter +=1
        fcounter +=1
        if fcounter == 10:
            fcounter = 0
            r = randint(0,100)
            if r > 66:
                speed +=1
            elif r > 32 and r < 67:
                speed -=1
            
    text.set(str(speed))
    root.after(100, update_counter)
        
def check_switches():
    global switch1, start, running, speed, oldtcounter
    
    # check button 1
    if GPIO.input(switch1) == 1 and running == 0:
        running = 1
        # delete old data file
        if os.path.exists(datafile) == True:
           os.remove(datafile)
        speed = 55
        root.after(100, update_counter)
    elif GPIO.input(switch1) == 1 and running == 1:
        running = 2
        root.after(100, quit)
        
    # check button 2      
    if GPIO.input(switch2) == 1 and oldtcounter != tcounter:
       with open(datafile, 'a') as f:
           f.write("tcounter: " + str(tcounter) + "  speed: " + str(speed) + "\n")
       oldtcounter = tcounter

    # check button 3        
    if GPIO.input(switch3) == 1 and oldtcounter != tcounter:
       with open(datafile, 'a') as f:
           f.write("tcounter: " + str(tcounter) + "\n" )
       oldtcounter = tcounter
         
    root.after(200, check_switches)

root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.after(100, check_switches)

fnt = font.Font(family='Helvetica', size=350, weight='bold')
text = StringVar()
text.set(str(speed))
label = tk.Label(root, textvariable=text, font=fnt, foreground="red", background="black")
label.place(relx=0.5, rely=0.5, anchor=CENTER)

root.mainloop()
Attachments
55.jpg
55.jpg (13.82 KiB) Viewed 1071 times

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Aug 15, 2019 7:33 pm

hi gordon,nielgl,
thanks a lot for all u r help. I live in US and EXIT0 is part of the branch. in text i basically wrote the flow chart of the program.
hare P

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Oct 10, 2019 9:06 pm

hi gordon & niegel need your help, need 2 different font size display 1" and 4"
sw0 on/off switch
sw1 toggle on/off switch
sw2 pushbutton switch value 0 or 1

sw0 on
speed=55
start 100 msec timer interrupt
open a file on sd card data.txt
main:
display two character speed value on screen ( 1 inch high size / 4 inch high size)
timer interrupt (100msec)
store timer value, speed value, sw2 value example 0.3 55 00 or 0.9 56 01
every 1 sec start a random generator (00,99) VALUE if value >66 increase speed value by 1
if value <66 >33 decrease speed value by 1
other wise no change
sw1 toggle up increase speed value by 1
toggle down decrease speed value by 1

continue main loop till sw0 is off
end main
close file and save to sd card

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Fri Oct 11, 2019 7:48 am

try this... I don't know what size your monitor is for 1" or 4" characters

Code: Select all

#!/usr/bin/env python3

from tkinter import *
import tkinter as tk
from tkinter import font
import time
import RPi.GPIO as GPIO
from random import *
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
sw0 = 17 # pin 11, switch to +3.3v
sw1 = 27 # pin 13, switch to +3.3v
sw2 = 22 # pin 15, switch to +3.3v
GPIO.setup(sw0,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# set variables
font_size = 400 # try 100 for 1" or 400 for 4" ( on a 23" monitor)
datafile = "/home/pi/data.txt"
speed = 0
timer = 0
fcounter = 0
running = 0
        
def quit(*args):
    root.destroy()
	
def update_counter():
    global fcounter, speed, timer, running
    if running == 1:
        fcounter +=1
        timer +=1
        with open(datafile, 'a') as f:
           f.write(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) + "\n")
           
        if GPIO.input(sw1) == 1:
            speed +=1
        else:
            speed -=1

        if fcounter == 10: # every second
            fcounter = 0
            r = randint(0,100)
            if r > 66:
                speed +=1
            elif r > 33 and r < 67:
                speed -=1
    text.set(str(speed))
    root.after(100, update_counter)
        
def check_sw0():
    global running, speed
    if GPIO.input(sw0) == 1 and running == 0:
        running = 1
        # delete old data file
        if os.path.exists(datafile) == True:
           os.remove(datafile)
        speed = 55
        root.after(100, update_counter)
        
    elif GPIO.input(sw0) == 0 and running == 1:
        running = 2
        root.after(100, quit)
    root.after(200, check_sw0)

root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.after(100, check_sw0)

fnt = font.Font(family = 'Helvetica', size = font_size, weight = 'bold')
text = StringVar()
text.set(str(speed))
label = tk.Label(root, textvariable=text, font=fnt, foreground="red", background="black")
label.place(relx=0.5, rely=0.5, anchor=CENTER)

root.mainloop()


hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Fri Oct 11, 2019 9:57 pm

Thank you gordon for u r prompt response. I have a 19" monitor. The switch 1 is a toggle switch meaning it is Up connects to pin13 and connects to pin 15 when down. Normally it is pulled down to gnd. In up mode 3.3v connects to pin 13 . In down mode 3.3v connects to pin 15.
SW2 can connect to gpio 23 on pin 16
please let me know if it is ok
hare

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Sat Oct 12, 2019 2:22 pm

try this...

Code: Select all

#!/usr/bin/env python3

from tkinter import *
import tkinter as tk
from tkinter import font
import time
import RPi.GPIO as GPIO
from random import *
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
sw0  = 17 # pin 11, switch to +3.3v
sw1a = 27 # pin 13, switch to +3.3v
sw1b = 22 # pin 15, switch to +3.3v
sw2  = 23 # pin 16, switch to +3.3v
GPIO.setup(sw0,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1a,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1b,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# set variables
font_size = 400 # try 100 for 1" or 400 for 4"
datafile = "/home/pi/data.txt"
speed = 0
timer = 0
fcounter = 0
running = 0
        
def quit(*args):
    root.destroy()
	
def update_counter():
    global fcounter, speed, timer, running
    if running == 1:
        fcounter +=1
        timer +=1
        with open(datafile, 'a') as f:
           f.write(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) + "\n")
           
        if GPIO.input(sw1a) == 1:
            speed +=1
        if GPIO.input(sw1b) == 1:
            speed -=1

        if fcounter == 10: # every second
            fcounter = 0
            r = randint(0,100)
            if r > 66:
                speed +=1
            elif r > 33 and r < 67:
                speed -=1
    text.set(str(speed))
    root.after(100, update_counter)
        
def check_sw0():
    global running, speed
    if GPIO.input(sw0) == 1 and running == 0:
        running = 1
        # delete old data file
        if os.path.exists(datafile) == True:
           os.remove(datafile)
        speed = 55
        root.after(100, update_counter)
        
    elif GPIO.input(sw0) == 0 and running == 1:
        running = 2
        root.after(100, quit)
    root.after(200, check_sw0)

root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.after(100, check_sw0)

fnt = font.Font(family = 'Helvetica', size = font_size, weight = 'bold')
text = StringVar()
text.set(str(speed))
label = tk.Label(root, textvariable=text, font=fnt, foreground="red", background="black")
label.place(relx=0.5, rely=0.5, anchor=CENTER)

root.mainloop()

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Sat Oct 12, 2019 4:05 pm

thanks Gordon again I am getting the hardware parts will test soon and let u know the result
sinerely
hare

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Tue Oct 15, 2019 3:48 pm

hi gordon & niegel
THanks for all your help. I have some requests
1. I have 2 displays on RP4 a. embedded 5" display where it boots
2. a 19" hdmi display where i like speed data to be displayed so how do i add this display and i would also like to display the timercounter, speed, sw2 position data before we write to sd card for debug purpose
3. do i run the program from terminal or the python editor??/
thanks for u r response
Hare Patnaik

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Wed Oct 16, 2019 7:33 am

1. Assume the 5" is driven by gpio ? which display, which software ?
2. is the 19" in HDMI 0 or 1 ?
3. Either run from your python editor or 'python3 filename.py' from terminal

I don't know the answer to driving the 2 screens yet.

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Oct 17, 2019 4:29 pm

Hi Gordon,
The 19" monitor is on "0" and only for display of the speed (for ex: 55 1" or 4" size) rest the boot and program run debug is on the embedded 5" display
Just for debug i like to display the timer counter, speed, sw2 value display on the HDMI monitor before we write to SD card file.
Thanks
Hare Patnaik

User avatar
neilgl
Posts: 1191
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Oct 17, 2019 10:47 pm

Have you got the parts now?

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Fri Oct 18, 2019 7:43 am

hkpatnaik wrote:
Thu Oct 17, 2019 4:29 pm
Hi Gordon,
The 19" monitor is on "0" and only for display of the speed (for ex: 55 1" or 4" size) rest the boot and program run debug is on the embedded 5" display
Just for debug i like to display the timer counter, speed, sw2 value display on the HDMI monitor before we write to SD card file.
Thanks
Hare Patnaik
This should achieve the display...

If you find the timings are running a bit slow try reducing the 100 in this line root.after(100, update_counter)

I am not sure how to drive your 2 monitors. This shows how to drive 2 HDMI monitors https://thepihut.com/blogs/raspberry-pi ... berry-pi-4 but one of yours is embedded.

Code: Select all

#!/usr/bin/env python3

from tkinter import *
import tkinter as tk
from tkinter import font
import time
import RPi.GPIO as GPIO
from random import *
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
sw0  = 17 # pin 11, switch to +3.3v
sw1a = 27 # pin 13, switch to +3.3v
sw1b = 22 # pin 15, switch to +3.3v
sw2  = 23 # pin 16, switch to +3.3v
GPIO.setup(sw0,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1a,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1b,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# set variables
font_size = 400 # try 100 for 1" or 400 for 4"
font_size2 = 50
datafile = "/home/pi/data.txt"
speed = 0
timer = 0
fcounter = 0
running = 0
        
def quit(*args):
    root.destroy()
	
def update_counter():
    global fcounter, speed, timer, running
    if running == 1:
        fcounter +=1
        timer +=1
          
        if GPIO.input(sw1a) == 1:
            speed +=1
        if GPIO.input(sw1b) == 1:
            speed -=1

        if fcounter == 10: # every second
            fcounter = 0
            r = randint(0,100)
            if r > 66:
                speed +=1
            elif r > 33 and r < 67:
                speed -=1
        text2.set(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) )
        with open(datafile, 'a') as f:
           f.write(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) + "\n")
    text.set(str(speed))
    root.after(100, update_counter)
        
def check_sw0():
    global running, speed
    if GPIO.input(sw0) == 1 and running == 0:
        running = 1
        # delete old data file
        if os.path.exists(datafile) == True:
           os.remove(datafile)
        speed = 55
        root.after(100, update_counter)
        
    elif GPIO.input(sw0) == 0 and running == 1:
        running = 2
        root.after(100, quit)
    root.after(200, check_sw0)

root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.after(100, check_sw0)

fnt = font.Font(family = 'Helvetica', size = font_size, weight = 'bold')
fnt2 = font.Font(family = 'Helvetica', size = font_size2, weight = 'bold')
text = StringVar()
text2 = StringVar()
text.set(str(speed))
label = tk.Label(root, textvariable=text, font=fnt, foreground="red", background="black")
label2 = tk.Label(root, textvariable=text2, font=fnt2, foreground="blue", background="black")
label.place(relx=0.5, rely=0.5, anchor=CENTER)
label2.place(relx=0.1, rely=0.9, anchor=CENTER)

root.mainloop()

Attachments
screen.jpg
screen.jpg (14.67 KiB) Viewed 711 times
Last edited by gordon77 on Fri Oct 25, 2019 7:15 am, edited 1 time in total.

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Oct 24, 2019 11:32 pm

Hi gordon
parts are ordered but have not come in it. where in program u set the timer interrupt for 100 msec and where is it serviced
Thanks
Hare

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Fri Oct 25, 2019 7:14 am

It's set here...

root.after(100, update_counter)

it runs def update_counter(): every 100mS

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Nov 07, 2019 7:03 pm

Hi Gordon,
I got all the parts and I am now wiring them. Could u please tell me how do i debug the program. can i try to display all the parameters before i write to file. how to put break point etc.
Thanks
Hare

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Nov 07, 2019 7:23 pm

hkpatnaik wrote:
Thu Nov 07, 2019 7:03 pm
Hi Gordon,
I got all the parts and I am now wiring them. Could u please tell me how do i debug the program. can i try to display all the parameters before i write to file. how to put break point etc.
Thanks
Hare
I am not sure what you need to debug.

To see extra parameters add them to
text2.set(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) )
And you will see them on the screen.

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Nov 21, 2019 11:00 pm

hi gordon,
I need help in debugging . Most of the time i get error when i escape is that sw0 is not high or on. even though i checked with the meter it shows on. How do i put break point to make sure sw0 is on and also switch 1-1 and 1-2 is on and also sw2. I have seen it working some time. unless i put break point i would not be able to debug
sincerely
Hare

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Fri Nov 22, 2019 8:23 am

Can you explain more what you mean by "Most of the time i get error when i escape is that sw0 is not high or on"

Run this version to see whats happening...

Code: Select all

#!/usr/bin/env python3

from tkinter import *
import tkinter as tk
from tkinter import font
import time
import RPi.GPIO as GPIO
from random import *
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
sw0  = 17 # pin 11, switch to +3.3v
sw1a = 27 # pin 13, switch to +3.3v
sw1b = 22 # pin 15, switch to +3.3v
sw2  = 23 # pin 16, switch to +3.3v
GPIO.setup(sw0,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1a,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1b,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# set variables
font_size = 100 # try 100 for 1" or 400 for 4"
font_size2 = 20
datafile = "/home/pi/data.txt"
speed = 0
timer = 0
fcounter = 0
running = 0
        
def quit(*args):
    root.destroy()
	
def update_counter():
    global fcounter, speed, timer, running
    if running == 1:
        fcounter +=1
        timer +=1
          
        if GPIO.input(sw1a) == 1:
            speed +=1
        if GPIO.input(sw1b) == 1:
            speed -=1

        if fcounter == 10: # every second
            fcounter = 0
            r = randint(0,100)
            if r > 66:
                speed +=1
            elif r > 33 and r < 67:
                speed -=1
        text2.set(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) )
        with open(datafile, 'a') as f:
           f.write(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) + "\n")
    text.set(str(speed))
    root.after(100, update_counter)
        
def check_sw0():
    global running, speed
    if GPIO.input(sw0) == 1 and running == 0:
        running = 1
        # delete old data file
        if os.path.exists(datafile) == True:
           os.remove(datafile)
        speed = 55
        root.after(100, update_counter)
        
    elif GPIO.input(sw0) == 0 and running == 1:
        running = 2
        root.after(100, quit)
    root.after(200, check_sw0)

root = Tk()
root.geometry("500x500") 
#root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.after(100, check_sw0)

fnt = font.Font(family = 'Helvetica', size = font_size, weight = 'bold')
fnt2 = font.Font(family = 'Helvetica', size = font_size2, weight = 'bold')
text = StringVar()
text2 = StringVar()
text.set(str(speed))
label = tk.Label(root, textvariable=text, font=fnt, foreground="red", background="black")
label2 = tk.Label(root, textvariable=text2, font=fnt2, foreground="blue", background="black")
label.place(relx=0.5, rely=0.5, anchor=CENTER)
label2.place(relx=0.1, rely=0.9, anchor=CENTER)

root.mainloop()

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Tue Nov 26, 2019 5:12 pm

hi gordon,
i fixed the sw0 so it works good but it gets stuck in lin 40 and does not go to the random number generator
sw1-1 and sw1-2 has some problem
hare

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Tue Nov 26, 2019 5:34 pm

Can you upload a photo to show me the switch wiring to the Pi.

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Wed Nov 27, 2019 9:12 am

This version will show you the state of the switches. O when not pressed, 1 when pressed...

If not then the code won't work.

Code: Select all

#!/usr/bin/env python3

from tkinter import *
import tkinter as tk
from tkinter import font
import time
import RPi.GPIO as GPIO
from random import *
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
sw0  = 17 # pin 11, switch to +3.3v
sw1a = 27 # pin 13, switch to +3.3v
sw1b = 22 # pin 15, switch to +3.3v
sw2  = 23 # pin 16, switch to +3.3v
GPIO.setup(sw0,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1a,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1b,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# set variables
font_size = 100 # try 100 for 1" or 400 for 4"
font_size2 = 20
datafile = "/home/pi/data.txt"
speed = 0
timer = 0
fcounter = 0
running = 0
        
def quit(*args):
    root.destroy()
	
def update_counter():
    global fcounter, speed, timer, running
    if running == 1:
        fcounter +=1
        timer +=1
          
        if GPIO.input(sw1a) == 1:
            speed +=1
        if GPIO.input(sw1b) == 1:
            speed -=1

        if fcounter == 10: # every second
            fcounter = 0
            r = randint(0,100)
            if r > 66:
                speed +=1
            elif r > 33 and r < 67:
                speed -=1
        text2.set(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) )
        with open(datafile, 'a') as f:
           f.write(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) + "\n")
    text.set(str(speed))
    root.after(99, update_counter)
        
def check_sw0():
    global running, speed
    if GPIO.input(sw0) == 1 and running == 0:
        running = 1
        # delete old data file
        if os.path.exists(datafile) == True:
           os.remove(datafile)
        speed = 55
        root.after(100, update_counter)
        
    elif GPIO.input(sw0) == 0 and running == 1:
        running = 2
        root.after(100, quit)
    else:
        text3.set("SW0: " + str(GPIO.input(sw0)) + " " + "SW1a: " + str(GPIO.input(sw1a)) + " " + "SW1b: " + str(GPIO.input(sw1b)) + " ")
        
    root.after(200, check_sw0)

root = Tk()
root.geometry("500x500")
#root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.after(100, check_sw0)

fnt = font.Font(family = 'Helvetica', size = font_size, weight = 'bold')
fnt2 = font.Font(family = 'Helvetica', size = font_size2, weight = 'bold')
text = StringVar()
text2 = StringVar()
text3 = StringVar()
text.set(str(speed))
label = tk.Label(root, textvariable=text, font=fnt, foreground="red", background="black")
label2 = tk.Label(root, textvariable=text2, font=fnt2, foreground="blue", background="black")
label.place(relx=0.5, rely=0.5, anchor=CENTER)
label2.place(relx=0.5, rely=0.7, anchor=CENTER)
label3 = tk.Label(root, textvariable=text3, font=fnt2, foreground="green", background="black")
label3.place(relx=0.5, rely=0.9, anchor=CENTER)

root.mainloop()
Attachments
551.jpg
551.jpg (21.19 KiB) Viewed 196 times

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Wed Dec 04, 2019 10:43 pm

thanks Gordon last program worked perfect. Only one final question i have is the switch input call GPIO.input( sw?) is the result after the switch bounce or i have to write the switch bounce software. The up , down switch seems to get multiple increment or decrements
Thanks
Hare

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Dec 05, 2019 8:35 am

The up/down switch is simply read every 100mS, so if it is held it will keep incrementing / decrementing every 100mS.

We would have to change the code if you want anything else.

hkpatnaik
Posts: 24
Joined: Thu Aug 01, 2019 4:14 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Dec 05, 2019 9:52 pm

Hi gordon,
The up / down swich seems to have multiple counts otherwise every thing is perfect and i thanks u sincerely with all humility for helping me with the project. like always one last request how can i make the program auto start on power up. Like in pc s they have have a bat file is there some thing in python which can auto start on power up or run condition.
my sincere thanks to Gordon and Nigele for u r help

regards
hare

gordon77
Posts: 4309
Joined: Sun Aug 05, 2012 3:12 pm

Re: 100 msec timer interrupt, input switch interrupt , display 2 numbers on a hdmi screen

Thu Dec 05, 2019 10:08 pm

You get multiple counts because it's reading the switch at 10 times per second.

This version should improve it...

Code: Select all

#!/usr/bin/env python3

from tkinter import *
import tkinter as tk
from tkinter import font
import time
import RPi.GPIO as GPIO
from random import *
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
sw0  = 17 # pin 11, switch to +3.3v
sw1a = 27 # pin 13, switch to +3.3v
sw1b = 22 # pin 15, switch to +3.3v
sw2  = 23 # pin 16, switch to +3.3v
GPIO.setup(sw0,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1a,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw1b,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# set variables
font_size = 100 # try 100 for 1" or 400 for 4"
font_size2 = 20
datafile = "/home/pi/data.txt"
speed = 0
timer = 0
fcounter = 0
running = 0
s1 = 0
s2 = 0
        
def quit(*args):
    root.destroy()
	
def update_counter():
    global fcounter, speed, timer, running, s1,s2,start1,start2
    if running == 1:
        fcounter +=1
        timer +=1
          
        if GPIO.input(sw1a) == 1 and s1 == 0:
            speed +=1
            start1 = time.time()
            s1 = 1
        if GPIO.input(sw1b) == 1 and s2 == 0:
            speed -=1
            start2 = time.time()
            s2 = 1

        if s1 == 1 and time.time() - start1 > 0.5:
            s1 = 0

        if s2 == 1 and time.time() - start2 > 0.5:
            s2 = 0

        if fcounter == 10: # every second
            fcounter = 0
            r = randint(0,100)
            if r > 66:
                speed +=1
            elif r > 33 and r < 67:
                speed -=1
        text2.set(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) )
        with open(datafile, 'a') as f:
           f.write(str(timer/10) + " " + str(speed) + " " + str(GPIO.input(sw2)) + "\n")
    text.set(str(speed))
    root.after(99, update_counter)
        
def check_sw0():
    global running, speed
    if GPIO.input(sw0) == 1 and running == 0:
        running = 1
        # delete old data file
        if os.path.exists(datafile) == True:
           os.remove(datafile)
        speed = 55
        root.after(100, update_counter)
        
    elif GPIO.input(sw0) == 0 and running == 1:
        running = 2
        root.after(100, quit)
    else:
        text3.set("SW0: " + str(GPIO.input(sw0)) + " " + "SW1a: " + str(GPIO.input(sw1a)) + " " + "SW1b: " + str(GPIO.input(sw1b)) + " ")
        
    root.after(200, check_sw0)

root = Tk()
root.geometry("500x500")
#root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.after(100, check_sw0)

fnt = font.Font(family = 'Helvetica', size = font_size, weight = 'bold')
fnt2 = font.Font(family = 'Helvetica', size = font_size2, weight = 'bold')
text = StringVar()
text2 = StringVar()
text3 = StringVar()
text.set(str(speed))
label = tk.Label(root, textvariable=text, font=fnt, foreground="red", background="black")
label2 = tk.Label(root, textvariable=text2, font=fnt2, foreground="blue", background="black")
label.place(relx=0.5, rely=0.5, anchor=CENTER)
label2.place(relx=0.5, rely=0.7, anchor=CENTER)
label3 = tk.Label(root, textvariable=text3, font=fnt2, foreground="green", background="black")
label3.place(relx=0.5, rely=0.9, anchor=CENTER)

root.mainloop()
If booting to the GUI...

Try using /etc/xdg/lxsession/LXDE-pi/autostart

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart


e. g. To run a python script add...
@/usr/bin/python3 /home/pi/filename.py

Then press ctrl & X, and then Y and enter.

To Make the file executable with sudo chmod +x filename.py

It will run after the gui starts.

Return to “Beginners”