Basic switch code

newboy

New Member
Hi Guys
I have looked through endless topics on various switch/ pwm/ led programs and problems on this forum and it has so far helped me to write this basic code.
Being new to the whole programming/ picaxe thing i was wondering if someone would be able to look over my code and tell me if i am going the right way about things.
I am using a toggle switch for the input and the two outputs are linked to the shutdown pins on two led drivers.

I seem to get some sort of switch bounce depending on speed the switch is activated causing the program to jump steps.

Is there an alternative way of achieving the result i am after. I have looked at the button code but my programming is not that good and i did read that it does not cure all switch bounce.
Code:
main: 
		
		low 1
		low 2
		nap 4
		main1:
		if pin3 = 1 then goto flash
		goto main1				

Flash: 
		nap 2
		flash1:
		pwm 1,5,10
		pwm 2,3,10
		if pin3 = 1 then goto lowfull
		goto flash1:
		
		
lowfull:
		 
		high 1
		low 2
		nap 4
		cont1:
		if pin3 = 1 then goto fullon
		goto cont1
		
fullon:
		high 1
		high 2
		nap 4
		fullon1:
		if pin3 = 1 then goto main
		goto fullon1
many thanks:):)
 
Last edited:

leftyretro

New Member
Switch contact bounce is a common problem with mechanical switches.

One can deal with it with external filtering components or with software using delays & retesting or the PICAXE has a 'BUTTON' command to help automate switch debouncing.

Lefty
 

newboy

New Member
Thanks for your reply Lefty.
I was not to sure of the programming side as i have not programmed before so was hoping the forum would be able to help me.
If this fails i was the going to add a cap to the switch.
:)
 

newboy

New Member
Can anyone please have a look at my code and just let me know if i am going the correct way about programming my 08m chip for my project, or is there a beter way.

Code:
main: 
		
		low 1
		low 2
		nap 4
		main1:
		if pin3 = 1 then goto flash
		goto main1				

Flash: 
		nap 2
		flash1:
		pwm 1,5,10
		pwm 2,3,10
		if pin3 = 1 then goto lowfull
		goto flash1:
		
		
lowfull:
		 
		high 1
		low 2
		nap 4
		cont1:
		if pin3 = 1 then goto fullon
		goto cont1
		
fullon:
		high 1
		high 2
		nap 4
		fullon1:
		if pin3 = 1 then goto main
		goto fullon1
Many thanks:):):)
 

tikeda

Member
Quick questions:
1) Why are you using the 'nap' command rather than the 'pause' command? I also notice that your longest nap period (4) is about 0.3 seconds. The program will make it through every step in less than one second after you set the pin high.

2) What sort of switch are you using and how is it wired? Many digital inputs 'want' to see either high or low voltages. Floating inputs, which happen when the pins are connected to nothing at all rather than a high or low voltage, can produce odd behavior in some chips. For example, if you are using a double-pole, single-throw switch to toggle between high and low voltage connections, you'll find that there is often an intermediate state where neither line is connected to the pin.

To ensure that the pin sees either 'high' or 'low' signals, you can attach the pin to one of the power rails through a resistor (10K ohms, for example). You also attach your switch between the same pin and the opposite power rail. If the switch is off (i.e. doesn't close the connection between the pin and the power rail), the pin sees the voltage of the rail connected through the resistor. If the switch turns on, the low resistance path through the switch allows almost the entire voltage of the switched power rail to reach the input pin. A little bit of current flows on through the resistor which is why you choose a resistor value that limits the current to a small amount (0.5mA for the size I suggested). At no point is the pin left in an floating condition.

Note for the paranoid: If you accidentally change the pin to an output and happen to close the switch, it's possible to damage a chip (Maybe not the Pixaxe, but some chips are fragile). For example, if the switch is connected to the 5V source and you accidently set the pin to output 0V, you've got a 'short' condition and the chip might burn out while trying to pass unlimited current. To prevent this, you can add a second resistor to the switch circuit between the switch and its power rail (say 500-1K ohms - roughly a tenth of the pull-up or pull-down resistor attached through the other rail). That limits the current through the chip to about 5-10 mA during an accidental shorting condition.

3) In the 'Flash' section of the code, I'm not sure what you're trying to achieve but it looks a bit funky. Maybe you could provide an outline of how you want the LED drivers to behave in each section of the program.
 
Last edited:

newboy

New Member
Hi tikeda

Thanks for replying to me, sorry i have not posted sooner.

Firstly I am trying to drive two high powered LEDs via driver boards using the PWM /TTL input on the drivers.

Step 1 all off
Step 2 both leds flashing (at full power or reduced)
Step 3 one led on at half power
step 4 both on at half power
step 5 both on at full power.

The steps above are the program i want to end up with. At the moment i only have the following

Step 1 all off
Step 2 both leds flashing full power
Step 3 one led on full power
Step 4 both leds on full power.

I put the NAP command in because when i activated the toggle switch i found the program jumped commands missing steps.
It helped but not that effective depending on speed of activation of the toggle switch.

I am using a single-pole double-throw toggle switch and have one side connected to 5v and the other to the input pin of the picaxe chip with a 10k resistor to 0v.
If i understand you correctly i should switch 0v and have the 10k resistor connected to the input pin and 5v so there is always some voltage on the input pin of the picaxe chip.

Opps, just re-read your reply and i have the switch connected correctly with a pull down resistor to 0v.


Many thanks
 
Last edited:

moxhamj

New Member
Your code looks fine. The easy way around contact bounce is to put a pause after every change. Eg if you are in a loop waiting for a switch to go high then as soon as it does, do a pause 100 = 0.1 seconds. I think you have already found that and put the nap command in. How fast is the user going to be changing the switch?
 

tikeda

Member
The cycling through the steps seems pretty quick. To trap the program at any one of the steps, you've got about a quarter of a second to flip the switch. Less, actually, because it takes time to recognize when to throw the switch.

When you mentioned the program 'jumping commands' while toggling the switch, is that because you are flipping the switch on and off quickly to move between sequential steps? I could see how switch bouncing or even a slow reaction time might create a skip, particularly over the 'Flash:' step which has the shortest delay. Try extending the pauses to something ridiculously long (e.g. PAUSE 1000), seeing if that corrects the problem, and then reducing the values to a point where you see skipping again. That will show where the safe-zone is for reliable switching.
Here's the program in pseudo-code...
Code:
main:
	mess with LED pins
	nap 4 (288 ms delay)
	check pin3, loop here if pin3 = 0	
Flash: 
	nap 2 (72 ms delay)
	check pin3, loop and refresh pwm if pin3 = 0		
lowfull:
	mess with LED pins
	nap 4 (288 ms delay)
	check pin3, loop here if pin3 = 0
fullon:
	mess with LED pins
	nap 4 (288 ms delay)
	check pin3, loop here if pin3 = 0
		otherwise return to main: (top)
An alternative is to shift states with every transition of the switch (off->on and on->off) instead of cycling the switch on & off to move to the next state. Something like this:

Code:
state_1:
	mess with LED pins
	PAUSE to debounce switch
	check pin3, loop here if pin3 = 0	
state_2: 
	PAUSE to debounce switch
	check pin3, loop and refresh pwm if pin3 = 1		
state_3:
	mess with LED pins
	PAUSE to debounce switch
	check pin3, loop here if pin3 = 0
state_4:
	mess with LED pins
	PAUSE to debounce switch
	check pin3, loop here if pin3 = 1
		otherwise return to state_1: (top)
 
Top