Menu

Help needed with replacing "wait" with Chris's Millis() function

Help
viscomjim
2016-07-15
2016-07-16
  • viscomjim

    viscomjim - 2016-07-15

    In an effor to get away from the wait function using a pot, I am trying to use Chris's Millis() function to replace it in many areas of my programs. With the following, any interrupts or changes in the pots value are not very responsive especially if the wait winds up being a second or a few seconds.

    do
    Led = on
    wait readAD(AN4) * 10 ms
    Led = off
    wait readAD(AN4) * 10 ms
    loop
    

    So would something like this work using a pot or would varying the pot during the loop make things wonky? (This is untested, just trying to figure it out during my lunch break...)

    do
    LED = on
        CurMillis = Millis
        do
            Interval = DelayPot * 10
        loop until Millis - CurMillis > Interval
    LED = off
        CurMillis = Millis
        do
            Interval = DelayPot * 10
        loop until Millis - CurMillis > Interval
    loop
    

    Is this the way to go forward or is there a better way to not tie up the pic in a wait?

    If not familiar with Chris's millis function, you are setting up a timer overflow interrupt running at 1 ms and perform the following sub (sub T0Int) and function (Millis())...

    '*********************** T0 INTERRUPT SUB *******************
    sub T0Int
    settimer 0, 100
    T0MS = T0MS + 1
    TMR0IF = 0
    end sub
    
    '*********************** MILLIS FUNCTION *******************
    function Millis() as long
    IntOff
    Millis = T0MS
    IntOn
    end function
    
     
  • Chris Roper

    Chris Roper - 2016-07-15

    Don't think of the millis() function in terms of interrupts or underlying functions, that will just confuse things, rather think of it as what it is, the time elapsed in milliSeconds since the board was powered up.

    It will not overflow for 50+ days so unless you intend your board to run the same application for more than a month and a half you don't need to worry about the rollover.

    What you do need to consider is the elapsed time, where:
    Elapsed_Time = CurrentTime - Last_Time

    So if you wanted to flash the LED according to the position of the pot the code would be:

    do
        Delay = Pot * 10 ' where pot is the result of an ADC function '
        CurMillis = Millis()
        if CurMillis - LastMillis >= Delay then 
            LED =! LED  ' Toggel LED '
            LastMillis = CurMillis
        End if
    loop
    

    Use the code I originally posted (with the preset values calculated for your clock speed and MPU) to set the timer, handle the interrupt and read the millis() and you should be fine.

    Hope that helps,
    Cheers,
    Chris

     

    Last edit: Chris Roper 2016-07-15
  • Chuck Hellebuyck

    Could just read the ADC at the top of the loop and then delay in a ForNext based on the value.
    Such as this:

       do
    
            dly =  ReadAD ( AN4 )
            for tim = 1 to dly
            wait 10 ms
            next
            LEDD2 = ON
            for tim = 1 to dly
            wait 10 ms
            next
            LEDD2 = OFF
    
        loop
    
     
  • Chris Roper

    Chris Roper - 2016-07-15

    The object is to avoid the wait and use the interrupt driven millis() function instead.
    Each time you say wait 10 mS you have thrown away 80,000 instruction cycles.
    Even for a hobby project that is a lot of redundancy, as a prototype for a commercial product it would be a no no.
    The Millis() function and Classes are probably the only two things I like about the Arduino.

    Cheers
    Chris

     
  • Chuck Hellebuyck

    I've written millions of line of code for lots of production products and trust me there are lots of delays in products. Just looping to see if a switch is pressed can create the same effect. I sense you want to be able to do something else instead of wait so interrupts or state machines are the way to go. In many cases deterministic code is desired so you knwo exactly how long it will be between sensor samples or switch reads. Interrrupts are actually non-deterministic since you can control when they occur.

    So Wait 10 ms is just a place holder. That can be replaced with a loop of 1ms delays and then read sensors or switches while lighting the LED. You need it faster, then delay miroseconds in a loop. and respond quicker to a switch press or sensor change. And the read instructions for the switch or sensor take time so if you can determine how long that takes then you don't need any Wait as they are the Wait.

    It all depends on the end goal and requirements. I was merely just trying to offer a simple alternative. Sorry if I wasn't helpful.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.