Turn off multiple tasks

alex126

Member
Hello. There is a 12 led night lamp with a touch switch. (2 chips 20m2). Every led is an independent task. Is there a way to turn off all of them at the same time after a period of time (30 min.) to save battery (4AA)
Thank you very much
.WP_20200519_001.JPG

Here is the code
#PICAXE 20M2
#NO_DATA
; works. turn on off the led when touch. Turn all led off after 30 minutes (1800000ms)
symbol V_med = 5200 ;long wires
symbol pause1 = 300

main:
start0:
touch16 B.6,w0 ; read value into w0
if w0 > V_med then
high C.0
pause pause1 ;delay to have a time for finger to be detached from the button
high C.0
end if
touch16 B.6,w0 ; read value into w0
if w0 > V_med then
low C.0
pause pause1 ;delay to have a time for finger to be detached from the button
low C.0
end if
goto start0

start1:
touch16 B.5,w1 ; read value into w1
if w1 > V_med then
high C.1
pause pause1
high C.1
end if
touch16 B.5,w1 ; read value into w1
if w1 > V_med then
low C.1
pause pause1
low C.1
end if
goto start1

start2:
touch16 B.4,w2 ; read value into w2
if w2 > V_med then
high C.2
pause pause1
high C.2
end if
touch16 B.4,w2 ; read value into w2
if w2 > V_med then
low C.2
pause pause1
low C.2
end if
goto start2

start3:
touch16 B.3,w3 ; read value into w3
if w3 > V_med then
high C.3
pause pause1
high C.3
end if
touch16 B.3,w3 ; read value into w3
if w3 > V_med then
low C.3
pause pause1
low C.3
end if
goto start3

start4:
touch16 B.2,w4 ; read value into w4
if w4 > V_med then
high C.4
pause pause1
high C.4
end if
touch16 B.2,w4 ; read value into w4
if w4 > V_med then
low C.4
pause pause1
low C.4
end if
goto start4

start5:
touch16 B.1,w5 ; read value into w5
if w5 > V_med then
high C.5
pause pause1
high C.5
end if
touch16 B.1,w5 ; read value into w5
if w5 > V_med then
low C.5
pause pause1
low C.5
end if
goto start5

goto main
 

inglewoodpete

Senior Member
Firstly please post your code between [code] and [/code] markers, so that it presents better and is easier to read, on the forum page.

Do you want the PICAXE to turn off after 30 minutes or just disable the LEDs?
 

alex126

Member
It can be a turn off by delay or disable leds after 30 minutes. It needs to be in the same time.
If I go by "pause" there is a different delay between each led (you don't touch each in the same time to make a pattern with leds). I checked in manual the interrupt commands... Thank you.
 

lbenson

Senior Member
Still not sure exactly what you're asking for; 30 minutes after what?

Something like this could work for 30 minutes after power-on:
Code:
  symbol minutes=b4
  dirsC=%11111111 ' all outputs
  time=0
  minutes=0
main:
  if time > 60 then: inc minutes: time=0: endif
  if minutes > 29 then: pinsC=%00000000: minutes=0: endif
' . . . the rest of your code
 

Aries

New Member
If you want to do something for all the tasks at the same time, you need a common flag which is set by something and then read by all the tasks - something like the following. This uses task 0 to monitor time and set TurnOff after 30 minutes. Presumably, your 30 minutes is after the last "touch", so you need to reset TurnOff and Minutes every time there is a touch.
Code:
symbol TurnOff = 66 ' pick a spare location somewhere in RAM
symbol Minutes = TurnOff + 1 ' count minutes
main:
  poke TurnOff,0
  poke Minutes,0
  time = 0

start0:
  peek Minutes,b0
  if time >= 60 then  ' another minute has passed
    inc b0
    poke Minutes,b0
    time = 0
  endif
  if b0 >= 30 then
    poke TurnOff,1
  endif
...
  if w0 > V_med then
    poke TurnOff,0
    poke Minutes,0
  endif
        
  peek TurnOff,b0
    if b0 = 1 then low C.0
  endif
  .....
  goto start0

start1:
  ...
  if w1 > V_med then
    poke TurnOff,0
    poke Minutes,0
  endif
  peek TurnOff,b2
    if b2 = 1 then low C.1
  endif
  ...
  goto start1
 

hippy

Senior Member
While the use of separate tasks per LED and button is entirely valid, my inclination is to refactor that so it can all be done as a single task within a timed loop -
Code:
#Picaxe 20M2
#No_Data

symbol V_med  = 5200 ;long wires
symbol pause1 = 300

#Macro Check( wVar, touchPin, ledPin )
  w0 = wVar & $7FFF
  If w0 = 0 Then
    Touch touchPin, w0
    If w0 > V_med Then
      wVar = wVar & $8000 ^ $8000 + pause1
      If wVar >= $8000 Then
        High ledPin
      Else
        Low  ledPin
      End If 
    End If
  Else
    wVar = wVar - 10
  EndIf
#EndMacro

Do
  Check( w1, B.6, C.0 )
  Check( w2, B.5, C.1 )
  Check( w3, B.4, C.2 )
  Check( w4, B.3, C.3 )
  Check( w5, B.2, C.4 )
  Check( w6, B.1, C.5 )
  Pause 10
Loop
And then one can add a timeout, in this case 30 minutes after the last touch ...
Code:
#Picaxe 20M2
#No_Data

symbol V_med  = 5200 ;long wires
symbol pause1 = 300

Symbol tick  = w10 ; b21:b20
Symbol secs  = b22
Symbol mins  = b23

#Macro Check( wVar, touchPin, ledPin )
  w0 = wVar & $7FFF
  If w0 = 0 Then
    Touch touchPin, w0
    If w0 > V_med Then
      wVar = wVar & $8000 ^ $8000 + pause1
      If wVar >= $8000 Then
        High ledPin
      Else
        Low  ledPin
      End If 
      mins = 30
      secs = 60
      tick = 0
    End If
  Else
    wVar = wVar - 10
  EndIf
  If mins = 0 Then
    Low ledPin
  End If
#EndMacro

Do
  Check( w1, B.6, C.0 )
  Check( w2, B.5, C.1 )
  Check( w3, B.4, C.2 )
  Check( w4, B.3, C.3 )
  Check( w5, B.2, C.4 )
  Check( w6, B.1, C.5 )
  Pause 10
  If mins > 0 Then
    tick = tick + 10
    If tick > 1000 Then
      tick = 0
      secs = secs Min 1 - 1
      If secs = 0 Then
        secs = 60
        mins = mins - 1
      End If
    End If
  End If
None of that has been tested, not even simulated, so may need tweaking.
 
Top