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]