Sustain Press on switch

Lamtron

Member
I have a project that requires a user to hold or maintain pressure on a input switch for a short delay say 5 secs before the output will turn on. I need some ideas on how to start. Not sure but it seems like a if then statement would be a good start. Once the delay has expired the output will stay on or high as long as the user holds down the input switch. This project is for disabled children who have high tone which makes it hard for them to play games or turn on devices.

Main:
if pin 3=1 then jump ' If input is pressed then goto to jump code
goto Main

Jump:
pause 5 secs ' wait 5 secs before output 2 goes high
High 2 ' Output is high relay is on

As you can see I think I may need a little help.
 
Last edited:

inglewoodpete

Senior Member
Have a look at the code I posted in code snippets a few years ago. You should be able to use some or all of the features included in the code. While it was written for the AXE092 board, PICAXE code is pretty universal and could be easily modified for other PICAXE chips or boards.
 

hippy

Technical Support
Staff member
If using an M2 you can use the 'time' vraiable to determine an approximate 5 seconds ...

Code:
time = 0
Do
  If pin3 <> 1 Then
    time = 0
  End If
Loop Until time >= 5
High 2
This loops until elapsed time reaches 5 seconds. If the button isn't pressed then the elapsed time is reset to zero. So for elapsed time to reach 5 seconds the button must have been held for 5 seconds.

Then to keep the output on until the button is released ...

Code:
Do : Loop Until pin3 = 0
Low 2
Putting it all in a loop, with some slight shuffling of the above ...

Code:
Do
  Low 2
  time = 0
  Do
    If pin3 <> 1 Then
      time = 0
    End If
  Loop Until time >= 5
  High 2
  Do : Loop Until pin3 = 0
Loop
 

hippy

Technical Support
Staff member
If you aren't using an M2 and the 'time' varable isn't available; the above can be emulated by using an 'elapsedTime' word variable which can count the number of milliseconds paused, for example -

Code:
Symbol elapsedTime = w1

Do
  Low 2
  elapsedTime = 0
  Do
    Pause 50
    elapsedTime = elapsedTime + 50
    If pin3 <> 1 Then
      elapsedTime = 0
    End If
  Loop Until elapsedTime >= 5000
  High 2
  Do : Loop Until pin3 = 0
Loop
That code can also be used with the M2 freeing up 'time' to be used for other things.

There's even a clever trick to reduce the code, roll the increment and pin3 test into one ...

Code:
  Do
    Pause 50
    elapsedTime = elapsedTime + 50 * pin3
  Loop Until elapsedTime >= 5000
The 'pin3' will only return a 0 (not pushed) or a 1 (pushed).
 
Top