Hello, I'm wondering if i'm doing this right. I'm trying to use the millis() function to delay another function precisely. I want to know if I'm declaring the variables right, if I'm fetching the potentiometer value right and if I really need floating points to do this.
Here's my code:
uint16_t delayTime = 1000;
uint64_t time;
void setup(){
}
void loop() {
while (millis() < time + analogRead(pot) * (delayTime / 1023)) {}
thatOtherFunction();
time = millis();
}
Try to avoid the use of while( ) loops as they are blocking.
Search this site for non-blocking TIMER, or BWD.
example:
//**************************************** h e a r t b e a t L E D T I M E R
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatMillis >= 500)
{
//restart this TIMER
heartbeatMillis = millis();
//Toggle the heartbeatLED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
You're missing the point, using millis() correctly allows you to wait until the time is appropriate while freeing up the processor to do other things. Using while() like that you might as well just use delay(), you've just re-created delay() in a different way.
This: Flashing multiple LEDs at the same time
Shows you several different ways to use millis() to flash LEDs while leaving the processor free to do other things. Follow the example that makes most sense to you.
uint16_t delayTime = 1000;
uint64_t time;
void setup()
{
}
void loop()
{
if (millis() - time >= analogRead(pot) * (delayTime / 1023))
{
thatOtherFunction();
//restart this TIMER
time = millis();
}
}
analogRead(pot) * (delayTime / 1023) converts a potentiometer reading to a 0 - 1000 value. Also, another thing that I want to know, is if I would need floating points to do it?