Difference between revisions of "Goodbye delay(), hello millis()"
(→Class) |
|||
Line 108: | Line 108: | ||
==with a led flasher== | ==with a led flasher== | ||
+ | |||
+ | Defining a class: | ||
+ | We start by declaring a “Flasher” class: | ||
+ | |||
+ | Then we add in all the variables from the Flasher example. Since they are part of the class, they are known as member variables. | ||
+ | |||
+ | <syntaxhighlight lang=c style="border:3px dashed hotpink"> | ||
+ | class Flasher | ||
+ | { | ||
+ | // Class Member Variables | ||
+ | // These are initialized at startup | ||
+ | int ledPin; // the number of the LED pin | ||
+ | long OnTime; // milliseconds of on-time | ||
+ | long OffTime; // milliseconds of off-time | ||
+ | |||
+ | // These maintain the current state | ||
+ | int ledState; // ledState used to set the LED | ||
+ | unsigned long previousMillis; // will store last time LED was updated | ||
+ | }; | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | Next we add a constructor. The constructor has the same name as the class and its job is to initialize all the variables. | ||
+ | |||
+ | <syntaxhighlight lang=c style="border:3px dashed hotpink"> | ||
+ | class Flasher | ||
+ | { | ||
+ | // Class Member Variables | ||
+ | // These are initialized at startup | ||
+ | int ledPin; // the number of the LED pin | ||
+ | long OnTime; // milliseconds of on-time | ||
+ | long OffTime; // milliseconds of off-time | ||
+ | |||
+ | // These maintain the current state | ||
+ | int ledState; // ledState used to set the LED | ||
+ | unsigned long previousMillis; // will store last time LED was updated | ||
+ | |||
+ | // Constructor - creates a Flasher | ||
+ | // and initializes the member variables and state | ||
+ | public: | ||
+ | Flasher(int pin, long on, long off) | ||
+ | { | ||
+ | ledPin = pin; | ||
+ | pinMode(ledPin, OUTPUT); | ||
+ | |||
+ | OnTime = on; | ||
+ | OffTime = off; | ||
+ | |||
+ | ledState = LOW; | ||
+ | previousMillis = 0; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | Finally we take our loop and turn it into a member function called “Update()”. Note that this is identical to our original void loop(). Only the name has changed. | ||
+ | |||
+ | <syntaxhighlight lang=c style="border:3px dashed hotpink"> | ||
+ | |||
+ | class Flasher | ||
+ | { | ||
+ | // Class Member Variables | ||
+ | // These are initialized at startup | ||
+ | int ledPin; // the number of the LED pin | ||
+ | long OnTime; // milliseconds of on-time | ||
+ | long OffTime; // milliseconds of off-time | ||
+ | |||
+ | // These maintain the current state | ||
+ | int ledState; // ledState used to set the LED | ||
+ | unsigned long previousMillis; // will store last time LED was updated | ||
+ | |||
+ | // Constructor - creates a Flasher | ||
+ | // and initializes the member variables and state | ||
+ | public: | ||
+ | Flasher(int pin, long on, long off) | ||
+ | { | ||
+ | ledPin = pin; | ||
+ | pinMode(ledPin, OUTPUT); | ||
+ | |||
+ | OnTime = on; | ||
+ | OffTime = off; | ||
+ | |||
+ | ledState = LOW; | ||
+ | previousMillis = 0; | ||
+ | } | ||
+ | |||
+ | void Update() | ||
+ | { | ||
+ | // check to see if it's time to change the state of the LED | ||
+ | unsigned long currentMillis = millis(); | ||
+ | |||
+ | if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime)) | ||
+ | { | ||
+ | ledState = LOW; // Turn it off | ||
+ | previousMillis = currentMillis; // Remember the time | ||
+ | digitalWrite(ledPin, ledState); // Update the actual LED | ||
+ | } | ||
+ | else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime)) | ||
+ | { | ||
+ | ledState = HIGH; // turn it on | ||
+ | previousMillis = currentMillis; // Remember the time | ||
+ | digitalWrite(ledPin, ledState); // Update the actual LED | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | By simply re-arranging our existing code into the Flasher class, we have encapsulated all of the variables (the state) and the functionality (the machine) for flashing a LED. | ||
+ | |||
+ | Now lets use it: | ||
+ | Now, for every LED that we want to flash, we create an instance of the Flasher class by calling the constructor. And on every pass through the loop we just need to call Update() for each instance of Flasher. | ||
+ | |||
+ | There is no need to replicate the entire state machine code anymore. We just need to ask for another instance of the Flasher class! | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang=c style="border:3px dashed hotpink"> | ||
+ | class Flasher | ||
+ | { | ||
+ | // Class Member Variables | ||
+ | // These are initialized at startup | ||
+ | int ledPin; // the number of the LED pin | ||
+ | long OnTime; // milliseconds of on-time | ||
+ | long OffTime; // milliseconds of off-time | ||
+ | |||
+ | // These maintain the current state | ||
+ | int ledState; // ledState used to set the LED | ||
+ | unsigned long previousMillis; // will store last time LED was updated | ||
+ | |||
+ | // Constructor - creates a Flasher | ||
+ | // and initializes the member variables and state | ||
+ | public: | ||
+ | Flasher(int pin, long on, long off) | ||
+ | { | ||
+ | ledPin = pin; | ||
+ | pinMode(ledPin, OUTPUT); | ||
+ | |||
+ | OnTime = on; | ||
+ | OffTime = off; | ||
+ | |||
+ | ledState = LOW; | ||
+ | previousMillis = 0; | ||
+ | } | ||
+ | |||
+ | void Update() | ||
+ | { | ||
+ | // check to see if it's time to change the state of the LED | ||
+ | unsigned long currentMillis = millis(); | ||
+ | |||
+ | if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime)) | ||
+ | { | ||
+ | ledState = LOW; // Turn it off | ||
+ | previousMillis = currentMillis; // Remember the time | ||
+ | digitalWrite(ledPin, ledState); // Update the actual LED | ||
+ | } | ||
+ | else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime)) | ||
+ | { | ||
+ | ledState = HIGH; // turn it on | ||
+ | previousMillis = currentMillis; // Remember the time | ||
+ | digitalWrite(ledPin, ledState); // Update the actual LED | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | |||
+ | Flasher led1(12, 100, 400); | ||
+ | Flasher led2(13, 350, 350); | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | led1.Update(); | ||
+ | led2.Update(); | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
==with the ultrasonic sensor== | ==with the ultrasonic sensor== |
Revision as of 17:22, 29 October 2020
Getting rid of the delay
Using the delay() function is nice, but the more we want to do, the more it just brings us problems. During a delay() call, you can’t respond to inputs, you can't process any data and you can’t change any outputs. The delay() ties up 100% of the processor. So, if any part of your code uses a delay(), everything else is dead in the water for the duration. So let's see how we can go on without using it:)
Usng millis()
One simple technique for implementing timing is to make a schedule and keep an eye on the clock. Instead of a world-stopping delay, you just check the clock regularly so you know when it is time to act. Meanwhile the processor is still free for other tasks to do their thing. A very simple example of this is the BlinkWithoutDelay example sketch that comes with the IDE.
/*
Blink without Delay
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.
The circuit:
- Use the onboard LED.
- Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
is set to the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your
Arduino model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
modified 11 Nov 2013
by Scott Fitzgerald
modified 9 Jan 2017
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to set a pin number:
const int ledPin = 3;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
State + Machine = State Machine
Note that we have variables to keep track of whether the LED is ON or OFF. And variables to keep track of when the last change happened. That is the State part of the State Machine.
We also have code that looks at the state and decides when and how it needs to change. That is the Machine part. Every time through the loop we ‘run the machine’ and the machine takes care of updating the state.
For the advanced group
For the advanced people, write a sketch where using millis() you can change the times of the on and off times separately.
another way
Ok, lets make it a bit more efficient
A variant that has a different on-time and off-time.
the ultrasonic example
now more than one
Class
with a led flasher
Defining a class: We start by declaring a “Flasher” class:
Then we add in all the variables from the Flasher example. Since they are part of the class, they are known as member variables.
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
};
Next we add a constructor. The constructor has the same name as the class and its job is to initialize all the variables.
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, long on, long off)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
};
Finally we take our loop and turn it into a member function called “Update()”. Note that this is identical to our original void loop(). Only the name has changed.
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, long on, long off)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
void Update()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
};
By simply re-arranging our existing code into the Flasher class, we have encapsulated all of the variables (the state) and the functionality (the machine) for flashing a LED.
Now lets use it: Now, for every LED that we want to flash, we create an instance of the Flasher class by calling the constructor. And on every pass through the loop we just need to call Update() for each instance of Flasher.
There is no need to replicate the entire state machine code anymore. We just need to ask for another instance of the Flasher class!
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, long on, long off)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
void Update()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
};
Flasher led1(12, 100, 400);
Flasher led2(13, 350, 350);
void setup()
{
}
void loop()
{
led1.Update();
led2.Update();
}