In this tutorial, we’ll program an application for Blink that tells the piece to Blink. You should be able to follow the code line by line and hopefully learn a thing or two in the process. First things first, here’s the entire code for the application. We’ll go through this line by line down below!
/*
* Your very first Blinks application
*/
Timer blinkTimer;
bool isBlinkOn;
void setup() {
// this only happens once
blinkTimer.set(500);
isBlinkOn = false;
}
void loop() {
// this happens ~30 times per second
if(blinkTimer.isExpired()) {
isBlinkOn = !isBlinkOn;
blinkTimer.set(500);
}
if(isBlinkOn) {
setColor(RED);
}
else {
setColor(OFF);
}
}
You’ll want to start by creating your code environment with the following language.
/*
* Your very first Blinks application
*/
void setup() {
// this only happens once
}
void loop() {
// this happens over and over again
// the rate can be variable, but it will happen at
// a maximum the refresh rate of the display (~66Hz)
}
After that, add your needed variables.
Timer blinkTimer;
bool isBlinkOn;
Then initialize those variables.
void setup() {
blinkTimer.set(500);
isBlinkOn = false;
}
If the time expires, update the status, on or off.
void loop() {
if(blinkTimer.isExpired()) {
isBlinkOn = !isBlinkOn;
blinkTimer.set(500);
}
}
Now set our display, should it be on or off?
void loop() {
...
if(isBlinkOn) {
setColor(RED);
}
else {
setColor(OFF);
}
}
That’s it! Take a look back at the entire code and see if you can work out how these items all work together!