08M Timer program

SMC

Member
Hey Guys!
Hippy & technical saved my butt a couple years back with an 18x project and I'm still so grateful!

Anyways- couldya help a newb with another basic programming problem?
The project requires lighting an LED for one timing cycle upon a SWITCH ON condition, and lighting the LED for a different timing cycle upon the SWITCH OFF condition. The circuit will have constant power. My problem is How do I make the SWITCH ON timing sequence run only once after the switch closes...and at the same time be watching for the switch opening to activate the 2nd timing cycle?

Here's my snippets of code so far:

MAINLOOP:
IF pin4 = 1 THEN goto SWITCH ON
IF pin4 = 0 THEN got SWITCH OFF


SWITCH ON:
high 0
for w0 = 0 to 100
pause w1
next w0
low 0
goto mainloop

SWITCH OFF:
high 0
for w0 = 0 to 50
pause w1
next w0
low 0
goto mainloop
 

inglewoodpete

Senior Member
You will need to record the state of the input pin for future reference.

Eg:
Code:
Symbol KeyMem = b7
MainLoop: IF pin4 = 1 THEN
             If KeyMem = 0 Then GoTo SwitchOn
             KeyMem = 1     'Remember that the 'turn on' condition has been handled
          Else
             'pin4 must be released (=0)
             If KeyMem = 1 Then GoTo SwitchOff
             KeyMem = 0     'Remember that the 'turn off' condition has been handled
          EndIf
          Goto MainLoop
Of course, there is an inherent problem when trying to use timers and key inputs in the same program: what happens when the key is pressed while the timer is stuck in the 'lamp on' loop? Of course, there's a solution to that, too :)
 

stocky

Senior Member
instead of "wasting" a full byte var - change b7 to bit0 - they you have 7 more bits to play with for other things.

I know it doesnt matter for this app - just a hint for the future :)
 

inglewoodpete

Senior Member
You're right Stocky: we only need to use 1 bit to record the previous state of the input.

The reason I chose b7 was for a register not previously referenced. SMC's code already used w0 and w1, which means that bit0 to bit15 are not available for use here.
 

stocky

Senior Member
oops - yeah - didnt read his code did I!!!

Anyway - handy hint for him and he doesnt need a WORD var for the loop anyway so:

Change W0 to b1
Change B7 to Bit0

:)
 

eclectic

Moderator
SMC.

1. How long is your W1 delay?

2. "Switch on" is a reserved word / phrase.
You need something else.

3. Did you mean something like
Code:
OFFswitch:
LOW 0 'high 0 ***
for w0 = 0 to 50
pause w1
next w0
HIGH 0' low 0 ***
goto mainloop
for the "off" section?

Anyhow, run your code through the simulator and try it.

e
 

SMC

Member
SMC.

1. How long is your W1 delay?

2. "Switch on" is a reserved word / phrase.
You need something else.

3. Did you mean something like
Code:
OFFswitch:
LOW 0 'high 0 ***
for w0 = 0 to 50
pause w1
next w0
HIGH 0' low 0 ***
goto mainloop
for the "off" section?

Anyhow, run your code through the simulator and try it.

e
Eclectic & Others:
Got it working!!
The timing cycles were up to 6 minutes long so I had to use word variables....and of course I had to change out the "switch on/off" syntax....and I had to use a gosub instead of a goto.....but it is working perfectly so far. Thanks again for the help- here is my much improved code:

Symbol KeyMem = b0


MainLoop:
pause 2000
b1 = 0
b2 = 0
readadc 1, b1 'read p1 (pre cycle pot) into variable b1
readadc 2, b2 'read p2 (post cycle pot)into variable b2


IF pin4 = 1 THEN
IF KeyMem = 0 THEN GOSUB keyOn
KeyMem = 1 'switch on' condition has been handled
ELSE
If KeyMem = 1 Then Gosub keyOff
KeyMem = 0 'switch off' condition has been handled
ENDIF
Goto MainLoop


keyOn:
high 0
for w3 = 0 to 720 'w3 = temp variable for for-next counter loop
pause b1 'b1 = pre cycle pot variable
next w3
low 0
return


keyOFF:
high 0
for w4 = 0 to 1440 'w4 = temp variable for for-next counter loop
pause b2 'b2 = post cycle pot variable
next w4
low 0
return
 

SMC

Member
ADC problem

I have 2 10k pots. +5 & GND across the pot, and the pot wipers to my ADC inputs. I've had to add 30 ohm resistors betweem GND and the pots to get the circuit to work? And now I can't get a 0 output from the ADC's. Same problem in the simulator. WTH? I thought a pot w/o any additional resistors was OK?
 

inglewoodpete

Senior Member
I've had to add 30 ohm resistors betweem GND and the pots to get the circuit to work?
You've got me there. Why do you need a 30ohm resistor between Gnd and the 'lower' end of the pot? The 30ohm resistor will certainly prevent the ADC from reading 0 as the lower limit. The additional resistor is not something I thought was necessary.
 

BeanieBots

Moderator
You DON'T need 30R resistors at the bottom of the pots to get them to work.
If you do, then something else is wrong.
You're correct that you won't get "zero" if you do fit them.

Please explain your reasoning behind the additional resistors. Then we might get to the bottom of the REAL problem.
 

hippy

Ex-Staff (retired)
From reading a post elsewhere it's quite important to indicate the circumstances of such problems; physical PICAXE chip, simulator or VSM.
 
Top