LED Counter

Step 1 - Build the Project

Keeping count of something in code means using a variable. In this sketch, the variable “count” will change its value from 0 to 1 to 2…..to 10 each time the loop of code runs.

Step 2 - Upload the Code

/* * Count to 10 over and over again with LED blinks. * Check out how loops work. */ byte count = 0; void setup() { pinMode(13, OUTPUT); //LED onboard Maker Board } void loop() { //Create a variable i and increase it by one for each loop as long as //it is less than the value of count for (byte i = 0; i < count; i++) { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); } delay(1000); // pause 1000 milliseconds between each count count++; //Increase count by one each time the code loops //When the count reaches 10, reset the count variable to equal 0 if (count > 10) { count = 0; } } //Try replacing the delay(200) with delay(i*100) to use your variable there // Need help? http://www.letsstartcoding.com/help // (c) 2016 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense

Step 3 - Read the Walkthrough

Above the setup, you create the variable by typing the variable type (byte) and then naming it. The variable can be named almost anything you want. The best variables are named clearly.

Why is the variable above the setup? The program has two major parts- setup and loop. The code inside setup is kept separate from the code inside loop. Because the loop needs to update ‘count’ as the program runs, ‘count’ wouldn’t work if put in setup().

Why not put the variable in the loop? When you created the variable, you gave it a value of 0 by typing “= 0;”. If the variable is in the loop, the code will give the variable a value of 0 each time the loop runs. You would never be able to count above 1, as the variable would always reset to 0.

Now the code knows there is an integer called count and it has a value of 0. In the setup, you type that there is an output on pin 13, so the code will send more current to that pin when it is HIGH.

Within the loop(), there is another loop started by the word ‘for’. These loops within loops are an important point, but remember that the void loop() { } is always the broadest loop.

The for loop creates another variable, i. i is a common name for a variable that is used only in a for loop. You can read up on the syntax of the for loop, here it says “there’s a new variable called i. While it is less than the value of count, keep doing this for loop and adding 1 to i”.

So far, you’ve just set up the control structure of the code. Now you get into the actual outputs of the code. The output is a blink on pin 13 that repeats ‘count’ number of times, using the variable.

When i is no longer less than count, the code will drop out of the for loop. The LED will stay off for 1 second, then 1 is added to the variable ‘count’.

Next is a check in the code : Is count greater than 10? If so, make count = 0. If not, start the loop over again.

There is alot going on here! The advantage to setting up these code structures and automatic checks is that, from now on, this code will run exactly as it is written. Being able to rely on your code is important if you’re making something like a burglar alarm!

The gist of the code is:

  • There is a variable called count and will be a number

  • Set pin 13 as an output

  • There is a variable called i and it will increase by 1 until it equals count

  • For the time that i is less than count, blink the LED on and off

  • Increase the count variable by 1

  • Check the count variable. Is it equal to 10? If yes, set count back to 0.