ignoring an input

Shadow1976

New Member
Hi guys.
excellent forum. I am new to picaxe and mcu's but i have a little project that i have become stuck with a bit of code that someone here would probably breeze through.
I am trying to create code that would activate an output (say an led) for x amount of time when an input changes state (low to high) but then ignore the high state until it moves from low to high again. Even ignore the high state on startup. I have tried an interrupt but have noticed it will loop if the input is still high causing the output to stay high much like an IF statement.
any info would be great.
I am playing with an alarm for kitchen doors for the little one, a bit of fun.
 

jims

Senior Member
Hi guys.
excellent forum. I am new to picaxe and mcu's but i have a little project that i have become stuck with a bit of code that someone here would probably breeze through.
I am trying to create code that would activate an output (say an led) for x amount of time when an input changes state (low to high) but then ignore the high state until it moves from low to high again. Even ignore the high state on startup. I have tried an interrupt but have noticed it will loop if the input is still high causing the output to stay high much like an IF statement.
any info would be great.
I am playing with an alarm for kitchen doors for the little one, a bit of fun.
Would something like this do it??
 

Attachments

Shadow1976

New Member
Hi jim,
yea sort of but i need it to continue to check other inputs and do the same thing. would that be possible? I am kind of stumped.
 
Hi guys.
excellent forum. I am new to picaxe and mcu's but i have a little project that i have become stuck with a bit of code that someone here would probably breeze through.
I am trying to create code that would activate an output (say an led) for x amount of time when an input changes state (low to high) but then ignore the high state until it moves from low to high again. Even ignore the high state on startup. I have tried an interrupt but have noticed it will loop if the input is still high causing the output to stay high much like an IF statement.
any info would be great.
I am playing with an alarm for kitchen doors for the little one, a bit of fun.
So, making sure I'm understanding right:

When a switch goes high, you want the output to activate for some time then turn off again. You then want the output to remain off, even if the switch is still high, until the switch is first returned to the low state (so like a reset action).

This can be done with two interrupts, with the first triggering when the switch goes from low to high, and the second when it goes from high to low. The first needs to sound the alarm, and the second needs to simply reenable the first interrupt, re-arming the alarm. But, PICAXE only allows us one interrupt routine regardless of the condition. So a variable must be introduced to keep track of whether the alarm has already been triggered or not. Depending on the value of the variable, we can find out why we have entered the interrupt. If it's 0, then the alarm hasn't been triggered yet, so if we get to the interrupt that must mean the switch just went high. If it's 1, the alarm has been triggered, so reaching the interrupt means the switch just went low. We can use an if statement to test this and execute the correct code as a result.
Try this:

Code:
[color=Blue]symbol [/color][color=Purple]IN [/color][color=DarkCyan]= [/color][color=Purple]pinc.0                  [/color][color=Green]'switch[/color]
[color=Blue]symbol OUT [/color][color=DarkCyan]= [/color][color=Blue]c.1                 [/color][color=Green]'output device[/color]
[color=Blue]symbol [/color][color=Purple]STATE [/color][color=DarkCyan]= [/color][color=Purple]b0                   [/color][color=Green]'has interrupt already been triggered?[/color]
[color=Blue]symbol ALM_TIME [/color][color=DarkCyan]= [/color][color=Navy]10000             [/color][color=Green]'how long output stays on for[/color]

[color=Black]arm:                                
      [/color][color=Blue]let [/color][color=Purple]STATE [/color][color=DarkCyan]= [/color][color=Navy]0                 [/color][color=Green]'enable the alarm
      [/color][color=Blue]setint [/color][color=Navy]%00000001[/color][color=Black], [/color][color=Navy]%00000001   [/color][color=Green]'trigger interrupt when switch goes high
      [/color]
[color=Black]main:
      [/color][color=Blue]pause [/color][color=Navy]1000                    [/color][color=Green]'currently an empty loop but program could do whatever else it needs to here
      [/color][color=Blue]goto [/color][color=Black]main
      [/color]
[color=Blue]interrupt:
      if [/color][color=Purple]state [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then             [/color][color=Green]'alarm has not already been triggered so it has just been triggered
            [/color][color=Blue]high OUT                [/color][color=Green]'switch output device on
            [/color][color=Blue]pause ALM_TIME          [/color][color=Green]'wait some time
            [/color][color=Blue]low OUT                 [/color][color=Green]'switch output device off
            [/color][color=Blue]let [/color][color=Purple]STATE [/color][color=DarkCyan]= [/color][color=Navy]1           [/color][color=Green]'alarm has now been triggered
            [/color][color=Blue]setint [/color][color=Navy]%00000000[/color][color=Black], [/color][color=Navy]%00000001 [/color][color=Green]'set interrupt so that it now triggers when switch goes LOW
      [/color][color=Blue]else                          [/color][color=Green]'alarm has already been triggered so it has just been reset
            [/color][color=Blue]pause [/color][color=Navy]100               [/color][color=Green]'wait a short time to make sure switch doesn't bounce
            [/color][color=Blue]let [/color][color=Purple]STATE [/color][color=DarkCyan]= [/color][color=Navy]0           [/color][color=Green]'alarm is reset
            [/color][color=Blue]setint [/color][color=Navy]%00000001[/color][color=Black], [/color][color=Navy]%00000001 [/color][color=Green]'trigger interrupt when switch goes high
      [/color][color=Blue]endif
return
            
            [/color]
 

Roman505

Member
Sequential processing without interrupts will do it. Code a loop which waits for the switch to be low. Note that this covers the startup state. Once the switch is low, you will exit that loop and enter a loop which waits for the switch to be high. When the switch is high, light your LED for the requisite time and jump back to the first loop, the one with which you started, waiting for the switch state to be low. Here is sample code {Edit: the usual "untested" caveat]:
Code:
SYMBOL Switch = pinc.0 
SYMBOL Led = c.1 
SYMBOL Alarming = 10000

main:
; switch may be high or low on startup, or even after the LED is flashed.
DO
; does nothing.
; a tight loop which will exit when the switch is low.
WHILE Switch = 1

; switch is now low. We loop until it is high.
DO
; does nothing (I'm good at doing nothing)
WHILE Switch = 0

; switch is now high, but not from startup.
HIGH Led
PAUSE Alarming
LOW Led

GOTO main
 
Last edited:

Shadow1976

New Member
Hi guys,
Thanks for the replies. We are getting somewhere with the above but I have managed to do it with just a single input but have not had any success with multiple inputs. This is where I get stuck. I want it to ignore that input until it goes back low whilst checking other inputs for a high state to alarm.
 

inglewoodpete

Senior Member
As your thread title suggests, you need to tell your program to ignore an input for a while but continue to do all the other things that you want to happen.

To do this, you need to change the value of a variable to indicate that you want the behaviour modified for a period.

Using pseudo code:

Code:
Do
   If pinButton 1 = Pressed Then
      If B1AlreadyPressed = Yes Then
         'do nothing
      Else
         varB1AlreadyPressed = Yes
         varLED1IsOn = Yes
         Turn On LED1
         varTimeCount1 = A value
      EndIf
   EndIf
   If pinButton 2 = Pressed Then
      If varB2AlreadyPressed = Yes Then
         'do nothing
      Else
         varB2AlreadyPressed = Yes
         varLED2IsOn = Yes
         Turn On LED2
         varTimeCount2 = A value
      EndIf
   EndIf
   If varLED1IsOn = Yes Then
      Dec varTimeCount
      If varTimeCount1 = 0 Then
         varLED1IsOn = Off
         Turn LED1 Off
      EndIf
   EndIf
   If varLED2IsOn = Yes Then
      Dec varTimeCount
      If varTimeCount2 = 0 Then
         varLED1IsOn = Off
         Turn LED2 Off
      EndIf
   EndIf
   Pause 20milliseconds
Loop
 
Top