How to code a NEOPixel Timer with Arduino
Learn how to code a DIY NEOPixel Game Timer using Arduino!
Parts List:
- Momentary Button
- Switch
- Protoboard
- Solid Core Hookup Wire
- ESP8266 with Stacking headers
- WS2812B LED Strip (200mm by 10mm)
- LiPo Battery
- Micro USB – Female to Male
Arduino Code:
#include <FastLED.h>
//pins
const byte BUTTON_PIN = 15;
const byte DATA_PIN = 2;
// LED array
const byte NUM_LEDS = 12;
CRGB leds[NUM_LEDS];
// Colors used when setting time. Color ref: https://github.com/FastLED/FastLED/wiki/FastLED-HSV-Colors
const CHSV UNCOUNTED_COLOR = CHSV(0, 255, 255); //Red
const CHSV SECONDS_COUNTED_COLOR = CHSV(160, 255, 255); //Blue
const CHSV MINUTES_COUNTED_COLOR = CHSV(96, 255, 255); // Green
const CHSV HOURS_COUNTED_COLOR = CHSV(64, 255, 255); // Yellow
// Colors / "Hue" used durring each turn (0-255)
const int START_HUE = 85; //Green(ish) MUST be a number < END_HUE
const int END_HUE = 255; //Red
const int HUE_INCREMENT = END_HUE - START_HUE; // Used to change color based on selected turn time
// Timing settings
const unsigned long HOLD_TO_FINISH_INTERVAL = 2 * 1000; // How long to hold button when making final selection for time
long turnTime = 0; // Manages the turnTime - determined in setup(), used in loop()
// Flag set in ISR to indicate a button press
volatile boolean buttonPressed = false;
/************************************************************
ISR: Actions to take on button press
***********************************************************/
void buttonPress() {
buttonPressed = true; //flag that button was pressed
}
/************************************************************
LED Effect: Set all LEDs same color
***********************************************************/
void changeAllColorTo(int h, int s = 255, int v = 255) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(h, s, v);
}
}
/************************************************************
LED Effect: Blink Red
***********************************************************/
void blinkRed() {
changeAllColorTo(255);
FastLED.show();
delay(300);
changeAllColorTo(0, 0, 0);
FastLED.show();
delay(300);
}
/************************************************************
LED Effect: Fade All
***********************************************************/
void fadeall() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(100);
}
}
/************************************************************
LED Effect: Half Cyclon
***********************************************************/
void halfCylon(CHSV color) {
static uint8_t hue = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
FastLED.show();
fadeall();
delay(10);
}
}
/************************************************************
Signal a time unit has been selected
***********************************************************/
void signalTimeSelected(CHSV color) {
for (int i = 0; i < 10; i++) {
halfCylon(color);
}
}
/************************************************************
Select Turn Time Routine
***********************************************************/
int selectTime(CHSV uncountedColor, CHSV countedColor) {
int timeCounter = 0; // This tracks the button presses, each button press is a time unit
unsigned long previousButtonHoldTime = 0; // Used for determining long button hold time
boolean update = true;
while (true) {
if (update) {
// Set color of each LED based on counted or uncounted
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = i < timeCounter
? countedColor
: uncountedColor;
}
FastLED.show();
update = false;
}
// Increment count when button pressed
if (buttonPressed) {
// Debounce button press
delay(50);
if (digitalRead(BUTTON_PIN)) {
buttonPressed = false; // Reset ISR button flag
timeCounter++;
update = true;
//Rollover timeCounter if max reached
if (timeCounter >= NUM_LEDS) {
timeCounter = 0;
}
}
}
// Check if button held
if (!digitalRead(BUTTON_PIN)) {
// Exit while if button has been held "long" time
if (millis() - previousButtonHoldTime > HOLD_TO_FINISH_INTERVAL) {
signalTimeSelected(countedColor); //Display cylon effect to show selection has been made
buttonPressed = false; // reset ISR button flag
break;
}
} else {
previousButtonHoldTime = millis();
}
}
return timeCounter; //Returns the number of times the button was pressed (less the long hold)
}
/************************************************************
Compute Turn Time
***********************************************************/
long computeTurnTime(long s = 0, long m = 0) {
s = s * 5 * 1000; // 5 seconds for every count
m = m * 60 * 1000; // 1 minute for every count
return s + m; // in milliseconds
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(84);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPress, RISING);
// Set turn time. Select seconds, then minutes.
long secondsCount = selectTime(UNCOUNTED_COLOR, SECONDS_COUNTED_COLOR);
long minutesCount = selectTime(UNCOUNTED_COLOR, MINUTES_COUNTED_COLOR);
turnTime = computeTurnTime(secondsCount, minutesCount);
}
void loop() {
boolean overTime = false;
// As turn time elapses, show a fade from Green to Blue to Red
for (int hue = START_HUE; hue <= END_HUE; hue++) {
changeAllColorTo(hue);
FastLED.show();
delay(turnTime / HUE_INCREMENT);
// If button flag was set in ISR then exit
if (buttonPressed) {
buttonPressed = false; // Reset button flag
break;
}
// If button is not pressed before turn ends, LEDs go into "overtime" mode
if (hue == END_HUE) {
overTime = true;
}
}
// Over Time Mode, All LEDs blink
while (overTime) {
blinkRed();
// If button flag was set in ISR then exit
if (buttonPressed) {
buttonPressed = false;
break;
}
}
} // End loop()
Transcript:
Have you ever wondered how all this electronic stuff that surrounds us every day is actually programmed? Things like your toaster or your microwave or your fridge or whatever electronic widget you might be using. It all has code and electronics behind it. Well, in this lesson, we’re gonna walk through the code that powers this simple turn timer, we’re gonna be using a micro controller development board called Arduino.
Arduinos are like really small computers that are fantastic for helping you build electronics projects. This will be your chance to throw your brain at some code and see how something like this simple turn timer is actually programed. We’re gonna be talking generally about how an Arduino sketch is laid out. So even if maybe some of the code seems a bit confusing to you, I think you’re gonna get a gist for how a program can be organized. We’ll make sure to link to the code in the description.
So, all right, let’s go ahead and jump into this code. So let’s start with the general layout of an Arduino program, and I’m gonna open up a brand new sketch just to show this. So when you open up a new sketch in your Arduino IDE, there’s two functions that get pre-filled for you. One is set up and one is loop. All the code in setup runs first, and it only runs once. And then all the code in the loop, it runs from the top. It goes down executes code. And when it gets to the bottom, it starts over again. So it loops over and over. So in the turn timer program, we’re gonna have a setup and we’re gonna have a loop just like we do in every other Arduino program. Now, what we’re also gonna have is we’re gonna have a space above setup where I’m gonna have some constants. Constants are data used in your program that are not gonna change in the program. These are things that aren’t gonna be changed by the user while you’re using the program. So this is stuff that’s predefined. The other thing you’ll have above the setup are global variables. Global variables are variables that can be used by any function inside of the sketch. We’ll be talking about functions in just a moment, but global variables could be accessed by any function inside of the sketch. For reasons we won’t get into here, you wanna try to limit the number of global variables you have. Now, the other thing we’ll have above the setup, often above the setup that is are some functions. Functions, help you organize your code and they can be called inside the setup or inside the loop. Now usually, but not always, I will write my functions above the setup. Okay, so again, this is the general layout. Again, I’ll have some constants up top, we’ll have some global variables, we’ll have functions and then we’ll have those two main functions, void setup and void loop in that order. So again, setup only runs once and loop runs over and over. So before we jump into the code, let’s just talk briefly about how, what this turn timer does, okay? So think about an egg timer, right? You set the timer of an egg timer. Then it goes tick, tick, tick, tick, and then ring when it’s done with its time, okay? Similar thing. But instead of using like an annoying ring noise and a tick tick, tick, tick, tick, it’s going to do that counting or that time lapse with LEDs. Okay, so the first thing you have to do with any timer is you gotta set the time, right? So we’re gonna turn this thing on and this right here, there’s a, well, I show you real quick. There’s a ring of LEDs in there, okay. And to set the time you just press the button and it turns one of the LEDs on, and then you press another button. It turns another LED on and each LED represents five seconds, right? So right now the timer’s been set for 10 seconds. Okay, so we have two different times we set. One seconds and then one is for minutes. In order to get to the minutes mode, I’m just gonna press and hold this. All right, so I’m pressing and holding. Okay, you’ll notice that little flash. Now I’m in the minutes mode. Now for every time I press this button and release, it’s gonna add one minute. So I’m just gonna press it once. So now we have one minute plus our previous, what was it? 10 seconds, so a minute and 10 seconds. Okay, now what I’m gonna do to get out of minutes phase and actually into the countdown phase, I’m gonna press and hold this. You’ll notice the LEDs change. And now we’ve got just this green color and what’s happening right now is inside our Arduino micro controller has got a clock that’s just ticking down and down. It’s got a timer that is that’s tracking the amount of time that is passed, okay? And what’s happening is the LED colors. Those individually addressable LEDs are changing color. And when they get all the way down to that next color, they’re gonna, it’ll turn red and then start blinking. And that blinking is like that beep, beep, beep or that ring, ring, ring to let us know that, hey, the turn is over the time, the time has been up, okay? Now we’re gonna fast forward this to get us to this red so you can see that. Okay, so now you’ll notice it’s blinking red. And then if I want to restart the timer for the same amount of time, again, this is the same amount of time, ’cause this is supposed to be used during like playing board games, right? Where you’re trying to limit the amount of time each player gets, you just press that again. You pass it and then it starts the time over again, again, minute and 10 seconds or whatever we had set it to. So that’s what we are gonna talk about. This program we look at, we’ve gotta somehow figure out how to do this. So I’m wondering, what do you think right now? What do you think the code for setting the time? Do you think that would be in setup or loop? ‘Cause we only do it once. Hmm, all right. What about this whole countdown stuff? You know like every time you press that, it does something, right? So it’s like, this is happening. This will kind of happen over and over again. Where do you think that code might be, right? Okay, well let’s find out. All right, so here we are in our code. Okay, this is all the code that runs that operation we just showed and it’s just over 200 lines of code. Now, I don’t want you to be angry at me ’cause I’m not gonna go line through line through all of this code. That would take us a really long time. There’s like just a lot of information in this generally, right in this whole like learning how to program, you can learn to do pretty quick, but then you can spend years like getting better at it. And if you’re interested in really like doing a deep dive into how to program Arduino and use all this cool stuff, definitely check out our website, programmingelectronics.com. We’ve got a bunch of training for doing just that. But I do really wanna walk through like the big parts here so that you can get an idea of how this code works and also make sure to check out the code in the description. So you can like, upload it in your own Arduino IDE, maybe make this project yourself and just see how it’s put together. Okay, so here we are at the top of the sketch and what do I have? I have a library, it’s called the FastLED library and people, a group of people spent a bunch of time of their own time put together this really library to control individually addressable LED strips. So the kind I’m using are called NeoPixels, but they go by many different names and you’ve probably seen them in different products or seen ’em around or maybe you’ve programmed ’em before. They’re really cool pieces of hardware. Think of like in a strip of LEDs and you can change the color of each one by writing some code and you can make some really cool effects, it’s pretty neat. And this is really just what I needed when I wanted to work with the hardware that I had. So that’s why I’m using this FastLED library. And you’re gonna see references to this FastLED library. We use code from this library all throughout this entire sketch. So if I say is something from FastLED, you know where it’s coming from again, hey, we get to use this for free that’s what’s so awesome about Arduino is so many people have graciously spent their time. They’ve built out these awesome libraries that you can just use. So like I don’t have the technical chops to dive in and write an awesome library for using these NeoPixels, but obviously somebody else did. So it’s great to be able to lean into their expertise and you can do that for tons of hardware as it relates to our Arduino. Okay, so we have that include right? We said, hey, that’s gonna be at the top of a sketch. Then what do we have here? Well, we’ve got a bunch of constants, okay? Now they’re not all constants. We’ll talk about this one here in just a second. But these constants are things that aren’t gonna change. Now you might be like, “Hey Mike, you talked about those defines, why aren’t you using defines here?” Well, for me, I generally lead defines and constants are doing very similar things. I generally lead towards constants because they have type checking, which basically means if I make an error in how I write something out or a value I assign to something, the compiler’s gonna know to be like, “Hey Mike, you screwed up.” And since I screwed up so much, I like to use the constant for that check, again, constants are things that are not gonna change. So I’ve got this thing called button pin, right? So on an Arduino, when you wanna control electronics, you do that through the different pins, like these electrical connections to these different pins and it’s pin three that I’m using for that button. So like as I’m using that turn timer, I’m never gonna like physically move that pin. It’s like, it’s soldered in here, right? So that’s never gonna change. That’s why it’s set as a constant, okay, so we’ve got all these different constant set. This CHSV is a way for setting the color inside the FastLED library and so that’s what we’re doing here. I just set some default colors the red, the blue, the green, the yellow, and just some more like color setting stuff in here with these constants. Now I’ve got some global variables kind of sprinkled in here. You’ll notice I kind of organized these under pins, LED array, colors used, colors for hue, timing. So I’ve kind of tried to organize ’em by use case up here. So before when I said you’ll see your constants and defines, and then you’ll see your global variables. Well, that’s not always the case. These can be intermixed and you know, that’s kind of what I did here. I just in intermixed ’em right here, ’cause I tried to organize ’em by function. So I’ve got the pins, the LED array, colors used when setting times, colors and hues used during each turn, some timer settings. And then I’ve got a variable used with the interrupt service routine down here. All right, so this is a really important variable right here that we’ll refer to quite a bit. This is an array that is holding a CRGB object. And this object again is defined by in this FastLED library. And it’s basically think of it as like a control panel for each LED, right? So we’re trying to control individually all of these LEDs and each LED has its own control panel. And that’s what this guy is right here. And so, we’re just making an array of these CRGB objects. So now as I go down, you’ll see, we run into a bunch of functions. So this is where all my functions start and I’m not gonna talk about each function right now. We’ll talk about ’em as we run into them in the setup, in the loop. So let’s get down to the setup and the loop. All right, so here we are in the setup. And if you recall, setup only runs once. So what’s the stuff we might want to do here. Well, if you’ll recall, one of the things we wanted to do was set the time that only happens once. We’re gonna do that in here, but before we get there, let’s just talk about what we got going on in setup. So I’m starting co communication. And then I have the all important line right here, FastLED add LEDs. And what this does is set up our NeoPixel strip. Now, if this looks a little odd, you’re not used to seeing something like that in a function call. We won’t go into it now, but basically it’s allowing us to specify the type of individually addressable LED strip you might be using. So here I’m using NeoPixels, but there’s so many different kinds out there and you can use all different types with this library. So you’re telling them what kind here, and then just the pin you’ll be using to control those LEDs is what you put here. Then we’re passing in LEDs. This is that array I talked about before that was holding the control panels for each LED. So a really important array. And then just the number of LEDs on the strip. So this is kind of setting everything up for us, here we’re just setting the brightness like a default value for the brightness of the LEDs. We’re setting a pin mode for our button pin. And then here we attach an interrupt service routine. So an interrupt service routine or ISR is a piece of code that you wanna have precedence over other pieces of code in specific situations. So basically like it’s a really important thing that’s time sensitive that you need to happen at a specific moment when, so something happens, right? So for us, it’s the button press, right? We wanna know when that button is pressed and we wanna know now, right? So we are using an interrupt service routine, which let’s just take a quick look at it, was the first function we ran into here’s our ISR. And all it does is set a variable to true. This is a variable I’m using as a quote unquote flag, right? And when button pressed is set equal to true, it’s just letting the program know that, hey, the button was pressed and then we’ll handle that inside the loop or in other functions, right? So all we do with this ISR is say, hey, this button pressed variable, which happens to be a global variable and can be used by all the other functions in our program. We’re just setting that equal to true. And then that will have an impact in the rest of our code. And then as the rest of our code evaluates, it’s gonna do things whether or not the button was just pressed and other pieces of the code will be setting button pressed equal to false, right? So it’s a flag that’s gonna be either true or false, okay? So there’s that ISR, after that we get into setting the time. So I’ve got a function here called select time. Notice I call this function here and I call the function here. So we use it once for setting seconds. And then we use that same function again for setting the minutes. And the only thing that changes here are some colors that we pass. And so these are actually colors that we want to see. So when we set the seconds, it’s one color. And when we set the minutes, it’s a different color. So let’s take a quick look at this select time function, which is right up here. I think this is the beefiest function. All right, so here’s this select turn time routine, right? This function that I wanna run. So you have to tell it what colors you wanna turn the NeoPixels, and then it’s gonna return a integer. And the integers gonna be the number of times the button got pressed, right? So what do we have to do inside this function? Well, we have to see how many times did you press the button, we need to update the LED and NeoPixels, right? Based on that count. So every time you press the button, you need to turn on another LED and then we also need to check like, hey, when are we done actually setting the time? And we do that with a long button press. So those are kind of the things we have to do inside here. And I won’t go into it in two depth. Again, you can check out the code, but those are the three things that we try to do in here. So we’ve got some variables, the track the number of time, the button was pressed that track how long the button was being held, and then whether or not this update variable tells us whether or not we need to update the LED strip again. So we’ve got this while true statement going on in here. And what we’re doing is we are gonna continue checking to see if the button was pressed, updating the LED color of the array, and then checking how long we held the button. So we’re gonna do those three things over and over and over again. And the only time we’re gonna stop is if the button has been held for a certain amount of time. So this line of code right here is checking to see, hey, have you held the button for long? I think it’s two seconds is what that hold to finish interval is that was one of the constant which we set. So if we’ve held it for longer than two seconds, then what we’re gonna do is we’re gonna set that button pressed equal to false, there’s that resetting that ISR flag. And then we’re gonna use a break, when we break that allows us to get out of setting the time. So you can check out the code and see how we updated the specific colors here. And when we’re pressing the button, we increment the counter and we’re setting the update to true to say, hey, the button was pressed. We need to update the LEDs again. But generally that’s what’s being done here. And then once we get to the end, we just return the number of times we press the button. Okay, so that is the select time. I know that was quick. Again, I’m trying to hit the highlights of the code. If there’s anything specifically that we’re going over, that you want, you have questions about, make sure to leave that note about that in the comments. So we’re doing that once for the second, right? So we’re gonna get a value back and it’s gonna say, hey, this is the count we had for the seconds, and we’re gonna get another value back for the minutes, right? So once we exit the select time for seconds, we just jump into the select time for minutes. Once we exit the minute count, then what we do is we set this turn time variable and turn time was one of the other, actually the third global variable that we used. And here, we use a compute turn time function. We’re gonna send in the seconds and the minutes that we just got right here, and what does that function look like? Here’s compute turn time, again, it’s taking the seconds and the minutes count, and it’s gonna return a long and that’s gonna be the actual length of the turn. So we’re just doing a computation in here. This is saying for every button press, when we’re setting seconds is gonna be five seconds. And then the way I did it here, I mean, I there’s so many different ways to do it, but basically for the first 10 LEDs, it’s one minute for every LED and then for every LED after that it’s five minutes, right? So we’ve got a total of 16 LEDs. So that allows you to set a longer turn if that makes sense. I don’t know if that makes sense or not, but it sort of did to me. And then once you get done, you just take all the seconds and minutes, add those together and return it back. So when we’re done with the setup, now we the total turn time, and when setup’s over, it only runs once and then we get into the loop. So now here we are in the loop. And would you believe me if I told you that I think the loop is actually a lot easier than the setup? What do we actually have to do in the loop? Well, what we do in the loop is we change the color of all the LEDs. We start at green and we go through a series of colors until we get to red. So we’ve gotta fade the LEDs through a specific hue to get to red. And then once we’ve gotten to that color, right, and we’re gonna do that over a period of time. So if the turn was say, one minute, then over the course of 60 seconds, we need to make sure that the hue is changing. So the color changes slowly from green through a spectrum to red, over the length of the turn. And then once the turn is over, we need to blink all of the LEDs red. That’s like, hey, your turns up, let’s blink red. And then finally, what we have to do in the loop is if the button button is ever pressed, we need to start the timer over. So those are kinda like the three things we need to do. So we gotta fade all the LEDs. We have to blink them if we’re over time, and then we need to reset the timer when the button’s pressed. So to do this, I started off by using a variable called overtime, and I said, it equal to false. And this variable’s gonna track whether or not the turn time is up. Like, have we exceeded the turn time? And if we have not exceeded the turn time, it’s false. If we have exceeded the turn time, I set it equal to true. And I just use it as a flag to tell other parts of my code, whether they should execute or not. So we start with a for loop. So what this for loop is gonna do is it is gonna change the colors of all the LEDs through a spectrum. And it’s gonna do that over the course of the turn. So if it’s a short term, then it’s gonna more quickly change the colors from green all the way to red. If it’s a long term, then it’s gonna slowly increment the colors from green all the way through to red, the way we’re able to change the colors using the FastLED library. The way I’m choosing to do it is with this value that you can use called hue. And it’s a single value, and it allows you to change through the spectrum by basic incrementing a number. So I have a start hue that happens to be green. And then I have an end hue that is red. And then all I do is I increment that hue number. So that allows me to push through a spectrum from green, all the way to red. So the way the FastLED library works is first, you need to set the color of the LED and then you’ve set the color. Then you use the FastLED show function to actually change the color of all the LEDs. So first you’re telling all the LEDs, hey, I want you to be this color or that color, but once you’ve done all those changes, then you say, all right, let’s show that. Now I wrote a function called change all colors to hue. Let’s go check that out real quick. All right, change all color to. And you’ll see, all it’s doing is a simple for loop. It’s walking through that LED array that we set earlier, and it’s changing how the LED actually looks using that CHSV that we talked about earlier. So it’s putting a new control panel in here with different parameters, one of those of which is the hue. So we change all the LEDs, we show them. So if we start out with green, we set all the LEDs to a specific color. Let’s say, we’re starting out and it’s green. Then we actually show that color. Then what we do is we do a little delay. So I’m just using the delay function here. I take the total turn time that was calculated and set up. And then I divide it by the Hue_Increment, the Hue_Increment you’ll notice it’s all caps. That’s what I use for when I’m making a constant or a define, I use all caps. This is just the distance between the start hue and the end hue in numbers. So this is like the number of increments I have to get to, to go from green to red, that’s what that value is. So I take the total turn time, and then I divide it by the number of increments I have. And that gives us the delay. So that’s how we adjust for a short term versus long term. Now this whole time, I wanna check if that button is being pressed or not. So I’m checking, hey, was the button pressed? If it wasn’t pressed, if this is not true, then I just skip this. If the button is pressed, then we break outta the for loop. We’ll talk about that in just a second, but what if the button wasn’t pressed? Well, then what we do is we check if the hue is equal to the end hue, so what does that mean? End hue, so here’s end hue right here, right? What we’re doing is we’re saying, hey, did we get all the way to the last color? If we got to the last color, then we set overtime equal to true. That means like, somebody is, this is a turn timer, right? So somebody’s just taken a long stinking turn, we’ve gotten to the last color. So now this is like telling us like, hey, we are overtime. We need to start blinking this thing red. Now notice that when we get to end hue, that is the end of the for loop. And we would break out, we would be done with this for loop, right? And then we get to this wow statement. If we get all the way to the end of this for loop, that means overtime will be set true. But if that was pressed at any time, we’ll notice overtime will still be false, right? So overtime is initially set false. If we press that button at any time, it’s still gonna be false. If the button isn’t pressed, it’s gonna be true because they haven’t pressed it there in overtime. So let’s just take a step back and think about this for loop one more time, all right? So there’s two is to get outta this for loop. One way is if you press the button, if you press the button, we’ve got a break statement. You get outta the for loop. And if you get outta the for loop early, then overtime is equal to false. That means somebody just press the button. It’s like, your turn was up, you pass it to the next player. They press the button and we’ll be restarting the counter, right? But overtime it’d still be false. But if you get to the end of the for loop, if hue is equal to that end hue, we get all the way to the end. Then we set overtime to true, this is like the slacker. Like, that’s why we built this turn timer, because it’s like, hey, your turn time’s up. Overtime is true, so what happens after this for loop? Well, we get into a while loop. This while loop only runs while overtime is true. So here, if you press the button when overtime was false. We wouldn’t even do this. We just skip right back up to the top. So what happens when we skip right back up to the top? Well, we start back at green and that’s exactly the behavior we wanted. If you recall, every time you press the button, we start the timer over. So that’s what we do right here. We just say, hey, yep, you press the button. Overtime was false, we don’t go into the while loop. We just start right back at the top. Okay, so what do we do if we’re in overtime mode? Well, as long as overtime is true, we’re gonna stay in this while loop. And what are we doing here? Well, we’re gonna blink red. So, what does blink red look like? That’s again, another function that was written. So blink red is using that other function we wrote. Remember I wrote that function called change all color to, it’s setting the color to red. It’s showing the LEDs and then it just does a little delay. And then it sets ’em all, basically turns the LEDs dark. It shows that they’re all dark and then it does another delay. So that’s all blink red is doing, pretty simple. So we blink red and then we check, hey, was that button flag pressed by chance? Did like somebody say like, hey man, your turn is up, pass the timer, your turn is over? And then the next person pressed the button. So we’re checking for that button press flag. And if it was changed, we set button pressed back to false and then we break out of this while loop and that’s pretty much it. Then you start back up at the top, man, that was like a lot to kind of go over. And I know I didn’t go over everything in super depth. So again, if you have questions about any piece of this specifically, any part of it that seems super confusing, or maybe I know, I don’t think I hit all of the code. There’s some that I didn’t go over. I apologize for that, but mention it in the comments and I can try to walk through specific parts perhaps or something like that. Well, thanks a ton for going through this with me. I hope you found it helpful. I know there were so many details I had to run over. It’s hard to like capture all this in a good, consistent way. And I apologize if it came out clear as mud, but this could be a good chance to take a look at the code kind of walk through it. And hopefully, my overview gave you way in, to kind of like, oh I remember what he was talking about there. I see specifically how that got handled, might be good. And this is probably a fun project to just go out and try to build on your own. Again, if you have questions about any specific part you want me to dive in further about one section or another or a piece of code, just mention it in the comments, I’ll do my best to do that, or I’ll do my best to answer any questions in the comments, or if you have any advice on how this could be made better, please let me know. If you haven’t yet, please like this video and subscribe to our channel. It really helps us get the word out about our channel. Again, if you wanna do a deep dive into the Arduino coding stuff, get into all these nitty gritty details, make sure to check out programmingelectronics.com. We’ve got a training program that walks through all of this stuff step by step. All right, have a great one, bye.
