Is this actually possible to do??????

TEZARM

Senior Member
Yep, me again. Still playing around with code and want to be able to the following.
Input 1 can be my trigger which is a switch going low when pressed.
Output 2 can be my output which simply turns on an led.
Now the tricky part. If Input 1 goes low once (a single negative pulse) than I want the led to turn on.
BUT if Input 1 is held low for 3 Seconds I want the led to flash instead as this will be a seperate function but still using the same input of course. So the trick is how do I get the picaxe to tell the difference between whether Input 1 is pressed once OR held low for 3 seconds as this is 2 totally seperate functions I am getting the picaxe to try and do here off the same Input only. Is this possible to do? How? Examples please guys. Thanks.
 

Bloody-orc

Senior Member
what will happen, if the button is released? it'll go off?
is so then you could make a loop checking the button status every 10 ms for 300 times or so.and if no change start LED FLASH sequence.
 

BeanieBots

Moderator
Come on TEZARM. This is very similar to the last forum tutorial you had. :)
As Bloody-orc states, make a loop that checks every 10mS or so and increments a counter with each check. All the time the button is pressed the counter increments. When the button is released, check the counter (how long it was pressed) and then make your choice.
Homework...
what happens if the button is held down for hours? what should it do by design and what will happen to your counter?
 

Jeremy Leach

Senior Member
Ok, I'm doing my own homework on the new language features, hope I've got it right !...

<code><pre><font size=2 face='Courier'>
'Assume there's an interrupt or continual check on input1 thats made it reach this point...

Counter = 0
Do
Pause 10
Inc Counter
If Counter = 300 Then FlashLED
Until Pin1 = 1

TurnOnLED:
you write the code...
Return

FlashLED:
you write the code...
Return
</font></pre></code>


Edited by - jeremy leach on 28/11/2006 21:33:52
 

TEZARM

Senior Member
&quot;Come on TEZARM. This is very similar to the last forum tutorial you had.&quot;

Um, Beaniebots, it's a completely different topic as far as I am concerned. I am sorry if I am a nuisance to you but if it is the same as before than maybe I am missing something from last time as I have tried figuring this out but failed so far to do it. I am still trying to come to grips with this programming and am not perfect so obviously I am not completely understanding the code side of things yet but as far as I am concerned theres nothing wrong with making mistakes, I mean thats the best way to learn is it not?

I am thinking I have not explained well enough. The button needs to be held down FOR 3 seconds for the led to flash. I don't mean a pause for 3 seconds to wait.
what will happen, if the button is released?
It will not stop, it will than wait till button is pressed once again and than the led will just stay on, than it waits again and when the button is held down for 3 secs it starts flashing again and on and on and on.
 

inglewoodpete

Senior Member
In principle, when using the 'on' time of a pushbutton to produce different results, you need to react to the <i>release </i> of the button rather than the 'press'. A loop counter (timer) is used to determine the length of time the switch is held.
More or less like Jeremy's code, but not exactly.

Added later:<code><pre><font size=2 face='Courier'>
Symbol LpCount = b0

Main: LpCount = 0
Do While Pin1 = 1 'Enter, loop while sw operated
Pause 10
If LpCount &lt; 255 Then
Inc LpCount
EndIf
Loop
If LpCount &gt; 30
GoSub FlashLED
ElseIf LpCount &gt; 0
GoSub TurnOnOrOff
EndIf
GoTo Main
</font></pre></code>

Forgive any syntax errors. (Jeremy had 1 or 2 as well ;o)

Edited by - inglewoodpete on 29/11/2006 04:17:13
 

eclectic

Moderator
Tez,

WHILE your LED on output 2 is flashing,

do you want the Picaxe to be doing several other things as well?

OR, will the chip just do the ONE job?

e.
 

Jeremy Leach

Senior Member
Hi Pete, TEZARM said &quot;Input 1 can be my trigger which is a switch going low when pressed&quot;, so it's active low ;-)

I'm still not sure we're really clear on the setup.
 

TEZARM

Senior Member
WHILE your LED on output 2 is flashing,
do you want the Picaxe to be doing several other things as well? No, just that mate.

Ah, well thanks for your examples guys. It's kinda funny how you are all using the new DO feature. I am still using the old Programming Editor at the mo so guess it might be time to update huh.
inglewoodpete, thanks for that, I understand it better now as the way u have explained it is perfect for my IQ. Cheers all.
 

inglewoodpete

Senior Member
As before, this is untested. However, it should maintain flashing and LED states and well as handling time-dependant switch control.
<code><pre><font size=2 face='Courier'>
Symbol LpCount = b0
Symbol LEDState = b1 '0 = off, 1 = on, 2 to 255 = flash
Symbol LED = ???
Symbol Active = 0
Symbol HalfTime = 50 'Must not be &gt; 127
'
Main: LpCount = 0
Do While Pin1 = Active 'Enter and/or loop while sw operated
Pause 10
If LpCount &lt; 255 Then
Inc LpCount
EndIf
If LEDState &gt; 1 Then
Inc LEDState
EndIf
Loop
Pause 10
If LpCount &gt; 30
'Start LED flashing
LEDState = 2
High LED
ElseIf LpCount &gt; 0
'Turn LED On or Off (Off also stops flashing)
If LEDState = 0 then
LEDState = 1
High LED
Else
LEDState = 0
Low LED
Endif
EndIf
If LEDState &gt; Halftime * 2 Then
'Flash the LED back on again
High LED
LEDState = 2
ElseIf LEDState &gt; HalfTime Then
'Half time of the flash cycle: reset the LED
Low LED
EndIf
If LEDState &gt; 1 Then
Inc LEDState
EndIf
GoTo Main
</font></pre></code>

Forgive any further syntax errors.
 

inglewoodpete

Senior Member
There are times when I just can't help myself. I was hooked, so I've now tested and enhanced the routine.

* The routine switches a LED depending on how long a pushbutton switch is held down.
* Switching condition is determined when the switch is <i>released. </i>
* A short press will turn the LED 'on' or 'off'.
* A long press starts the LED flashing.
* Once 'on' or flashing, the LED will continue to flash even if the switch is pressed again and held down.

With just a few code changes, the program could control 2 different LEDs according to how long the switch is held down.
<code><pre><font size=2 face='Courier'>'Written for PICAXE 08M (AXE-092 Board)
' Tested on 08M firmware 9.1
' 30-Nov-2006 80 bytes Peter Gee &quot;inglewoodpete&quot;
'
'***** Definitions *****
' Variables
Symbol SwTime = b0
Symbol LEDState = b1 '0 = off, 1 = on, 2 to 255 = flash
' Hardware
Symbol LED = 1 'Output
Symbol InSwitch = Pin3 'Input
' Logical Conditions
Symbol Active = 1 'Can be 0 or 1
' Counter Values
Symbol LongPress = 30 'Discrimination between short and log press
Symbol HalfTime = 10 'LED Hi-Lo transition
Symbol FullTime = 20 'LED Lo-Hi. Must not be &gt; 254
'***** End Definitions *****
'
Main: SwTime = 0
'The Do loop will be bypassed if switch not pressed
Do While InSwitch = Active
Pause 10
If SwTime &lt; 255 Then 'Avoid byte overflow
Inc SwTime
EndIf
Gosub HandleFlash 'Maintain flashing if required while button held down
Loop 'Remain in loop while switch pressed
Pause 10
'Process any switch press
If SwTime &gt; LongPress Then
'Start LED flashing
LEDState = 2
High LED
ElseIf SwTime &gt; 0 Then 'Short press
'Turn LED On or Off (Off also stops flashing)
If LEDState = 0 Then 'Off: so turn LED on
LEDState = 1
High LED
Else 'Turn off
LEDState = 0
Low LED
Endif
EndIf
Gosub HandleFlash
GoTo Main
'
HandleFlash: If LEDState &gt; 1 Then
'Flash control required only for range 2 to 255
Select Case LEDState
Case &gt; FullTime
'Cycle complete: Flash the LED back on again
High LED
LEDState = 2 'Reset counter to minimum
Case &gt; HalfTime
'Half time of the flash cycle: reset the LED
Low LED
EndSelect
Inc LEDState
EndIf
Return
</font></pre></code>

Enjoy. I did ;o)
 
Top