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 msLed = offwait 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 > IntervalLED = 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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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...)
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())...
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:
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
Could just read the ADC at the top of the loop and then delay in a ForNext based on the value.
Such as this:
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
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.