Toggle 2 o/p's from 1 i/p?

jbsmith1

New Member
Hi,
I'm trying to construct code to control a 12v bottle valve with a single momentary push button.
The solonoid valve has 3 wires, ground, open and closed, it only has to be powered momentarily to opperate open/closed as it latches.
I will use some logic mosfets to interface with the solonoid, that part I'm fine with.

I would like some help on code to toggle two o/p's high for 1/4 sec from a single momentarily high i/p, 1 press=Open, and a second press=Closed.

I have been reading the manuals and seen code to toggle a single o/p, and even code for an r/s flipflop, but non are quite right, and I can't seem to modify them to work for this.


At the moment I have no code to offer, as I don't know where to start.
 

techElder

Well-known member
Start with a plain English description of each step that you want to take. Describe only one operation per sentence.

That should make your program flow seem logical to you.

Then you can start figuring out your schematic. What pins/legs will you connect to the external transistor drivers? What pins/legs will be used for user input? How much power will the circuit require? Which PICAXE has the features/hardware available to solve the problem?

Then consider what might happen if the user doesn't do exactly what you expect them to. For instance, what happens if the user holds the button down instead of just pushing and letting go? What happens if they push 2 buttons at once?

THEN you might be ready to actually simulate some code in the Program Editor. Convert your plain English wording into Basic language statements. Work it all out there before you even insert one wire into anything. Correct your schematic as your simulated program evolves.

When your simulated program works as you want, then take the electronics and wire to the breadboard and find out why the hardware doesn't quite work as planned.

Repeat. (Have fun, too!)
 

BCJKiwi

Senior Member
Since the same input/switch is being used, an alternating output function needs to be created in code.

A simple method is to use a bit or byte variable to store either 0 or 1

When the button is pressed, check the variable,
if 0 set the variable to 1 and set output 1 high for the required time.
or
if not 0 set the variable to 0 and set output 0 high for the required time.

Best to use symbols for the outputs.
 

1968neil

Senior Member
The following code uses "Branching" ie: one push does one thing the next push does another.
Hopefully simple enough to get you started.
This will do one valve on then off, youll need to add the second valve yourself.

Regards
Neil

Code:
symbol ValveOn = 2
symbol ValveOFF = 1
symbol pbswitch = pin3     ' push button switch on pin3
symbol down = 0            ' define input state
symbol counter = b0

counter = 0                'set counter to 0 before starting


loop1:

if pbswitch = down then switchdown
branch counter, (ValveOpen,ValveClosed)
counter =0 
goto loop1

switchdown:
counter = counter +1
sdloop:
if pbswitch = down then sdloop
goto loop1

ValveOpen:
High ValveOn
pause 250
Low ValveOn
goto loop1

ValveClosed:
high ValveOFF
pause 250
low ValveOFF
goto loop1
 

jbsmith1

New Member
Thankyou very much for every ones help.
I'm one of them people that doesn't really learn from text books, but does from practical testing, so that bit of code is a great help Neil thankyou.:)
 

hippy

Ex-Staff (retired)
I'd do it as a simple linear state machine. Here 'Do:Loop Until' is used as 'wait until'. It could be turned into real code by simply adding ...

Symbol theButton = pin?
Symbol pushed = ? ' 1 or 0
Symbol notPushed = pushed ^ 1

and the two open and close routines

Code:
Do
  Do : Loop Until thebutton Is pushed
  Gosub open
  Do : Loop Until thebutton Is notpushed
  Do : Loop Until thebutton Is pushed
  Gosub close
  Do : Loop Until thebutton Is notpushed
Loop
 
Top