elapsed time running in parallel

marzan

Senior Member
Hi everyone. Haven`t been here for a while. Had a few health issues. Getting old SUCKS!.
Anyway, I am working on a new project. The basic idea is to use a touch pad to set off a beeper when touched. That part is working. the part I need help with is I want to run a program in parallel that if the touchpad isn`t touched within 5 minutes that the beeper is set off to remind you that the unit is still switched on. Also can the time be reset to zero if the pad is touched?
I was thinking that the enable time function might be able to be used, with the 2 programs running in parallel. Is that the right way to head, or is there a better way?
Here is the code that runs the touchplate, I am using an 08M2:
Code:
    setfreq m32
    symbol buzzer = c.4             
    symbol touchplate = c.1
    symbol adjustpot = c.2
    symbol touchval = w0
    symbol adjval = w1
    Main:



    do
        touch16 touchplate , touchval
        readADC10 adjustpot,adjval
        ;debug
        if touchval < adjval then
            high buzzer
            pause 800
            low buzzer
            pause 800
            high buzzer
            pause 800
            low buzzer
            wait 12
            
            
            else goto main
        endif
        
    loop
Thanks. Marz
 

AllyCat

Senior Member
Hi,

First a few "comments" or suggestions: I don't see any need to use Setfreq M32, why use about 4 times more power than needed (and make the actual time delays more confusing)? Also, it would be much better to make the Touch system "Self-Calibrating", for example by looking for sudden CHANGES in the TOUCHVAL. Finally, the ELSE GOTO main is unnecessary; that path will just "Fall through" anyway (and the structure would be better by not "jumping out" of a DO ... LOOP).

But to answer your question, the TIME variable should be all that you need. You don't even need an ENABLETIME as it runs by default. For example, change the "loop" at the end to LOOP UNTIL time > 300 ; seconds. After that time, the program will Fall Through to anything else you want it to do (then you could use a GOTO main but a higher level DO .. LOOP would be better). To reset the time, just use TIME = 0 at any appropriate point(s), probably inside the "touched" and at the end of the "timeout" sections.

Cheers, Alan.
 

Flenser

Senior Member
marzan,

These are two different ways to do a "superloop" main loop:
Code:
main:
<your code>
goto main
Code:
do
   <your code>
loop
You don't need the "goto main" that you have in your code.
and without it you end up with this main loop that continually checks if the touchpad has been touched and sounds the buzzer if it has:
Code:
    do
        touch16 touchplate , touchval
        readADC10 adjustpot,adjval
        ;debug
        if touchval < adjval then
            high buzzer
            pause 800
            low buzzer
            pause 800
            high buzzer
            pause 800
            low buzzer
            wait 12
        endif
    loop
Then you need to keep track of the last time that the touchpad was touched and continually re-calculate how long since it was last touched.
Once you have this you can test if it's been longer than 5min in the main loop without needing a parallel task.

something like this. NOTE this code is not tested only syntax checked.
Code:
setfreq m32
    symbol buzzer = c.4            
    symbol touchplate = c.1
    symbol adjustpot = c.2
    symbol touchval = w0
    symbol adjval = w1
    symbol lasttime = w2
    symbol elapsedtime  = w3


lasttime = time
Main:
    do
        touch16 touchplate , touchval
        readADC10 adjustpot,adjval
        ;debug
        if touchval < adjval then
           lasttime = time   ; update the last time the touchpad was touched
            high buzzer
            pause 800
            low buzzer
            pause 800
            high buzzer
            pause 800
            low buzzer
            wait 12
        endif
       
        elapsedtime = time-lasttime
        if elapsedtime > 300 then
           lasttime = time   ; update the last time to start the next 5 minute timout
            high buzzer
            pause 80
            low buzzer
            pause 80
            high buzzer
            pause 80
            low buzzer
      endif

    loop
 
Last edited:

marzan

Senior Member
Hi,

First a few "comments" or suggestions: I don't see any need to use Setfreq M32, why use about 4 times more power than needed (and make the actual time delays more confusing)? Also, it would be much better to make the Touch system "Self-Calibrating", for example by looking for sudden CHANGES in the TOUCHVAL. Finally, the ELSE GOTO main is unnecessary; that path will just "Fall through" anyway (and the structure would be better by not "jumping out" of a DO ... LOOP).

But to answer your question, the TIME variable should be all that you need. You don't even need an ENABLETIME as it runs by default. For example, change the "loop" at the end to LOOP UNTIL time > 300 ; seconds. After that time, the program will Fall Through to anything else you want it to do (then you could use a GOTO main but a higher level DO .. LOOP would be better). To reset the time, just use TIME = 0 at any appropriate point(s), probably inside the "touched" and at the end of the "timeout" sections.

Cheers, Alan.
Thanks for your input @AllyCat . That`s what I like about this forum. Everyone who replies knows better ways of doing things than us "occasional" coders, and gives that advice freely and in a way that dosen`t make us feel like idiots. That`s rare on forums these days.
Cheers, Marz.
 

marzan

Senior Member
This is the code that works as I hoped it would:
Code:
setfreq m8
    symbol buzzer = c.4            
    symbol touchplate = c.1
    symbol adjustpot = c.2
    symbol touchval = w0
    symbol adjval = w1
    symbol lasttime = w2
    symbol elapsedtime  = w3


lasttime = time
wait 4
     for b5 = 1 to 2
         toggle buzzer
         wait 1
    next
     
Main:
    do
        touch16 touchplate , touchval
        readADC10 adjustpot,adjval
      adjval = adjval * 34
        if touchval < adjval then
           lasttime = time   ; update the last time the touchpad was touched
            for b8 = 1 to 4
           toggle buzzer
           pause 200
           next
            wait 4
        endif
       
        elapsedtime = time-lasttime
        if elapsedtime > 150 then
           lasttime = time   ; update the last time to start the next 5 minute timout
         for b8 = 1 to 10
           toggle buzzer
           pause 120
           next
      endif

    loop
I had the frequency set so high because there seemed to be a big delay when it was touched. Then I realised I still had the debug on. Doh! I had to multiply the ADC value until The buzzer stayed on so there is a good range of adjustment for the sensitivity.. I will show you what it is for when I have all the bits sorted out. I`m 3d printing the enclosure at the moment.
Marz.
 
Top