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

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

Wed Aug 07, 2019 8:18 pm

hi all i am new to raspberry. pls help me with coding timer (100 msec) , switch input interrupt
how can i display 2 numbers on the hdmi screen 1'' high and 4'' high ( example 54 on 2 different size)
any help is appreciated
harry P

User avatar
neilgl
Posts: 640
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

Wed Aug 07, 2019 10:30 pm

Is that like a stopwatch/timer sort of thing?

hkpatnaik
Posts: 13
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 08, 2019 2:25 pm

yes it is 100msec ( 0.1sec) timer and certain events run due to the timer.
Also i need a random number generator program

User avatar
neilgl
Posts: 640
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 Aug 08, 2019 3:22 pm

Here is an example of python3 using tk to display a simple GUI program, with large fonts. Is that what you want? GPIO and timer code can be added later, and python has a random number generator.
Run it from a terminal: python3 big.py

Code: Select all

# python3 and tk
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        myfont = ('times', 132, 'bold')
        self.hi_there["text"] = "54 Hello World\n(click me)"
        self.hi_there.config(font = myfont)
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")
        newfont = ('Helvetica', 64, 'bold')
        self.quit = tk.Button(self, text="QUIT", fg="red",command=root.destroy)
        self.quit.config(font = newfont)
        self.quit.pack(side="bottom")

    def say_hi(self):
        self.hi_there["text"] = "54 Hello World\n(clicked)"

root = tk.Tk()
app = Application(master=root)
app.mainloop()

hkpatnaik
Posts: 13
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 08, 2019 8:05 pm

Hi,
I have a RP board running with a monitor on the hdmi port. I also have a SD card with boot program which has NOOBS i believe. so after booting I have a Road scene and there are few tabs in the left hand corner starting with Raspberry etc. I have a key board and mouse.
How do i type in the program u provided? do i go to editor . at present if i go to terminal mode i see a $ prompt
so i know i am asking basic questions like how do i enter the program. do i open The Thonny python IDE???
thanks
hare

User avatar
rpdom
Posts: 14742
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

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

Thu Aug 08, 2019 8:16 pm

Click on the Raspberry to get the program menu. Select the "Programming" option. That will give you a list of various programming tools including Thonny.

User avatar
neilgl
Posts: 640
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 Aug 08, 2019 8:18 pm

In the browser, looking at this forum at my example, Select all, edit copy.
Start a terminal prompt. ( You could do it via Thonny, but not needed.)
Edit a file called big.py like this:

Code: Select all

nano big.py
Now edit paste, the code appears on your terminal
Press control-o, control-x to save big.py and exit nano.
Now at the $ prompt, type

Code: Select all

python3 big.py
It should start the GUI program.
Let me know how it goes...

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

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

Sat Aug 10, 2019 2:49 pm

Hi neilgl,
Thanks for u r help. I am having trouble with key board. I have changed 3 key boards for If i use the # key on raspberry the display shows British pound sign. I can not see the # sign at all. I have not compiled but changed key boards still i can not type # sign but on my PC it shows # but on RP it shows the british pound sign
thanks again for working with me
hare

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

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

Sat Aug 10, 2019 3:06 pm

Try changing the settings...

choose Preferences > Raspberry Pi Configuration > Localisation > Keyboard, then choose as required
Attachments
keyb.jpg
keyb.jpg (41.08 KiB) Viewed 322 times

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

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

Sat Aug 10, 2019 3:44 pm

hi neigel,
Thanks, sorry to bother u. I get syntax error
code:select all syntax error at ":"
also syntax error
nano big.py syntax error at big.
pls. help
hare

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

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

Sat Aug 10, 2019 3:56 pm

Heres' a pygame stopwatch

Code: Select all

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

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

redColor = pygame.Color(255,0,0)
greyColor = pygame.Color(50,50,50)
pygame.init()
font_size = 200
width = font_size * 6
height = font_size
windowSurfaceObj = pygame.display.set_mode((width,height),1,24)
pygame.display.set_caption('STOPWATCH')
fontObj = pygame.font.Font('freesansbold.ttf',font_size)
msg = "00:00:00:000"
msgSurfaceObj = fontObj.render(msg, False,redColor)
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(2,0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update()

try:
   while GPIO.input(switch)==0: 
      down = 0
   print ("  Ready")

   down = 0
   
   while True :
      if GPIO.input(switch)==1:
         if down == 0:
            start = time.time()
         now = time.time() - start
         m,s = divmod(now,60)
         h,m = divmod(m,60)
         msg= "%02d:%02d:%02d" % (h,m,s)
         psec = str(now-int(now))
         pstr = psec[1:5]
         msg = msg + str(pstr)
         pygame.draw.rect(windowSurfaceObj,greyColor,Rect(0,0,width,height))
         msgSurfaceObj = fontObj.render(msg, False,redColor)
         msgRectobj = msgSurfaceObj.get_rect()
         msgRectobj.topleft =(2,0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
         windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
         pygame.display.update()
         down = 1
      if GPIO.input(switch)==1:
         down = 0
      for event in pygame.event.get():
         if event.type == QUIT:
            pygame.quit()
            sys.exit()

except KeyboardInterrupt:
  print ("  Quit")
  GPIO.cleanup() 

User avatar
neilgl
Posts: 640
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

Sat Aug 10, 2019 5:19 pm

Ah yes I meant click on the red Select all in my post and then copy it. Then you can paste it when you have the editor (nano) open on the pi.

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

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

Sat Aug 10, 2019 6:16 pm

Hi neilgl,
Thanks for u r patience and i made some progress
line 6 def_init_(self,master=None): invalid syntax pointing to the semi colon :
this is while i was trying to run the program
sincerely
Hare P

User avatar
neilgl
Posts: 640
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

Sat Aug 10, 2019 6:22 pm

Ok post the code from your pi onto a reply here, using the “Code” tags that are in the grey bar here on this forum when you reply.

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

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

Mon Aug 12, 2019 6:32 pm

Thanks Gordon I will definitely try your program.
Hare

User avatar
neilgl
Posts: 640
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

Mon Aug 12, 2019 8:28 pm

You may get an error when you try that for pygame.locals.

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

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

Mon Aug 12, 2019 8:32 pm

# python3 and tk
import tkinter as tk
class application(tk.Frame):
def_init_(self,master=None)
super()._init_(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there=tk.Button(self)
myfont=('times',132,'bold')
self.hi_there["text"]="54 Hello World\n(me)"
self.hi_there.config(font=myfont)
self.hi_there["command"]=self.say_hi
self.hi_there.pack(side="top")
newfont=('Helvetica',64,'bold')
self.quit=tk.Button(self,text="QUIT",fg="red",command=root.destroy)
self.quit.config(font=newfont)
self.quit.pack(side="bottom")
def say_hi(self):
self.hi_there["text"]="54 Hello World\n(clicked)"
root=tk.Tk()
app=Application(master=rot)
app.mainloop( )

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

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

Mon Aug 12, 2019 8:43 pm

Hi niegll
when i run big.py
i get following errors
File" .big.py" line 5 in <module>
class application( tk.frame);
File" .big.py" line 6 in application
def_init_(self ,master=None)
Name Error: name"def_init_" is not defined

User avatar
neilgl
Posts: 640
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

Mon Aug 12, 2019 10:10 pm

Looks like the copy paste is not working properly - maybe some UK to non-Uk translation.
Anyway, indentation is critical in python.
Post the code you are running in between “Code” tags

markkuk
Posts: 102
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

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

Tue Aug 13, 2019 6:30 am

You must add a space between "def" and "__init__". "def" is a Python keyword that starts a function definition, "__init__" is the name for a class constructor function.

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

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

Tue Aug 13, 2019 3:27 pm

Hi neilgl & Gordon,
I ran Gordon's program and it ran perfect as prescribed. I will appreciate if u & neilgl can help me in my program.
board has 3 push button switch inputs
sw1 start on/off
sw2 record speed data
sw3 record video data
3 local or global variables
tcounter
speed
when sw1 is pushed : open a data file
clear all variables
speed= 55
display speed on the HDMI screen in the center


when we get
100 msec timer interrupt
tcounter increments by 1
when tcounter is multiples of 10 ( i.e every second) call the random number generator ( 0 to 99)
if the random number >67 increment speed by one and display speed in the center of hdmi screen EXIT 0
if the random number is between 33 and 66 decrement the speed by one and display speed and exit 0
if the random number is between 0 and 32 keep the speed same value ( no increment or decrement) exit 0

exit 0: if sw2 pushed on then store tcounter value, speed
if sw3 is pushed on then store tcounter value
if sw1 is closed stop the program, close the file

thanks
hare

User avatar
neilgl
Posts: 640
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

Tue Aug 13, 2019 3:40 pm

OK what happened when you ran the program I posted?
I also have another one ( partly stolen from @gordon77 ) that starts/stops a stopwatch using switches on GPIO pins.

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

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

Tue Aug 13, 2019 5:26 pm

hi niegll,
I ran your program but had syntax and definition errors but gordon's program ran perfect and i could see the result 00:00:00:000 displayed on the window. I changed the font size and could display 1" and 4" characters. hope u can help me with the total program i described in the previous reply.
I may soung like a 5 yr old but i am retired and continuing education at age 70. but it is hard to learn at this age. I can do it given enough time . I never learned software but i am fairly good in electronics
hare P

User avatar
neilgl
Posts: 640
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

Tue Aug 13, 2019 9:29 pm

Ok you are not much older than me. So copy and paste (into nano) my first example. Let me know if it works on your pi - it does work on mine. Indentation and spaces are important in python. The three switches requirements look pretty straightforward t implement.
I can set up another pi to be like yours - what country is it?

gordon77
Posts: 4072
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 8:40 am

I've modified neilgl's code to include a stopwatch, hopefully give you some ideas.

Code: Select all

# python3 and tk
import tkinter as tk
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
switch = 17 # pin 11, switch to +3.3v
GPIO.setup(switch,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        global running
        running = 0
        self.master.title('Stopwatch')
        self.hi_there = tk.Button(self)
        myfont = ('times', 132, 'bold')
        self.hi_there["text"] = "00:00:00:00"
        self.hi_there.config(font = myfont)
        self.hi_there["command"] = self.start_clock
        self.hi_there.pack(side="top")
        newfont = ('Helvetica', 64, 'bold')
        self.quit = tk.Button(self, text="QUIT", fg="red",command=root.destroy)
        self.quit.config(font = newfont)
        self.quit.pack(side="bottom")
        L1 = tk.Label(self.hi_there, text="Click on time, or toggle GPIO #17, to start / stop " , font = 30)
        L1.place(x=20, y=180)
        self.check_switches()

    def start_clock(self):
        global start,running
        start = time.time()
        if running == 0:
            running = 1
        elif running == 1:
            running = 0
        self.update_clock()
        
    def update_clock(self):
        if running == 1:
            now = time.time() - start
            m,s = divmod(now,60)
            h,m = divmod(m,60)
            psec = str(now-int(now))
            pstr = psec[1:4]
            msg= "%02d:%02d:%02d" % (h,m,s)
            msg = msg + str(pstr)
            self.hi_there["text"] = msg
        self.after(10, self.update_clock)
        
    def check_switches(self):
        global switch, start, running
        if GPIO.input(switch) == 1 and running == 0:
            start = time.time()
            running = 1
            self.update_clock()
        elif GPIO.input(switch) == 1 and running == 1:
            running = 0
            
        self.after(100, self.check_switches)
        
        
        

root = tk.Tk()
app = Application(master=root)
app.mainloop()
Attachments
watch.jpg
watch.jpg (16.69 KiB) Viewed 118 times

Return to “Beginners”