How to define a timer or a time counter?

nanogear

Member
Hello,

Imagine that I have a light sensor connected to a 08M2 and change the state of a pin dependent of the condicional of b0 defined by the sensor.

main:
readadc C.1,b0
debug b0
pause 500
if b0 > 20 then sub1 ; if b0 > 120 then do sub1

low B.2 ; else switch on 2
goto main

sub1:
high B.2
goto main


How can I make that if b0 > 20 for more than 5 seconds, then execute sub1????



Thank you in advance guys!!
 

nick12ab

Senior Member
Code:
#picaxe 08m2
main:
    readadc C.1,b0
    debug b0
    pause 500
    if b0 > 20 and time >= 5 then sub1 ; if b0 > 120 then do sub1
    if b0 <= 20 then
        time = 0
    end if
    low B.2 ; else switch on 2
    goto main

sub1:
    high B.2
    goto main
 

nanogear

Member
Code:
#picaxe 08m2
main:
    readadc C.1,b0
    debug b0
    pause 500
    if b0 > 20 and time >= 5 then sub1 ; if b0 > 120 then do sub1
    if b0 <= 20 then
        time = 0
    end if
    low B.2 ; else switch on 2
    goto main

sub1:
    high B.2
    goto main

Oh my God!!! Thank you so much!!!! You are awesome!!!
 

nanogear

Member
Hello, thank you for your help...

How can I make that if I have:


main:
readadc C.1,b0
debug b0
pause 500
if b0 > 20 and time >= 5 then sub1
if b0 <= 20 then
time = 0
end if
low B.2
goto main

sub1:
high B.2
pause 50
wait 3
low B.2
goto main

I maintain B.2 high for 3 second then get low. But I want that B.2 remains low until the condition b0 > 20 for 5 seconds is no longer fulfilled... Please help me!!!
 

nick12ab

Senior Member
I maintain B.2 high for 3 second then get low. But I want that B.2 remains low until the condition b0 > 20 for 5 seconds is no longer fulfilled... Please help me!!!
Sorry, I don't really understand what you're trying to say - B2 remains low until the condition... is no longer fulfilled

If this code does what you want it to do then great, otherwise can you expand on your question?
Code:
#picaxe 08m2
main:
    readadc C.1,b0
    if b0 > 20 and time >= 5 then
        high B.2
    else
        low B.2
    end if
    if b0 <= 20 then
        time = 0
    end if
    goto main
 

nanogear

Member
Sorry, I don't really understand what you're trying to say - B2 remains low until the condition... is no longer fulfilled

If this code does what you want it to do then great, otherwise can you expand on your question?
Thank you in advance and Im sorry for the lack of information.

I want to simulate an automatic switch like the one that use some cars to turn on/off lights depending on the amount of ambient light. That is why I am using a light sensor connected to the ADC of the 08M2.

You helped me with a first issue(Wait 5 seconds to high B.2 when b0 > 20 ). That works awesome.

For the purposes of the project, I need to simulate a momentary switch, in which, when b0 > 20 and time >= 5 then high B.2 for only 3 seconds and then B.2 go low again.

The b0 still be reading more than 20 from the ADC and B.2 need to be in low until, once again, b0 go less than 20 and newly increase more than 20 to repeat the process of wait 5 seconds to change the state of B.2 to high.

Im trying to use the DO-LOOP functions but it looks like my code freeze.

Please help me with this... I am still searching more functions from picaxe code to achieve this.

Again, Thank you nick12ab!!!
 

nick12ab

Senior Member
For the purposes of the project, I need to simulate a momentary switch, in which, when b0 > 20 and time >= 5 then high B.2 for only 3 seconds and then B.2 go low again.
Does this do what you want?
Code:
#picaxe 08m2
main:
    readadc C.1,b0
    if b0 > 20 and time >= 5 and time < 8 then
        high B.2
    else
        low B.2
    end if
    if b0 <= 20 then
        time = 0
    end if
    goto main
 

nanogear

Member
Does this do what you want?
Code:
#picaxe 08m2
main:
    readadc C.1,b0
    if b0 > 20 and time >= 5 and time < 8 then
        high B.2
    else
        low B.2
    end if
    if b0 <= 20 then
        time = 0
    end if
    goto main
Thank you so much... Let me implement it and check if this works for me!!! Thank you again!!
 

nanogear

Member
Hello,

How can I define in the code that when: b0 < 9 and time >= 5 and time <8 then sub1... but at the same time when b0 > 48 and time >= 5 and time <8 then sub1?

I can't find the correct sintax to achieve this.

Code:
main:                                                 ; make a label called main
     readadc C.1,b0                                   ; read ADC1 into variable b0
     debug b0                                         ; transmit value to computer screen
     pause 500                                        ; short delay
     if b0 < 9 and time >= 5 and time <8 then sub1    ; if b0 < 9 for 5 or more seconds then do sub1
     if b0 >= 9 then                                  ; if b0 >= 48 then dont count time
        time = 0
     end if
     high B.2                                         ; else switch on 2
     goto main                                        ; jump back to start

sub1:                                                 ; make a label
     low B.2                                          ; switch off 2
     wait 2                                           ; wait 3 seconds to simulate switch
     high B.2                                         ; switch on 2
     pause 500
     goto main                                        ; jump back to start
Thank you in advance
 

nick12ab

Senior Member
Try:
Code:
#picaxe 08m2
main:                                    ; make a label called main
    readadc C.1,b0                        ; read ADC1 into variable b0
    debug b0                            ; transmit value to computer screen
    pause 500                            ; short delay
    if time >= 5 and time <8 then                ; if b0 < 9 for 5 or more seconds then do sub1
        if b0 > 48 or b0 < 9 then
            goto sub1
        end if
    end if
    if b0 >= 9 then                        ; if b0 >= 48 then dont count time
        time = 0
    end if
    high B.2                            ; else switch on 2
    goto main                            ; jump back to start

sub1:                                    ; make a label
    low B.2                            ; switch off 2
    wait 2                            ; wait 3 seconds to simulate switch
    high B.2                            ; switch on 2
    pause 500
    goto main                            ; jump back to start
 

hippy

Ex-Staff (retired)
Im sorry. When b0>48 the program is not jumping to sub1. Only when b0 < 9.
That's correct, and as you seem to have noted in post #12; that's because you reset time when b0 >= 9 so you'll never ever get a condition when b0 > 9 and time > 0.

The solution is to only reset time to zero when it should be set to zero and that condition won't be when b0 > 9. What it will be is hard to say because we have no idea what b0 represents or what values it may contain.
 

nanogear

Member
That's correct, and as you seem to have noted in post #12; that's because you reset time when b0 >= 9 so you'll never ever get a condition when b0 > 9 and time > 0.

The solution is to only reset time to zero when it should be set to zero and that condition won't be when b0 > 9. What it will be is hard to say because we have no idea what b0 represents or what values it may contain.
hippy, thank you very much!!!

b0 represent what the ADC converts from an ambient light sensor. 0 to 255. The idea is that when b0 < 9 or b0 > 48 for more than 5 seconds and less than 8 seconds then jump to the subrutine.
 

hippy

Ex-Staff (retired)
b0 represent what the ADC converts from an ambient light sensor. 0 to 255. The idea is that when b0 < 9 or b0 > 48 for more than 5 seconds and less than 8 seconds then jump to the subrutine.
So if we assume 10 to 47 is the 'no action' level, then the time can be reset then, so one option is ... ( will need to be tweaked ) ...

Code:
Do
  ReadAdc <pin>, lightLevel
  Select Case lightLevel
    Case < 9  : If time > 5 And Time < 8 Then
                  Gosub Sub1 
                End If
    Case > 48 : If time > 5 And Time < 8 Then
                  Gosub Sub1 
                End If
    Else      : time = 0 
  End Select
Loop
 

nanogear

Member
So if we assume 10 to 47 is the 'no action' level, then the time can be reset then, so one option is ... ( will need to be tweaked ) ...

Code:
Do
  ReadAdc <pin>, lightLevel
  Select Case lightLevel
    Case < 9  : If time > 5 And Time < 8 Then
                  Gosub Sub1 
                End If
    Case > 48 : If time > 5 And Time < 8 Then
                  Gosub Sub1 
                End If
    Else      : time = 0 
  End Select
Loop
Its working!!! Thank you a lot! Just need to be tweaked as you said!!!

Thanks!!!
 
Top