Mom. switch to toggle 2 servo positions

Dangre

New Member
I'm sure this has been done a hundred times but I cannot find the suitable code searching the forums. I want to use a simple momentary switch to toggle between two set servo positions. I have my code working fine moving my servo to any position (pulse) I code but now need to add the switch. I.E. press switch and it goes to position A, press switch again and it goes to position B, press switch again and it goes back to potion A .....etc. Thanks in advance!!!:rolleyes:
Edit: Using 8M2
 
Last edited:

Dangre

New Member
2 functions, 1 Mom. switch (long press)

Here is another simple one for the experts. I have a simple momentary switch that I want to have one function for a quick press and another function if the same switch is pressed for 3 seconds.:cool:
Edit: Using 8M2
 
Last edited:

westaust55

Moderator
Have you tried a search of the forum on this subject?
A forum search with the simple search facility at top right of the forum pages found in ~2 pages using "long" and first entry using "pushes" (sometimes pays to consider alternate words), the following thread on the same concept:
http://www.picaxeforum.co.uk/showthread.php?22277-detecting-long-button-pushes


EDIT:
Having answered what is now post 2 above, I found your second thread on what is seemingly the same topic and have merged them into 1 thread.
 
Last edited:

Dangre

New Member
Actually, these are two separate requests. For the long press, I will look at the codes that are mentioned and give them a try (any of thoose methods recommended over the others?[interrupts vs. timers?]).

My other request (the one with the subject on this thread) stills remains unanswered for me. I need to switch the position of a servo from position A to position B to A to B to A.... by just by pressing (and re-pressing) a momentary switch (only using short presses). If I was just turning on and off a LED I could use the toggle command but what I want to do (I guess) is to toggle between two constants with the same switch. I have some ideas which I will try but was hoping for a more elegant solution than the one I have in mind. I will give my code a try and if it works I will re-post.
 

SD70M

Senior Member
You mean like this?

08M2 Servo Control.jpg
Code:
init:
	symbol dirFlag = b1  ' direction flag
	dirFlag = 0
	symbol minTrav = 100  ' minimum servo travel
	symbol maxTrav = 200  ' maximum servo travel
	servo 2, minTrav  ' initialise servo position to minimum

main:
	b0 = pin1  ' get button state
	
	if b0 = 1 and dirFlag = 0 then  ' button pressed, servo at min travel
		servopos 2, maxTrav	' move to max travel
		dirFlag = 1      ' set direction flag
	elseif b0 = 1 and dirFlag = 1 then  ' button pressed, servo at max travel
		servopos 2, minTrav	' move to min travel
		dirFlag = 0      ' set direction flag
	endif
	
	do while b0 = 1 ' Debouncing loop
		b0 = pin1
	loop
	
	goto main
 

Dangre

New Member
SD,
That looks like exactly what I was looking for!:D Again, sorry for if this was posted before, I just could not find it from my forum search. Just curious, what program did you use for your schematic?
 

SD70M

Senior Member
I've got Picaxe VSM, which is the one I used but I have numerous schematic apps.

Picaxe VSM is the only one I have that does the simulation for me.

Angie
 

westaust55

Moderator
To achieve a toggling flag for determining servo position you can use the math XOR function.

Let's say you have the switch on pin B.1
Then
Code:
Symbol switch1 = pinB.1
Symbol posflag=bit0 ; remember bit 0 is part of b0 so don't use b0 elsewhere

If switch1 = 1 then
  Posflag = posflag XOR 1
Endif
Then you can set the servo position base on the value of the variable "posflag"

The above could be part of an interrupt subroutine so you do not miss a switch press and the main program loop can then check posflag from time to time.
 

Dangre

New Member
That's mom., short for momentary. Being old enough to be a grandfather, I don't rely on my Mom anymore. For those interested in the details, it is just a simple R.R. (railroad) switch machine actuator. I'll give the before-mentioned code a whirl and report back (I seem to have misplaced my servo test apparatus). Too many projects....
 
Top