Page 1 of 1

momentary switches

Posted: Sat Oct 29, 2011 10:02 pm
by allenf
Sorry I cant get my head round thistoday.
How can I use a momentary switch to flash an led on and off continuously until the switch is pressed again wherupon led stops. flashing. BUT also tohave a second momentary switch that will interupt the flash or off state and override it with a constant on state. then its in constant turn it back to flash.

Am i trying to make my code to complex or is there a simple way to do it..

I have tried the componaent macro call to read the switch high but it doesn`t matter whether the macro is looped or not it only cycles once

Someone please help

Manythanks
Allen

Re: momentary switches

Posted: Sun Oct 30, 2011 3:30 am
by medelec35
Hi Allen,
There may be a better way, but here is my version.

I have added switch de-bounce, and action is only taken when switch is released.
Pressing and releasing SW connected to port A0 Enables LED connected to port B0 to either flash or stay off.
Pressing and releasing SW connected to port A1 makes LED connected to port B0 either Solid or allows to flash, but only if flashing is enabled via SW connected to port A0
Also the simulation is very slow. LED will flash at about 1Hz on real hardware (untested as its a bit late. If I get time will test later)
If you want to speed up simulation, then you can place a call Timer0_Tick macro in main between the 20ms delay and bottom of the loop.
Don't forget to remove the call macro before compiling to hex or to chip.

Hope this helps, and I have not made it too complicated.

Martin

Re: momentary switches

Posted: Sun Oct 30, 2011 3:21 pm
by Spanish_dude
Hi Martin,

Just a couple of things to improve the program a bit.

- You forgot to initialize the value of the variables such as A0_Toggle, etc., so don't forget to do that because you don't know what value they have when starting the PIC.
- Your debounce code could be put into a macro. Take a byte as argument and return true or false to the Toggle variables.
- You can simplify the Toggle calculation by doing as follows : "A0_Toggle = (NOT A0_Toggle) & 1;

I think the "NOT" in Flowcode is the bitwise NOT (~).

Another way to do this but will use an if statement, is to use A0_Toggle as a counter. If the value exceeds or equals 2 then reset to 0.

Nicolas

Re: momentary switches

Posted: Sun Oct 30, 2011 6:00 pm
by medelec35
Hi Nicolas,
You are indeed correct about assigning variables at the start of program especially if read before wrote!
I don't to very late nights or early mornings very well :P
I also understand you could use one switch to assign one value and another to assign same variable with a different value.
Tbh, the toggling variable was the 1st method I thought of, so went with that, as tiredness was getting the better of me.
At least there are two options which is better than one :)
Thanks anyway

Martin

Re: momentary switches

Posted: Mon Oct 31, 2011 3:59 am
by allenf
hi medelec35.

Many thanks for that. Not quite what i was expecting (not quite what im looking for but i should be able to modify it from there to call macros for the led {i need two leds to flash alternately.conyinuously until switch 1 is pressed again And a second switch(2) that latches on a moment switch the unlatches on the second press})

I was going down the lines of macro`ing the led flash within a loop and banging an interupt in to takeit outof the loop and turn the macro off but it sort of doesnt work that way LOL.
would there be a way to call the macro with the first switch 1 press to jump out of a loop and call an OFF macro on the second switch 1 press.

chip is a 16f627a (cheapo that i have spare lol)