steveinciren
Posts: 6
Joined: Fri Apr 08, 2016 12:18 pm

Displayed event counter

Fri Apr 08, 2016 12:25 pm

Hi Guys - - -it has been suggested to me that the raspberry pi would be a good candidate to make a tv displayed 2 digit event counter that just displays two big digits that increase by one every time a button is pressed - - t would go to 99 then the next step would take it back to zero ... can anybody help me with this please ?

Candyjet
Posts: 56
Joined: Sun Jan 10, 2016 3:45 pm

Re: Displayed event counter

Fri Apr 08, 2016 2:31 pm

Have you tried typing 'detect button press pi' into Google?
It's a good place to start and will show you what you need.

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

Re: Displayed event counter

Fri Apr 08, 2016 3:06 pm

This should get you started,,, connect your switch between pin 26 and gnd

Code: Select all

#!/usr/bin/python
import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
import time

# set font size 
fontsize = 720

# calculate window size
width = fontsize * 2
height = fontsize

# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),FULLSCREEN,16)
fontObj = pygame.font.Font('freesansbold.ttf',fontsize)
pygame.display.set_caption('Counter')
color = pygame.Color(255,0,0)
blackColor = pygame.Color(0,0,0)

GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)

# set input for button
button = 26
GPIO.setup(button,GPIO.IN,pull_up_down = GPIO.PUD_UP)
count = 0
msgSurfaceObj = fontObj.render(str(count), False,color)   
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(fontsize/1.5,0)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update(pygame.Rect(0,0,width,height))
      
while True:
   if GPIO.input(button) == 0:
      count +=1
      if count == 100:
         count = 0
      # print    
      pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
      msgSurfaceObj = fontObj.render(str(count), False,color)   
      msgRectobj = msgSurfaceObj.get_rect()
      if count > 9:
         msgRectobj.topleft =(fontsize/2,0)
      else:
         msgRectobj.topleft =(fontsize/1.5,0)
      windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
      pygame.display.update(pygame.Rect(0,0,width,height))
      time.sleep(.25)
      #unmask the next 2 lines if you don't want holding the button to increment the counter
      #while GPIO.input(button) == 0:
      #   time.sleep(0.01)
               
   # check for ESC key pressed to quit
   for event in pygame.event.get():
       if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
          pygame.quit()
          sys.exit()

steveinciren
Posts: 6
Joined: Fri Apr 08, 2016 12:18 pm

Re: Displayed event counter

Fri Apr 08, 2016 5:17 pm

Thankyou soooo much ... ill try it out pronto ... what a lovely bunch you all are ... ;)

steveinciren
Posts: 6
Joined: Fri Apr 08, 2016 12:18 pm

Re: Displayed event counter

Wed Apr 13, 2016 2:27 pm

ok - - so silly question ... ive bought a PI .. and ive got a desktop looking thing ... and then what on earth do i do with th lovely script ... ive tried pasting it into python script editor and saving it ... and thats kind of where im at ... how does one make an executable file on a PI ?

steveinciren
Posts: 6
Joined: Fri Apr 08, 2016 12:18 pm

Re: Displayed event counter

Wed Apr 13, 2016 4:14 pm

ignore that - - done it .. lol ... all working beautifully - - next step .. number subtraction on another button ... and a reset button on another :S .... however - - changing font has me vexed ... it doesnt like me using any of the other .ttfs ... it just crashes the PI each time i change it to anything else :S

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

Re: Displayed event counter

Thu Apr 14, 2016 8:32 am

Firstly I have never found a way of getting different fonts with pygame.
If you are experimenting with the scripts change FULLSCREEN to 1, this will then show you the error messages and mean you don't need to reboot the Pi when it stops.

Heres some examples...
Using 3 buttons as you suggested...

Code: Select all

#!/usr/bin/python
import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
import time

# set input for button
plus_button = 26
minus_button = 40
reset_button = 22

# set font size 
fontsize = 720

# calculate window size
width = fontsize * 2
height = fontsize

# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),FULLSCREEN,16)
fontObj = pygame.font.Font('freesansbold.ttf',fontsize)
pygame.display.set_caption('Counter')
color = pygame.Color(255,0,0)
blackColor = pygame.Color(0,0,0)

GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(plus_button,GPIO.IN,pull_up_down = GPIO.PUD_UP)
GPIO.setup(minus_button,GPIO.IN,pull_up_down = GPIO.PUD_UP)
GPIO.setup(reset_button,GPIO.IN,pull_up_down = GPIO.PUD_UP)
count = 0
msgSurfaceObj = fontObj.render(str(count), False,color)   
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(fontsize/1.5,0)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update(pygame.Rect(0,0,width,height))
      
while True:
   if GPIO.input(plus_button) == 0:
      count +=1
      if count == 100:
         count = 0
      pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
      msgSurfaceObj = fontObj.render(str(count), False,color)   
      msgRectobj = msgSurfaceObj.get_rect()
      if count > 9:
         msgRectobj.topleft =(fontsize/2,0)
      else:
         msgRectobj.topleft =(fontsize/1.5,0)
      windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
      pygame.display.update(pygame.Rect(0,0,width,height))
      time.sleep(.25)
      #unmask the next 2 lines if you don't want holding the button to increment the counter
      #while GPIO.input(plus_button) == 0:
      #   time.sleep(0.01)

   if GPIO.input(minus_button) == 0:
      count -=1
      if count < 0:
         count = 0
      pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
      msgSurfaceObj = fontObj.render(str(count), False,color)   
      msgRectobj = msgSurfaceObj.get_rect()
      if count > 9:
         msgRectobj.topleft =(fontsize/2,0)
      else:
         msgRectobj.topleft =(fontsize/1.5,0)
      windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
      pygame.display.update(pygame.Rect(0,0,width,height))
      time.sleep(.25)
      #unmask the next 2 lines if you don't want holding the button to increment the counter
      #while GPIO.input(minus_button) == 0:
      #   time.sleep(0.01)
      
   if GPIO.input(reset_button) == 0:
      count = 0
      pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
      msgSurfaceObj = fontObj.render(str(count), False,color)   
      msgRectobj = msgSurfaceObj.get_rect()
      msgRectobj.topleft =(fontsize/1.5,0)
      windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
      pygame.display.update(pygame.Rect(0,0,width,height))
      
   # check for ESC key pressed to quit
   for event in pygame.event.get():
       if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
          pygame.quit()
          sys.exit()
Using 2 button for UP and DOWN, hold one to reset to 0...

Code: Select all

#!/usr/bin/python
import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
import time

# set input for button
plus_button = 26
minus_button = 40

#reset time
reset_time = 2

# set font size 
fontsize = 720

# calculate window size
width = fontsize * 2
height = fontsize

# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),FULLSCREEN,16)
fontObj = pygame.font.Font('freesansbold.ttf',fontsize)
pygame.display.set_caption('Counter')
color = pygame.Color(255,0,0)
blackColor = pygame.Color(0,0,0)

GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(plus_button,GPIO.IN,pull_up_down = GPIO.PUD_UP)
GPIO.setup(minus_button,GPIO.IN,pull_up_down = GPIO.PUD_UP)

count = 0
msgSurfaceObj = fontObj.render(str(count), False,color)   
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(fontsize/1.5,0)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update(pygame.Rect(0,0,width,height))
      
while True:
   if GPIO.input(plus_button) == 0:
      start = time.time()
      count +=1
      if count == 100:
         count = 0
      pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
      msgSurfaceObj = fontObj.render(str(count), False,color)   
      msgRectobj = msgSurfaceObj.get_rect()
      if count > 9:
         msgRectobj.topleft =(fontsize/2,0)
      else:
         msgRectobj.topleft =(fontsize/1.5,0)
      windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
      pygame.display.update(pygame.Rect(0,0,width,height))
      time.sleep(.25)
      
      while GPIO.input(plus_button) == 0:
         # check for count button held for > reset_time
         if time.time() - start > reset_time:
            count = 0
            pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
            msgSurfaceObj = fontObj.render(str(count), False,color)   
            msgRectobj = msgSurfaceObj.get_rect()
            msgRectobj.topleft =(fontsize/1.5,0)
            windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
            pygame.display.update(pygame.Rect(0,0,width,height))
         time.sleep(0.01)
      
   if GPIO.input(minus_button) == 0:
      start = time.time()
      count -=1
      if count < 0:
         count = 0
      pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
      msgSurfaceObj = fontObj.render(str(count), False,color)   
      msgRectobj = msgSurfaceObj.get_rect()
      if count > 9:
         msgRectobj.topleft =(fontsize/2,0)
      else:
         msgRectobj.topleft =(fontsize/1.5,0)
      windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
      pygame.display.update(pygame.Rect(0,0,width,height))
      time.sleep(.25)
      while GPIO.input(minus_button) == 0:
         # check for count button held for > reset_time
         if time.time() - start > reset_time:
            count = 0
            pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
            msgSurfaceObj = fontObj.render(str(count), False,color)   
            msgRectobj = msgSurfaceObj.get_rect()
            msgRectobj.topleft =(fontsize/1.5,0)
            windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
            pygame.display.update(pygame.Rect(0,0,width,height))
         time.sleep(0.01)
      
     
   # check for ESC key pressed to quit
   for event in pygame.event.get():
       if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
          pygame.quit()
          sys.exit()
Using 1 button, quick press to go UP, slow press for DOWN and long press for RESET. TImes set in script . It may take some practice and you may get some debounce issues...

Code: Select all

#!/usr/bin#!/usr/bin/python
import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
import time

# set input for button
button = 26

# button times
plus = .25
minus = 1

#reset time
reset_time = 3

# set font size 
fontsize = 720

# calculate window size
width = fontsize * 2
height = fontsize

# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),FULLSCREEN,16)
fontObj = pygame.font.Font('freesansbold.ttf',fontsize)
pygame.display.set_caption('Counter')
color = pygame.Color(255,0,0)
blackColor = pygame.Color(0,0,0)

GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(button,GPIO.IN,pull_up_down = GPIO.PUD_UP)

count = 0
msgSurfaceObj = fontObj.render(str(count), False,color)   
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(fontsize/1.5,0)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update(pygame.Rect(0,0,width,height))
      
while True:
   if GPIO.input(button) == 0:
      start = time.time()
      while GPIO.input(button) == 0 and time.time() - start < reset_time:
         time.sleep(0.1)
      timer = time.time() - start 
      if timer < plus:
         count +=1
         if count == 100:
            count = 0
      if timer > plus and timer < reset_time:
         count -=1
         if count < 0:
            count = 0
      if timer > reset_time:
         count = 0

      pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
      msgSurfaceObj = fontObj.render(str(count), False,color)   
      msgRectobj = msgSurfaceObj.get_rect()
      msgRectobj.topleft =(fontsize/1.5,0)
      windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
      pygame.display.update(pygame.Rect(0,0,width,height))
      time.sleep(0.25)
     
   # check for ESC key pressed to quit
   for event in pygame.event.get():
       if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
          pygame.quit()
          sys.exit()
/python
import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
import time

# set input for button
button = 26

# button times
plus = .25
minus = 1

#reset time
reset_time = 3

# set font size 
fontsize = 720

# calculate window size
width = fontsize * 2
height = fontsize

# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),FULLSCREEN,16)
fontObj = pygame.font.Font('freesansbold.ttf',fontsize)
pygame.display.set_caption('Counter')
color = pygame.Color(255,0,0)
blackColor = pygame.Color(0,0,0)

GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(button,GPIO.IN,pull_up_down = GPIO.PUD_UP)

count = 0
msgSurfaceObj = fontObj.render(str(count), False,color)   
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(fontsize/1.5,0)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update(pygame.Rect(0,0,width,height))
      
while True:
   if GPIO.input(button) == 0:
      start = time.time()
      while GPIO.input(button) == 0 and time.time() - start < reset_time:
         time.sleep(0.1)
      timer = time.time() - start 
      if timer < plus:
         count +=1
         if count == 100:
            count = 0
      if timer > plus and timer < reset_time:
         count -=1
         if count < 0:
            count = 0
      if timer > reset_time:
         count = 0

      pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
      msgSurfaceObj = fontObj.render(str(count), False,color)   
      msgRectobj = msgSurfaceObj.get_rect()
      msgRectobj.topleft =(fontsize/1.5,0)
      windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
      pygame.display.update(pygame.Rect(0,0,width,height))
      time.sleep(0.25)
     
   # check for ESC key pressed to quit
   for event in pygame.event.get():
       if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
          pygame.quit()
          sys.exit()

steveinciren
Posts: 6
Joined: Fri Apr 08, 2016 12:18 pm

Re: Displayed event counter

Tue Jun 28, 2016 7:25 pm

wow - - ive logged back in to ask a few questions and found all your amazing responses .. thankyou ... typically id put this poject on a shelf to do some other stuff and now need to finish it quickly ... one last very cheeky question ... can i have the button make a sound (through the hdmi) when pushed ... or even better .. read out the number ?? :)

bradyv2001
Posts: 1
Joined: Thu Nov 10, 2016 8:18 pm

Re: Displayed event counter

Thu Nov 10, 2016 8:25 pm

For a fairly new python coding newbie (coming from PHP world), this thread has helped me tremendously with my first Pi project. I was hoping someone with more experience could assist me in changing from numbers (1,2,3) to alphabet (A-Z)? From my few days of searching various forums, I think I should be using a statement like such:

>>> chr(ord('c') + 1)

but not sure how to reconfigure any example above. Any assistance would be greatly appreciate.

MartinusP
Posts: 3
Joined: Tue Aug 08, 2017 6:21 pm

Re: Displayed event counter

Tue Sep 26, 2017 7:12 pm

how can I copy and paste the code above?

Martien.

Return to “Beginners”