switching an output after a period of idle from the input switch.

Protoproff

New Member
Hi there it maybe an easy thing for someone out there but for me i've been out of project work for many years now, and sadly i'm forgetting all i learnt.
I hope you can help, i'm basically trying to switch on an output C.2 after the input C.1 hasn't registered a signal (high) after a one minute period.
C.1 will intermittently go High but if it fails to go high in a one minute or longer period then i need C.2 to go high. Any suggestions or pointers, anything will help. I'm using a Picaxe 08m2 IC. Thanks in advance Arif.
 

AllyCat

Senior Member
Hi,

It may depend for how long the input pin remains high, and what else the PICaxe is required to do, but the basic method could be:
Code:
do
  if pinc.1 = 1 then
    time = 0
    Low c.2
  endif
  if time > 59 then
    High c.2
  endif
loop
Make the 59 rather smaller and you can quickly test it in the simulator and you might want to use a PAUSE or PULSOUT when time > 59 . ;)

Cheers, Alan.
 

Protoproff

New Member
Hi Alan thanks for the reply and the suggestion, however after trying the code and adding some pauses i couldn't get it to work, any ideas bud?

wait 2
do
if pinc.1 = 1 then
time = 0
Low c.2
pause 10
endif
if time > 10 then
High c.2
pause 1000
endif
loop

that's what i've tried anything im missing? Thanks Arif
 
Last edited:

AllyCat

Senior Member
Hi,

Seems to work fine in the Program Editor Simulator. I've added a few lines which might help if the Editor/Simulator is not set up ideally.
In the Simulator, "Pin 2" should change colour after about 10 seconds, and back (or not change) if/when you click on "Pin 1". The Terminal Emulator window should show the progress of "time".
Code:
#picaxe 08m2
#terminal 4800
#no_data
wait 2
do
  if pinc.1 = 1 then
    time = 0
    Low c.2
  pause 10
  endif
  if time > 10 then
    High c.2
    pause 1000
  endif
  sertxd(#time,"  ")
loop
"Time" only works with M2s; it can be replaced with (say) "B1" and add a line after the "do" of "Pause 1000 : inc B1" .

Cheers, Alan.
 

Protoproff

New Member
Hi bud i managed to get the duration period to around 2 min(38) through experimenting, it seems i had the sim delay on which was giving me weird visuals. After switching off delay i was able to see the program function as you have shown, i tried the elseif command not sure if it had any significance. do and loop is new to me does it have benefits using these commands? Thanks for showing me the way.

Thanks Arif

main:
lOW C.2
pause 10
if pinC.1 = 1 then
time = 0
ELSEIf time > 38 then
High C.2
pause 100

end if
goto main
 
Last edited:

AllyCat

Senior Member
Hi,

"Time" should increment quite close to once per second in a real PICaxe, the simulator will always run more slowly.

DO ... LOOP and the qualifiers WHILE ... , UNTIL ... , etc. are the "preferred" structures because the Program Editor / Compiler can match up the start and end of loops and tests, and warn of (some) coding errors. For simple programs, there's nothing wrong with GOTO ... main; , etc., but using GOTOs can create more complex programs that may be impossible to analyse and fault-find. Therefore, we try to show how it "should" be done. ;)

Cheers, Alan.
 

Protoproff

New Member
Thanks Alan you've revived my coding interest with your help very much appreciated, and at these hours. I Think its time to hit the sack thanks for the very quick turn around time with my query I've learnt quite a bit off you.

Cheers Arif
 

AllyCat

Senior Member
Hi,

Personally I dislike the use of ELSEIF because ELSE , ELSEIF , ELSE IF and ELSE : IF are all potentially different instructions. Sometimes, ELSE is absolutely necessary, particularly where the input signal may change whilst the code is running, for example.
Code:
IF pinc.1 = 1 THEN
   Low REDLED
   High GREENLED
ELSE
   Low GREENLED
   High REDLED
ENDIF
If that were written as two separate IF ... ENDIF clauses then there is the possibility that both (or neither) LEDs would light, if the input changed whilst the instructions were being executed. That can't happen with the ELSE because only one of the two sections (shown indented for clarity) will be executed. (Multiple) indenting becomes particularly useful when "nesting" IF, FOR and DO, etc. sections of code. Maybe I'm being pedantic, but I've sometimes wondered why its ENDIF and not ENDELSE . :)

For more complex structures, sometimes the SELECT ... CASE is preferred, but I'm not a great enthusiast of it, because you do need to RTFM (as its behaviour is not as "obvious" as most Basic commands).

Cheers, Alan.
 

Protoproff

New Member
H,
Thanks again i understand now what you mean with the ELSEIF command i found that i can get some erratic things going on especially with the timing. You're example worked fine with my project. Certainly has revived my interest in MCU's. One other question i have bud can i apply 4.2v direct to pin 4 (C.3 In) without a pulldown resistor or is that bad practise? I know some pins have internal pullup/down resistors. I'm assuming i'll be fine as the voltage to pin 4 will be low and vary from 4.2v down to around 3.3v as the lipo battery discharges in the circuit.
Sorry for the delayed response to your previous advice but working night shifts has scrambled my brain, gladly off them now.

Thanks Arif.
 

AllyCat

Senior Member
Hi,

Generally, it is recommended to insert a resistor (typically of 1k - 10k) in series with any "signal" pin, to protect the chip against accidental current surges. Care is particularly needed for Leg4 (c.3) because it's the "programming" input of the "base" PIC chip, which doesn't have an internal "electrostatic protection" diode.

Also, it depends why you want to connect the battery to that pin (it isn't an ADC input so it can't measure the voltage) and basically will only detect if the input level falls below about 1.4 volts. If you just want it to detect if the battery becomes disconnected, then you should use a pull-down resistor of a value that won't excessively drain the battery. Without any pull-down resistor, the pin may still "float" high for a considerable time.

I don't believe you've specified the power supply voltage or its source, but the voltage applied to any pin should never be higher than the supply voltage itself. Note that the CALIBADC command can be used to measure the actual supply voltage, without needing any external components.

Cheers, Alan.
 

Protoproff

New Member
Hi Alan,

Thanks for the feedback and sorry once again for the delayed response, my first real first day back to project work. I have been feeding direct supply voltage of 3.6-4.2v (depending on state of lipo) into c.3 pin on the 08m2. I guess i should have realised that was a no no. I have modified the circuit to incorporate a pulldown resistor as well as a inline resistor to the input. Can you please advise me as to wether it looks to be correct. Im basically trying to measure physical movement in a kiddie rattle toy. Im not trying to measure the pulses so haven't added a debounce in code, just need to know the rattle is moving. I've added a pic of the circuit, maybe you can correct me or improve on it.

Thanks Arif (uk)
 

Attachments

AllyCat

Senior Member
Hi,

Yes, that should work, but it can be made simpler (and better) :

ALL of the 08M2's I/O pins have internal PULLUP resistors (but the Program Editor won't activate those on the c.0 and c.5 pins for you) so it's simpler to connect the "switch" between the input pin and Ground (Vss). Then you can activate the internal Weak Pullup resistor (on c.3) with a PULLUP %1000 command (it's not a coincidence that the "1" is followed by three "0"s). In this particular case, the 1k is hardly needed, because c.3 can never become an output and it's not being connected to anything that could feed energy through the pin.

The only difference is that the switch would then be "Active Low" (the PICaxe reads a "0" when the contact is closed) but if it is a "gravity switch" then the switch might be closed anyway if the toy is oriented "upside down"? So the program probably needs to look only for changes in the input signal, which can be done in many ways, for example using PULSIN ..... or COUNT... or direct IF pin... = ... THEN etc. commands.

Another advantage of the PULLUP command method is that you could switch it OFF and SLEEP the PICaxe for short periods, to reduce the average power drain to almost nothing. Thus you might not even need an ON/OFF switch for battery operation and could use the CALIBADC command to check and warn when the battery voltage (i.e. the PICaxe supply rail) is getting low. ;)

Cheers, Alan.
 
Top