Newbie needs help with first program

chipset

Senior Member
Hi Im a very green newbie and Ive just started trying to do programming so bear with me. I have a good understanding of hardware but the programming is a whole new world that I havent quite got the hang of yet.

Im using an 08m to trigger three transitors that in turn control three relays. I have a Push to make button as a trigger and I need it to do as follows:

STEP 1
Press button

debounce the button

output0 goes hi

STEP 2
press button a second time

debounce the button

output0 stays hi and output1 goes hi

STEP 3

press and HOLD the button a third time

output0 turns off output1 stays hi and output2 goes hi

as soon as the button is released output1 goes hi output1 stays hi and output2 turns off

STEP 4

press the button

debounce the button

all pins turn off

Also if you hold the button for longer than 2 seconds during Steps 1 or 2, I want the program to automatically start Step 3.

Additionally if you press the button twice within 1 second (double click) during steps 1 or 2 I want it to turn off as well.

Now being a newbie this program is hurting my head trying to figure it out and I was hoping maybe someone could help me out with this cause Im going nowhere fast. Other than code for debouncing the button I dont know what else to cut and paste in order to get this going.
 
Last edited:

chipset

Senior Member
i did some of the walk through ones in the manual but more or less Ive never written a line of code myself. If I look at someones code I understand what it means but I dont know how to write it myself. The best way I can explain it is that Im like someone that can understand a foreign language on a basic level but I cant speak it. Id be the person reading phrases out of a translation book.
 

chipset

Senior Member
maybe someone can direct me to a tutorial site so I can begin to learn this? I dont know where to start and I figured this project wouldnt be very complicated
 

lbenson

Senior Member
Here is a first pass at the basic outline of what you have laid out.
Code:
#picaxe 08m

symbol buttonPin = pin3

symbol buttonPressCount = b13

dirs = %10111 ' pin 4=output;3=input;2,1,0=output

main:
  if buttonPin = 1 then
    pause 10   ' a little debounce time--increase or decrease as appropriate
    inc buttonPressCount
    if buttonPressCount = 1 then
      high output0
    else if buttonPressCount = 2 then ' do something else
      high output1
    else if buttonPressCount = 3 then ' do something else
    else if buttonPressCount > 3 then 
      low output0
      low output1
      low output2
      buttonPressCount = 0
    endif
  endif
  do while buttonPin = 1 ' wait until it goes off
     pause 1000          ' wait a second
  loop
  goto main
You can run this in the simulator in the program editor by pressing the "Simulate" button. Close the "Memory" and "Serial Output Buffer" windows. You can see that your program is looping waiting for a button press. Click on pin 3 to turn on the first output. This may not be altogether what you intend, but the program loops until you click on pin 3 again, effectively releasing the pushbutton. Click again and the second output goes on. Fill in the rest of the code to fit your requirements. Note that in the Simulation window, you can break the program execution by clicking the "||" icon, and then you can resume by clicking ">" or single-step by clicking "}".

If you're not certain of the meaning of a command, check it out in Manual 2 (available under "Help"). Have fun.
 

westaust55

Moderator
Hi Chipset,

Welcome to the PICAXE forum.

The "tutorial" is in reality the entire set of PICAXE manuals. MAnual 2 covers all the commands with brief examples.

What you have written at post 1 is what is often known as pseudo code.
Written in plain english from which the program can be then written.

There is a BUTTON command which includes switch debounce.
It so happens that a further explanation has been written on this particular (more complex) command by BeanieBots (from memory) and some of that has since been added into the manual.
There are potentially simpler ways to check for a switch closure and when it is released. lbenson has given you one such an example.

It can come down to reading the manual and understanding the funciton of the many BASIC commands.

Try things for yourself and ask specific questions where you are stuck.

Is this just a home project or part of your school work projects?
 

chipset

Senior Member
Thanks guys, Im 28 and well out of school. I was hurt in a bad motorcycle accident and havent been able to live a regular life so I have tried to find something new to do that doesnt require any physical effort but keeps me busy mentally. More or less Im just doing this for fun and to learn something new.

My problem is knowing how to link all the commands together and which commands to use. The big issue with searching is knowing what to search for otherwise Id copy what other people have done and figure out why they did what they did.

Ill look into the peoples posts you referenced and go from there.

I hate being a beginner cause everything is complex no matter how simple it actually is, hopefully Ill be able to move past this stage sooner rather than later.
 

westaust55

Moderator
For your input swiitch, have a look at PICAXE Manual 3 page 25.

The top diagram witht he resistor to 0V and switch to 5V is the more common configuration.

Then as the words above the diagram say:
when switch is open: if pin0 = 0
when switch is closed: if pin0 = 1

Can then use a command like
IF pin0 = 1 THEN . . . . do what you want when the switch is closed/pressed

and after a delay

IF pin0 = 0 THEN . . . . the switch has been released

you can build this into a DO...LOOP command to wait until the switch is activated or released.

Code:
DO
Loop Until pin1 = 1 ; wait until switch pressed
pause 100            ; pause for debouce
DO
Loop Until pin1 = 0 ; wait until switch released
 
Last edited:

chipset

Senior Member
ah ha thanks. I was wondering how to tell it to start doing something else only after the switch has been released.

I can make the outputs turn on and off I understand that. What Ive been having trouble with is how to tell it to move to the next set of commands only after the switch has gone hi, debounced, then went low.

should I be using the button press counter or is there a simpler way of doing this? I didnt know if I should make each step a sub routine or not. It seemed easy to understand if I did it that way.

I can make 4 routines

sub1:
high 0

sub2:
high 0
high 1

sub3:
low 0
high 1
high 2

sub 4:
low 0
low 1
low 2

Now how I make that work based on button presses I dunno. Im trying to figure out the loops and when to put an endif ect. The syntax box pops up as I experiment and I just try to fix it as I go. That doesnt mean itll work right but thats the only way Ive been able to get anything working at all lol
 

chipset

Senior Member
ok I came up with this and its almost perfect,

main:
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
high 0
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
high 1
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
low 0
high 2
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
high 0
low 2
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
low 0
low 1

goto main

Now how do I make it so that when 2 goes high, it only stays high while the button is pressed?

I want everything else to stay on after the button is pressed and released except for that part of the routine. If I can get that part figured out Im done!!! Thanks so much for the help so far!!
 

chipset

Senior Member
never mind I GOT IT!!!

main:
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
high 0
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
high 1
DO
Loop Until pin3 = 1 ; wait until switch pressed
low 0
high 2
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
high 0
low 2
DO
Loop Until pin3 = 1 ; wait until switch pressed
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
low 0
low 1

goto main

now to figure out the extra stuff about jumping to different spots of the code and turning it off if you double click the button.

Otherwise Im really happy right now. Thanks so much for the help
 

chipset

Senior Member
ok I getting syntax errors trying to add in a new part of the code

during the first loop I want to be able to hold the button for a set amount of time and it goes to the 3rd loop. I was trying to write it like this:

if pin3 = 1 >2000 then
low 1
high 2
high 3

I think it has to do with the >2000 part.

Also I have a feeling that if I add it to the first loop that it would go to the second loop afterwards and thats not what I want I just want it to goto another part of the code. Should I redo the code to goto sub for each section and make a sub routine for each loop?

Also I want to have it so that if pin3 = 1 twice in 500 then it resets.

Im looking at the manual in the if,then,else section and Im not quite getting it.
 

westaust55

Moderator
firstly, look at this slightly modified code:
red for new and green for removed code
Code:
main:
DO
Loop Until pin3 = 1 ; wait until switch pressed 1st time
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
high 0
DO
Loop Until pin3 = 1 ; wait until switch pressed 2nd time
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
high 1
DO
Loop Until pin3 = 1 ; wait until switch pressed 3rd time
pause 100 ; pause for debouce

[COLOR="Red"]LOW 0
HIGH 2[/COLOR]


DO
Loop Until pin3 = 0 ; wait until switch released
[COLOR="Green"]; low 0
; high 2[/COLOR]

[COLOR="Red"]HIGH 1
LOW 2[/COLOR]

[COLOR="Green"]; from post 1 definition, do not need the next 6 lines

; DO
; Loop Until pin3 = 1 ; wait until switch pressed 4th time
; pause 100 ; pause for debouce
; DO
; Loop Until pin3 = 0 ; wait until switch released
; high 0
; low 2[/COLOR]

DO
Loop Until pin3 = 1 ; wait until switch pressed 5th time
pause 100 ; pause for debouce
DO
Loop Until pin3 = 0 ; wait until switch released
low 0
low 1

goto main
secondly, you can use the markers
[code] and [/code]​
around your program code so it appears in a sub window.

Next you need to think about how you can act if a key is pressed for more than 2 seconds.
This can be done by introducing a small pause say 100 milliseconds (0.1 sec) and counter ( 0.1 x 20 = 2 seconds) within a
DO...Loop structure to test it the 2 seconds has expired.
This can become a second reason/test to loop until button is released or time is expired.

Have a look at the PAUSE comamnd and then the variable maths (eg b0 = b0 + 1) around (from memory) page 19 of Manual 2

To jump to another location a test first like IF... THEN (labelname)
and add a label at the point you want to goto (labelname:) - = has the colon ( : ) after the name
 
Last edited:

Rickharris

Senior Member
For essentially simple tasks you only need to worry about 8 commands:

High and Low - turn outputs on and off
Wait and Pause - The microprocessor operates so fast (about 10,000 instructions a second) that it can be too fast - wait allows a do noting period.
Goto - Gosub - These allow you to jump around in the programme so you don't have to have all the instructions in a linear order. Goto - simple jumps to a label - Gosub will jump to a label but at the end of that part (return) will jump back where it cam from.


If you get very excited you can add to these 6 commands IF / Then to read your inputs.

and you can add For / Next to do a loop of commands a set number of times.


For 99% of simple tasks these commands will do the job - the rest of the 38+ command set is refining - doing things differently or entering areas you are not ready for yet!


Study manual 2 for how to use these command and start with a simple switch and LED set up - when you can turn on the LEd by pressing the switch you can replace the switch with your transistor.

Have fun. It takes my students 4 or 5 hours to get a grip of the above and then it gets easy.

Nothing to do with your problem:

Code:
Start: [COLOR=Lime]' a label[/COLOR]

IF pin 3=1 then flashLED   [COLOR=Lime]' checks input pin 3[/COLOR]

goto start  [COLOR=Lime]' if input not on go back to start to check again[/COLOR]

flashLED:   ' a label

for b0=1 to 10 [COLOR=Lime] ' set up a loop to turn LED on 10 times.[/COLOR]
high 4   [COLOR=Lime]' turn led on[/COLOR]
wait 1   [COLOR=Lime]' wait 1 second[/COLOR]
low 4   [COLOR=Lime]' turn it off[/COLOR]
wait 1   [COLOR=Lime]' wait 1 second[/COLOR]
next b0  [COLOR=Lime]' check if done 10 times if not go back to flashLED[/COLOR]


goto start  ' go back to start to do it all again.
 
Last edited:

chipset

Senior Member
Thanks guys. First I may not have been as specific as I needed to be in my first post but the code I have at this point does exactly what I want it to. The project is a digital ignition switch for a car. output0 = the accessory circuit of a car. This is what would be the first position of an igniton switch or the circuit that works when you twist the key back from the off position to listen to the radio. Output1 = the IGNITION circuit, this powers up the engine components and would be the run position of the ignition switch. Output2 = Starter circuit. The way I wanted it to work is that you press the button one time and the ACC turns on. Hit it again and IGN powers up and ACC stays on. Press and Hold the button a third time and this activates the STARTER but only as long as you hold the button, as soon as you let the button go it turns the STARTER off. During the Start cycle you dont want the ACC on cause it saps too much power off the battery which is needed for cranking. IGN needs to stay on though or the ignition coil and other related parts would turn off as well and the motor wouldnt start cause they were turned off while it was cranking. After the Start it reverts back to having IGN and ACC on as you drive around. When you want to turn the car off hit the button again and the car turns off.

The varibles I wanted were if you just wanted to hop in and fire the motor off you could turn the car on with the first press of the button then if you held the button for more than 2 secs it would go to the Start mode where you keep holding the button until the engine starts.

The other varible was a safety feature so that you could turn the car off at anytime. I figured a quick double click would be best cause in an emergency people would be hitting the button as fast as they could multiple times (I know I have).

I understand the goto function now and with the right code to differentiate time between a normal button press, a long button press and a double click, I can have it goto different parts of the code.

What Im not getting is the syntax necessary to make it do that??? You guys have been giving me just enough help so I can learn it myself and I appreciate that as I dont like being spoonfed but as a beginner I need someone to hold my hand so that I dont twist my brain in knots trying to figure out which command to do.

I was looking at the Variable math part and Im confused as to what b1 or w1 are? like b1= 1 + 3. Is b just like x or y in algebra and you just substitute pin3 for b? As you can tell the manual isnt as clear to me right now so I dont understand what Im reading and it becomes almost worthless lol. Either way thanks for all the help.
 

SilentScreamer

Senior Member
b0 is a byte variable, that is stored in RAM. It is just a number between 0 and 255. To better explain this might help.

Remember the number "0" in location 0.
Go make a cup of tea.
Add "5" to the number that you've remembered in location 0.

This is in effect what variables are used for. It allows the PICAXE to remember a number "in the back of its head" while it does something else.

w0 is a 16 bit number so you can store a number between 0 and 65535.
 

chipset

Senior Member
ok at this point Im trying to get the code to read pin3 = 1 for different periods of time. I cant figure out how to do it correctly. Heres what I was doing. in acc: i cant get it to read the loop for 2000. It is waiting to see pin3 = o to move on. If I switch the code around then it pauses for 2 seconds no matter what. I want it to run normally and in the event you press the button for 2 seconds then it goes to starter.

Code:
main:
do 
loop until pin3 = 1
pause 100
do 
loop until pin3 = 0
goto ACC

acc:
high 0
DO
Loop Until pin3 = 1 ; wait until switch pressed 1st time
do
loop until pin3 = 0
goto IGN
do
loop until pin3 = 1
pause 2000 
goto STARTER

ign:
high 1
do
loop until pin3 = 1
goto starter

starter:
low 0
high 1
high 2
do
loop until pin3 = 0
if pin3 = 0 then goto RUN

run:
high 0
low 2
do
loop until pin3 = 1
pause 100
do 
loop until pin3 = 0
low 0
low 1
goto main
hopefully someone can see what I can do
 

chipset

Senior Member
ok Im looking at the else if commands and I think this is the command I need to use but I have no freakin clue how to write it out

IF pin3 is high for less than 2000 THEN goto ign ELSE goto starter ENDIF???

I think thats kind of how it should be written but Im totally lost.

How would I write that out and still include the loops and the debounce function? Also what should I be doing for the double click routine? These are kinda tough to understand and they are the big stumbling blocks for me right now
 

lbenson

Senior Member
In the code example I provided, you can put a counter in the loop which pauses for one second. Then test to see which state you are in (for example, buttonPressCount = 3) and act accordingly. Set "symbol secondCount = b12" at the top.

Code:
  secondCount = 0
  do while buttonPin = 1 ' wait until it goes off
     pause 1000          ' wait a second
     inc secondCount
     if secondCount = 2 and buttonPressCount = 3 then
                         ' do something
     endif
  loop
You have only a single test to see if the button has been pressed, and you are put into a "state" by the code after the button press detection--state "buttonPressCount=1" means one thing, "buttonPressCount=2" means another. This state cannot change until the button has been released. The loop for one second allows you to see how long the button remains depressed, and tests for how long and which state you are in can allow you to take further actions.
 

SilentScreamer

Senior Member
Not the best way I expect but this will do it.

Code:
if pin3=1 then				'Is pin3 high?
	for b0 = 1 to 20			'Run this loop 20 times to make sure the button is held down
		pause 100			'Pause 0.1 seconds
		if pin3=0 then ing	'If the button has been released then goto ing
	next					'Loop round (part of for b0 = 1 to 20)
	goto starter			'If the button has been held down for 2 seonds then goto starter
endif
goto ing					'goto ing
 

BCJKiwi

Senior Member
Or, following your original logic (untested),
Code:
b0 = 0              ' make sure b0 = 0
do until pin3 = 1 and b0 => 20   'check both pin status and counter - exit loop after 20, 0.1 sec loops
   pause 100
   b0 = b0+1        ' count number of loops of 0.1sec
loop                ' go round again
if b0 => 20 then    ' OK loop finished (either button released or 2 secs reached) - only go to starter if 2 secs reached
  let b0=0          ' tidy up b0
  goto STARTER
endif
 
Last edited:

chipset

Senior Member
thanks guys!! Silent Screamers code works just fine, I cant tell anything other than it works and it does.

Question can I modify this portion of code to do the double click part or should I look into a different code? Im still trying to figure out what this part of the code means but it works and Im sure Ill pick it up sooner or later.

Otherwise thanks a ton!!
 

BCJKiwi

Senior Member
Best to use a separate block of code.

Use SS's code as a basis since you have that working.
The difference is that a loop within a loop will be needed,
do the first loop, set a counter,
do the second loop
did you get get an on, off , on, off in a suitable time frame?

If so, shut down.
 

chipset

Senior Member
ok I think Im understanding the code to hold the button for 2 seconds. If I want it to go into a loop if pin3 has gone high twice in 1 second can I make a variation of
Code:
Do 
loop until pin3=1 
pause 100
do 
loop until pin3=0
do 
loop until pin3=1
pause 100
do 
loop until pin3=0
heres what I have so far and it works great just need to figure out the double click goto main routine.


Code:
main:
do 
loop until pin3 = 1
pause 100
do 
loop until pin3 = 0
goto ACC

acc:
high 0
do 
loop until pin3 = 1
pause 100
if pin3=1 then				'Is pin3 high?
	for b0 = 1 to 20			'Run this loop 20 times to make sure the button is held down
		pause 100			'Pause 0.1 seconds
		if pin3=0 then ign	'If the button has been released then goto ing
	next					'Loop round (part of for b0 = 1 to 20)
	goto starter			'If the button has been held down for 2 seonds then goto starter
endif
goto ign		

ign:
high 1
DO
Loop Until pin3 = 1 ; wait until switch pressed
goto starter



starter:
low 0
high 1
high 2
do
loop until pin3 = 0
if pin3 = 0 then goto RUN

run:
high 0
low 2
do
loop until pin3 = 1
pause 100
do 
loop until pin3 = 0
low 0
low 1
goto main
That would make a loop that looked for the switch being pressed twice. The magic ingredient I suppose would be to only do this if it happened within 1 second. Im understanding the loops now and they look to be very handy for stepping through a sequence.

I ve looked at SS's code and I understand that it wants to loop 20 times and if pin3 is still 1 then execute STARTER. What Im dont understand is how do I modify this to look for the code I have above?

Im sure theres a simple way to do this but as you guys can tell Im not that good at this. The good thing is I have enough here to do quite a few other simple projects. The Do Loops are great for simulating a decade counter and by adding in the pause command you can emulate a flip flop circuit for debouncing. Its all pretty damn cool I hope as I ask questions and get help I can build up my own knowledge base to this so that I can make even cooler projects in the future.
 
Last edited:

lbenson

Senior Member
Some of your difficulties, it seems to me, come from trying to make a single input switch act like what in the real world are different switches (even if in the case of a car ignition switch they are within a single component). You might do better to choose a bigger chip, for example the 14M, which will allow you to have as many inputs as you need.

You don't need to actually have the chip in your possession to get the software to work--you can check it all out in the simulator.
 

chipset

Senior Member
well thats not the point of this though. All I need is 3 outputs and one input. I could make the same thing with a schmitt trigger and a decade counter (I already have) I just wanted to do it with a PIC. The first set of code that is done already does what my hardware based system does with the added benefit of having less parts and a programmable debounce option. Then with help from you guys I made it so it would jump to another part of the program with a long button press. Now Im trying to make it turn off at any point by double clicking it. At each point of this project over the last day I have learned a ton about this from where I had no idea what I was doing to where now I can get this to do quite a few things. Im just trying to add something new to it and in the process learn some new commands.
 

BCJKiwi

Senior Member
A loop around this code block is needed as you need to time each phase else it can sit there forever after one press and the next press would be seen as a double click even if an hour later!

Code:
Do 
loop until pin3=1 
pause 100
do 
loop until pin3=0
do 
loop until pin3=1
pause 100
do 
loop until pin3=0
To sort out this type of logic issue, it be helpful to hand sketch a simple flow chart.

In each block, think about what can happen if things don't occur in the intended manner. It is simple to test that it works when things occur as you intend and think your'e done - the trick is to think of the alternatives, like the double click insn't - like when it is two separate clicks.
 

chipset

Senior Member
is there a code snipet for a double click? Im sure if I could find something I could work it into my code. The last part about holding the button for 2 secs was over my head... this is out of my league. Truthfully I dont even know where to start.

Ive seen so many different way code is written just in this thread that I dont know what to make of it all. One of the more confusing things is that different people write their code in different ways and its hard to make heads and tails of it all.

If worse comes to worst and noone can help me with this part is there maybe a workbook type website or something that gives you real world exaples of different commands? Reason I say that is that its one thing to have the manual tell you in very technical terms what each comand does and its another thing to know how to actually implement it. I hate to bother people that are well past my skill level and thats why I was hoping there was somewhere I could go to build my skills and understanding of this since I can tell it is really powerful even in its simplicity.
 

lbenson

Senior Member
Well, what is a double-click? Part of the issue is the question of how fine-grained your timing needs to be. In Windows, a control is available to allow each user to individually set timings like double-click time.

In any case it will be hard to escape counting the number of time periods and checking the intervals between events (button-on, button-off, button-on). Something like this should work--but if made generic you'll have to deal with timing overflows--tough in 08M codespace with limited number of registers.

Code:
#picaxe 08M
symbol timePeriodCount   = b13 ' i.e., count of 100ms loops; watch for overflow
symbol buttonOnTime      = b12
symbol buttonOffTime     = b11
symbol buttonOnDuration  = b10
symbol buttonOffDuration = b9

' bit value symbol definitions--note: uses b0
symbol currentButtonPinState = bit0
symbol lastButtonPinState    = bit1

symbol timePeriod = 100     ' 100 milliseconds
symbol doubleclickInterval = 5 ' say 2 clicks within 1/2 second

symbol buttonPin = pin3

dirs = %10111 ' pin 4=output;3=input;2,1,0=output

main:
  currentButtonPinState = buttonPin
  if currentButtonPinState <> lastButtonPinState then ' new button event 
    if currentButtonPinState = 1 then ' this was a button press
      buttonOnTime = timePeriodCount
      buttonOffDuration = buttonOnTime - buttonOffTime 
      if buttonOffDuration <= doubleclickInterval then ' a double-click
        high output4
      else      ' not a double-click
                ' do something else
        low output4
      endif
      
                                     ' do something now
    else   ' this was a button release
      buttonOffTime = timePeriodCount 
                                     ' do something else
    endif
  endif
  inc timePeriodCount 
  pause timePeriod
  lastButtonPinState = currentButtonPinState 
  goto main
The code basically does 100 millisecond loops and within each loop checks for a change of state of one input--either a transition of the button from off to on or from on to off. Which transition it was is determined and how long it was in the previous state (how long pressed or how long not pressed). As an example of what your code can do it turns on output4 if the button was pressed and released twice within 500 milliseconds.

This code doesn't account for overflow of timePeriodCount, which with a 1-byte counter and a 100ms time period, happens every 25.5 seconds (100ms*255). It also doesn't treat triple-click differently from double-click. Note that output4 is used as an indication of a double-click occurance.

Your other code--and all of your code--would go within this loop, or in subroutines called from this loop, which provides the timing for everything. You would need buttonPressCount and other indicators to determine in what "state" you are in within this timed "state machine". This is a generic way of doing the kind of thing you want to do. Getting your head around this kind of "event-driven" coding is tricky, but it will give you the most flexibility in the long run if your code is timing-based.

This will run in the simulator.
 
Last edited:

chipset

Senior Member
thank you very much. I will probably be starting at this for a few days until it makes sense to me but now I can go back and forth between the manual and this and figure out why you did certain things. Ill get back to you guys with the results. Thanks again!!
 

chipset

Senior Member
I know this is an old post but I had some medical issues and have been out of it for quite a while. As such I kinda lost some of my programming knowledge. Here the code again...

Code:
main:						;label
low 0 					;turn of ACC
low 1
do 						;start loop
loop until pin3 = 1			;loop until button is pressed
pause 100					;wait for debounce
do 						;start loop
loop until pin3 = 0			;loop until button is let go
goto ACC					;go to ACC step
acc:						;label
high 0					;turn on ACC relay
do 						;start loop
loop until pin3 = 1			;loop until button is press
pause 100					;wait for debounce
if pin3=1 then				'Is pin3 high?
	for b0 = 1 to 5			'Run this loop to make sure the button is held down
		pause 100			'Pause 0.1 seconds
		if pin3=0 then main 	'If the button has been released then goto main and start over
	next					'Loop round (part of for b0 = 1 to 20)
	goto preign				'If the button has been held down then goto preign
endif
goto main		
preign:
do loop until pin3=0
goto ign

ign:
high 1
do 
loop until pin3=1
if pin3=1 then				'Is pin3 high?
	for b0 = 1 to 5			'Run this loop 20 times to make sure the button is held down
		pause 100			'Pause 0.1 seconds
		if pin3=0 then main 	'If the button has been released then goto ing
	next					'Loop round (part of for b0 = 1 to 20)
	goto starter		'If the button has been held down for 2 seonds then goto starter
endif
goto starter
starter:
low 0
high 1
high 2
do
loop until pin3 = 0
if pin3 = 0 then goto RUN
run:
high 0
low 2
do
loop until pin3 = 1
pause 100
do 
loop until pin3 = 0
low 0
low 1
goto main
What Im trying to do is go to START from MAIN if the button is held for 3.5 seconds. I hate asking what to do but Im so rusty and was a newbie anyways that after a0ll the time in the hospital I pretty much forgot what I need to do.

I think I need to do a "do loop" and if it exceeds 3500 while pin 3=1 then goto START not sure if it should go in main or preign though.

Hopefully someone can play the oilcan for my rusty brain as Id like to add this to the program.

Thanks.
 
Last edited:

westaust55

Moderator
You need to use the EXIT command within a For Next loop

See PICAXE manual 2 page 52

Code:
SYMBOL  time = w6 
SYMBOL buttonPin = pin3

Main:
	IF buttonPin = 1 THEN
		FOR time = 1 TO 3500
			PAUSE 1
			IF buttonPin = 0 THEN EXIT
		NEXT time
		IF time > 3500 THEN  ; . . .do or goto the action when > 3.5 secs
	ENDIF
; or here if switch released earlier than 3.5 seconds
 
Top