Counting to view on the OLED display

I want to simply count every time an Input switch is pressed and display this on my OLED Display. I want it to display 1 then 2 then 3 etc. each time I press the switch. I can get my OLED Display to display words; serout B.7, N2400, ("Hello"). I also need a simple code to count. Can anyone help...just starting. Thanks.
 

WhiteSpace

Well-known member
How about something like this? I was experimenting with the Picaxe OLED display before I tried the small 128 x 64 display, so I had written something very similar to what you want, as a test. It works on the simulator, but not tested on a real display. I'm also fairly new to this, so I'm sure some more experienced members will be able to improve on this.

You'll want to keep your serout B.7 rather than the serout B.0 that I have.

Good luck!

Rich (BB code):
;switch is on pinB.1 - high if closed

InitializeDisplay:
      serout b.0, n2400, ( 254, 1 )
      pause 500
                  
SayHello:
      serout b.0, n2400, ( 254, 1 )
      pause 500
      serout b.0, n2400, ( 254, 128 )           ; First line of display
      serout b.0, n2400, ( "Hello") ; Display "Hello"
            
      pause 1000                    ; Wait a second

SetCounter:
      let b1 = 0 ; start the counter at 0
      goto DisplayCount
      
Counter:
      If pinB.1 = 1 then ; if switch is pressed
            pause 75 ' debounce
            inc b1 'increase the value of b1 by 1
            else goto Counter 'keep looping until there is a button press
endif
      
      DisplayCount:
      serout b.0, n2400, ( 254, $C0 )           ; Second line of display
      serout b.0, n2400, ("Counter = ", #b1)    ; #b1 displays the contents of b1
      pause 100                     ; Wait 1/10 second
      
      if b1 < 255 then goto Counter ;so that the Counter doesn't go beyond 254 - 255 is the largest number than can fit in a byte variable 
end
 

inglewoodpete

Senior Member
Since this is a student exercise in program development, it would be wise to look at potential issues with the code generously provided by WhiteSpace. The first is that the debounce detection may not be effective: there is no confirmation of a positive key press. Secondly, there no checking that the switch has been released before a successive keypress can be identified.
 

WhiteSpace

Well-known member
Thanks inglewoodpete - I thought someone might be able to improve on this. I hope I haven’t done the OP’s homework though! I wasn’t sure that I had properly understood the denounce process, so I’m glad you have picked this up. Manual 3 suggested adding a short delay after the button press, but I don’t fully understand how the code works.
 
Thanks 'White space'. That got me started and I developed it a little further from there. Although not a student, actually retired, everyone needs to start somewhere and you were very helpful indeed.
 

WhiteSpace

Well-known member
That’s great Nickalston - I too am just starting and have had some great help from senior members here, so I’m pleased to be able to pay some of it back already. ^ “debounce”, of course, not “denounce” - my Autocorrect seems to have a very authoritarian view of the world.
 

hippy

Technical Support
Staff member
My take on this would be ...
Code:
Symbol BUTTON_PIN = pinC.0
Symbol PUSHED     = 1

Symbol OLED       = B.1

MainLoop:
  Pause 500
  SerOut OLED, N2400, ( 254,1 )
  Pause 500
  Do
    Do : Loop While BUTTON_PIN =  PUSHED
    Do : Loop Until BUTTON_PIN <> PUSHED
    w0 = w0 + 1
    BinToAscii w0, b15,b14,b13,b12,b11
    SerOut OLED, N2400, ( 254,$80, b15,b14,b13,b12,b11 )
  Loop
 

inglewoodpete

Senior Member
Thanks 'White space'. That got me started and I developed it a little further from there. Although not a student, actually retired, everyone needs to start somewhere and you were very helpful indeed.
Sorry Nickalston, I must have been reading between the lines :confused:. But yes, I guess we're all students. I'm trying to be semi-retired but work keeps finding me. Fortunately, I enjoy what I do and it pays well!

@hippy, Your example code addresses the need to ensure that the switch has been released, preventing multiple loops from counting a single, longer key press.

The debounce algorithm that I use is two positive results for a button press, spaced 40mS apart. If not, allow the code to loop and perform the test again. Generally 40mS seems to be accepted as a debounce period for mechanical switches. I have to accept that PICAXEs run relatively slowly and you may get away with not testing twice for a single press. It is difficult to operate a pushbutton for less than 100 or 120mS, so if a bounce is detected during the first loop, the positive button press should be picked up in the second.
 

WhiteSpace

Well-known member
@inglewoodpete - for your debounce code, do you mean something like this? I hope I'm doing 3 things:
- by looping back to CheckCount after the first test, ie. if C.0 is 0, then I wait for a button push;
- then with the "if pinC.0 = 0" in effect I wait for the switch to go high again . If it doesn't it was a bounce and can be ignored; and
- I wait for the button to stop being pressed, so that even if the button is pressed for a long time the counter changes only once

Thanks very much.

Rich (BB code):
;CheckCount:
;
;     If pinC.0 = 1 then            'C.0 in this instance is a push button
;     pause 40                      'waits for pinC.0 to go high
;     Else goto CheckCount          ; if not, it loops back to check the counter again
;     endif                         ; then continues with the code
;
;     If pinC.0 = 0 then goto CheckCount 'The code is reversed here - if pinC.0 = 0, ie. it was a very short press, probably a button, it loops
;                                         ; back to wait for a proper signal.  If it's 1 (a proper press) the code continues
;     let b8 = b8 - 1                     'decrease the value of b8 to feed into the counter.
;     
;     do loop while pinC.0 = 1      ; and then wait until the button is released before advancing the counter
 

inglewoodpete

Senior Member
I would use the following structure, based on hippy's example, above. The code here is fine for simple projects but you need to be aware that any other code or events will not be processed while the button is pressed/held.
Code:
'Based on original code by hippy
Symbol BUTTON_PIN = pinC.0
Symbol PUSHED     = 1
Symbol OLED       = B.1
MainLoop:
   Pause 500
   SerOut OLED, N2400, (254,1)
   Pause 500
   Do
      If BUTTON_PIN = PUSHED Then
         Pause 40
         If BUTTON_PIN = PUSHED Then
            'If button test gives 2 positive results, 40mS apart...
            w0 = w0 + 1 'Alternatively, use 'Inc w0'
            BinToAscii w0, b15,b14,b13,b12,b11
            SerOut OLED, N2400, (254, $80, b15, b14, b13, b12 ,b11)
            Do: Loop While BUTTON_PIN = PUSHED
         EndIf
      EndIf
   Loop
For my personal projects, I tend to use the X2-series of PICAXEs and use a timer interrupt.
 
Top