Using single input to control multiple outputs or actions.

johndimo

New Member
Hello all. I'm new to using picaxe microntrollers and looking to learn as much as I can about them to see what I can do with them. I have a strong background in electronics (13 years experience) but haven't done any programming since college back when I used assembly language and worked with the 68HC11 Motorola chips.

I can do small easy stuff no problems, but I would like to learn more complicated things. I've already built a simple single input, single output project for my kitchen and it works well.

What I'm trying to accomplish is to use a single button that can handle multiple output actions. So for instance, pressing once will do one thing, and pressing it again will do something else.

Here's the code I have now that I have been testing and it works well. But I'm not sure if it is an unorthodox or long way of doing what I want. If there is a simpler or less space wasting way of doing it, I'm all ears. I'm just looking to see if I'm on the right track or if I'm wasting precious space with unnecessary code.

Code:
init:
	symbol led = 2 ;set led as an output on pin 2
	symbol inp = pin3 ;set inp as an input on pin 3
	b1 = 0 ;initialize led state variable	

main:
	if inp = 1 AND b1 = 0 Then ;checks if button is pushed and if led state is off
		high led	;Turn on LED
		sertxd("LED should be ON",13,10) ;Send message to computer
		b1 = 1 ;Change led state to on
		gosub btnheld
	end if
	
	if inp = 1 AND b1 = 1 Then ;checks if button is pushed and if led state is on
		low led ;turn off led
		sertxd("LED should be OFF",13,10) ;send message to comp
		b1 = 0 ;Change led state of of
		gosub btnheld
	end if
	goto main

btnheld: 
	do loop while inp = 1 ;Do nothing until button is released
	pause 50 ;Small pause for debounce
	return
What this basically does is perform an action, only once per button press. Changing the action each time it is pushed. It's designed to simulate a rising edge on the button (meaning holding button down does nothing).

What I would like to get myself to is timed actions. Say, push 3 times within a specified time and do action 3 or push 4 times within a specified time and do action 4. Are there timing functions available that I can use? I'm working with an 08M at the moment.

John
 

Haku

Senior Member
Off the top of my head, without the timer function of the X2 chips I would approach this with using the count command.

My thinking goes along the lines of making the Picaxe wait for the first button press, then use the count command to count how many further presses of the button happen within the set time period of the count command (0 to 65 seconds when running at 4mhz).
 

Jeremy Leach

Senior Member
The way I've done this in the past is to take regular, rapid samples of the input state, with fixed pauses between samples. The idea is that the execution time of the code between the pauses is relatively insignificant, so the routine can keep track of the overall elapsed time (by counting the samples) and in parallel detect changes of the input state and keep a count of state changes.

Then when the elapsed time has reached a threshold determine what to do with IF statements or a SELECT CASE structure.
 

johndimo

New Member
Off the top of my head, without the timer function of the X2 chips I would approach this with using the count command.

My thinking goes along the lines of making the Picaxe wait for the first button press, then use the count command to count how many further presses of the button happen within the set time period of the count command (0 to 65 seconds when running at 4mhz).
Hmm. Never considered the count command. I went and looked it up in the manual to see how it works. It appears to be able to do exactly what I need.

Have you used this command with a mechanical button? The manual states to be careful as there may be debouncing issues. Nontheless, I'll play around with that command and see what I come up with. Thanks!
 

Dippy

Moderator
I'm sure a lot of people find the Button command useful.
I find it one of the most confusing commands ever duplicated from Stamp.
Basically, I haven't got a clue what it does, so I'd rather do it all myself icw a bit of cap debounce as suggested.
 

geoff07

Senior Member
Well it always avoids soldering. And if the logical cap isn't big enough changing a digit is easier than sucking the solder back off with one of those vacuum thingies!
 

Dippy

Moderator
A proper designer solders the correct value first time ;)

I'm not saying DO or DON'T.
I'm saying DO what is best for your App after you have tested and checked.
So many people put penny-pinching before reliability and quality. (No, I'm not talking about Ebay vendors).
 

johndimo

New Member
Lots of good suggestions, thanks folks.

I just looked up the button command. Looks confusing and I'm not sure it does what I'm looking for. Looks mainly for preventing debounce and to do a repeating count function while the button is pressed down. Some correct me if I'm wrong. A repeating count is not what I'm looking for.

Looks like from what I've read the COUNT command in combination with a SELECT CASE might be exactly what I'm looking for and simply using some kind of hardware debounce to prevent false reading while the COUNT command is doing its thing.
 

BeanieBots

Moderator
You could use the BUTTON command to count up a variable at a rate determined by the command parameters, but I think your idea of using COUNT and SELECT/CASE will probably be the most straightforward to implement.
 
Top