Learn Programming and Electronics with Arduino

Learn Programming and Electronics with Arduino

By Programming Electronics AcademyTechnologyEducationCourses
Download on the App Store

Learn Programming and Electronics with Arduino episodes

  • How to Make One Button Have the Functionality of Two or More with Arduino

    Do you have an application where you want multiple buttons for different user inputs? Maybe you have a timer and you want one button for minutes and another for hours.

    But there is a problem – you only have room for one button!

    In this tutorial, we are going to use Arduino to explore how to make one button have the functionality of two or more.

    Click here to join our 12-part HD Video Course.

    You Will Need:

    (1) Momentary push button

    (5) Jumper wires

    (1) Solderless breadboard

    (2) LEDs

    (2) 220 Ohm resistors

    Set Up The Circuit:

    To demonstrate making one button have the functionality of two or more, we will set up a simple circuit with 2 LEDs and a button. Based on how we press the button, different LEDs will illuminate.

    Follow the instructions and schematic below to get the circuit set up before we dive into the mechanics of the Arduino code.

    1. Using a jumper wire, connect any GND pin from the Arduino, to the ground rail on your breadboard.
    2. Place an LED on your breadboard, make sure to note which way the long leg is facing.
    3. Using a jumper wire, connect pin 13 from your Arduino to the breadboard in the same channel where you have the long leg of the LED attached.
    4. Now connect one side of the 220 Ohm resistor to the short leg of the LED, and connect the other leg to the ground rail on the breadboard. The orientation of the resistor doesn't matter.
    5. Repeat this using pin 12, and another LED and resistor.
    6. Finally, place your push button on the breadboard. Depending on the style of your pushbutton, they often fit well straddling the long trench that goes through the breadboard.
    7. Connect a jumper wire from one side of the button to pin 2 on the Arduino.
    8. Connect a jumper wire from the other side of the button to the ground rail on the breadboard.

    That's it for the circuit setup. Now, when you press the push button (which will electrically connect both sides of the button), pin 2 to will have ground voltage applied. We will use this ground voltage input to trigger our different functions.

    Examine the Sketch:

    There are couple ways to implement the multi-function button press using Arduino.

    One way is to have the number of presses determine the output. For example, a single click might highlight the "hour" field of an LCD timer and a double click might highlight the "minute" field of the display.

    Another way that we can implement multiple functions with one button is for the user to hold down the button for different lengths of time with the length of the hold determining the output.

    For example, if the user holds the button for half a second and releases, something happens. If she holds it for 2 seconds, something different happens.

    This latter method of using button hold length time to determine separate functions is the strategy we will learn here.

    Before I go any further though, I would like to thank Steve for creating the base Arduino code that we will be using. Steve is a member of the Premium Arduino course (a couple of months ago, he was new to Arduino).

    While creating a home automation project, he was in need of using a single button to do multiple things, and came up with a very simple way to make it happen. Thanks Steve!

    Here is the complete sketch, I recommend looking it over first, and then we will discuss it piece by piece below.

    /*Using a Single Button, create mutliple options based on how long the button is pressed The circuit: * LED attached from pin 13 to ground through a 220 ohm resistor * LED attached from pin 12 to ground through a 220 ohm resistor * one side of momentary pushbutton attached to pin 2 * other side of momentary pushbutton attached to Ground * Note 1: on most Arduinos there is already an LED on the board attached to pin 13. * Note 2: In this circuit, when the button is pressed, Ground Voltage is what will be applied. Created DEC 2014 by Scuba Steve Modified JAN 2015 by Michael James Both members of https://programmingelectronics.com This code is in the public domain */ /////////Declare and Initialize Variables//////////////////////////// //We need to track how long the momentary pushbutton is held in order to execute different commands //This value will be recorded in seconds float pressLength_milliSeconds = 0; // Define the *minimum* length of time, in milli-seconds, that the button must be pressed for a particular option to occur int optionOne_milliSeconds = 100; int optionTwo_milliSeconds = 2000; //The Pin your button is attached to int buttonPin = 2; //Pin your LEDs are attached to int ledPin_Option_1 = 13; int ledPin_Option_2 = 12; void setup(){ // Initialize the pushbutton pin as an input pullup // Keep in mind, when pin 2 has ground voltage applied, we know the button is being pressed pinMode(buttonPin, INPUT_PULLUP); //set the LEDs pins as outputs pinMode(ledPin_Option_1, OUTPUT); pinMode(ledPin_Option_2, OUTPUT); //Start serial communication - for debugging purposes only Serial.begin(9600); } // close setup void loop() { //Record *roughly* the tenths of seconds the button in being held down while (digitalRead(buttonPin) == LOW ){ delay(100); //if you want more resolution, lower this number pressLength_milliSeconds = pressLength_milliSeconds + 100; //display how long button is has been held Serial.print("ms = "); Serial.println(pressLength_milliSeconds); }//close while //Different if-else conditions are triggered based on the length of the button press //Start with the longest time option first //Option 2 - Execute the second option if the button is held for the correct amount of time if (pressLength_milliSeconds >= optionTwo_milliSeconds){ digitalWrite(ledPin_Option_2, HIGH); } //option 1 - Execute the first option if the button is held for the correct amount of time else if(pressLength_milliSeconds >= optionOne_milliSeconds){ digitalWrite(ledPin_Option_1, HIGH); }//close if options //every time through the loop, we need to reset the pressLength_Seconds counter pressLength_milliSeconds = 0; } // close void loop Comments:

    At the top of the sketch, we find the comments. You should make it a habit to read the comments in a sketch before jumping into the mechanics of the code. The comments should lay the groundwork for what is going to happen in the program and will help you interpret the intent of the code as you begin to analyze it.

    Declare and Initialize Variables:

    After the comments, we start initializing and declaring variables. Since, we are going to be tracking time, we need to have a variable to record the length of time a button is being held. We do that with the pressLength_milliSeconds variable:

    //We need to track how long the momentary pushbutton is held in order to execute different commands //This value will be recorded in seconds float pressLength_Seconds = 0;

    Now, you might think that the variable name is really long and annoying. And I wouldn't particularly argue with you – I mean, why would I include milliSeconds in the name of the variable?

    The reason I do this is because I think including the unit of measurement in the variable name is helpful when other people are trying to read your code. Writing code that other people can read is not only good for other people, but also future versions of yourself who forget what the heck you were thinking when you wrote the code! [End Rant]

    The next thing we need to set up are the parameters for when options will get executed. In this example, I have two variables for two options:

    // Define the *minimum* length of time, in milli-seconds, that the button must be pressed for a particular option to occur int optionOne_milliSeconds = 100; int optionTwo_milliSeconds = 2000;

    Each option is defined by the number of milliseconds that the button must be held for that specific option to get executed. In order to get my first option to happen, I have to hold the button for at least 100 milliseconds which is pretty much a short tap on the button.

    If I want the second option to happen, then I have to hold the button for at least 2000 milliseconds aka 2 seconds.

    If you wanted more options, you would add more variables here with their corresponding hold times.

    Our final initializations will be to specify pin numbers for our button and LEDs.

    //The Pin your button is attached to int buttonPin = 2; //Pin your LEDs are attached to int ledPin_Option_1 = 13; int ledPin_Option_2 = 12; Setup() the Sketch:

    The setup() for this sketch is pretty straight forward (if it's not straight forward to you, make sure to check out our free 12-part Arduino Course, after which this setup will be very familiar to you).

    We want to make sure that the pin our push button is connected to is set as an INPUT_PULLUP:

    // Initialize the pushbutton pin as an input pullup // Keep in mind, when pin 2 has ground voltage applied, we know the button is being pressed pinMode(buttonPin, INPUT_PULLUP);

    We do this to make sure that the button pin is not floating (if you are wondering what the heck that means, you can read more on that here – but if you just roll with me until we get through this tutorial, you should be fine ).

    We also want to specify the pins that our LEDs are attached to as OUTPUTs, because we will be applying voltages to these pins in order to illuminate them:

    //set the LEDs pins as outputs pinMode(ledPin_Option_1, OUTPUT); pinMode(ledPin_Option_2, OUTPUT);

    Finally, it never hurts to start serial communications for debugging purposes.

    //Start serial communication - for debugging purposes only Serial.begin(9600);

    With setup() complete, now we can jump into the main loop of our sketch…

    The Main Loop():

    We know we are going to have to measure the length of time the button is pressed, and then record it.

    To do this, we use a while statement whose condition requires the button pin to be in a LOW state (remember, when we push the button, pin 2 will have a ground voltage applied).

    //Record *roughly* the tenths of seconds the button in being held down while (digitalRead(buttonPin) == LOW ){

    Once the button is pressed and held, the while statement starts executing. The first thing we do in the while statement is to delay 100 milliseconds, and then record that into our time tracking variable:

    delay(100); //if you want more resolution, lower this number pressLength_milliSeconds = pressLength_milliSeconds + 100;

    Keep in mind the first time through the loop, pressLength_milliSeconds will be equal to 0, so we are just adding 100 to the variable.

    It can be handy to know how long the button has been pressed as you add options. To make this easy, we want to print the current value of the pressLength_milliSeconds variable to the serial monitor window:

    //display how long button is has been held Serial.print("ms = "); Serial.println(pressLength_milliSeconds);

    Let's ignore the rest of the code for a second, and imagine what happens if we keep holding the button.

    The first time through the while loop, we add 100 milliseconds to the time tracking variable and we print that value to the serial port. The next time through loop, we add another 100 milliseconds to the timer counter variable, and print this new value to the serial monitor.

    As long as the button is being held down, then we keep adding time to the pressLength_milliSeconds variable – this is the crux of the program.

    When we release the button, the while statement stops, because the condition is no longer met, and we stop adding time to pressLength_milliSeconds.

    So let's pretend we held the button for three seconds, and then let go - what happens?

    Well, as we discussed, the while statement ends and the next line of code we encounter is an if statement.

    //Option 2 - Execute the second option if the button is held for the correct amount of time if (pressLength_milliSeconds >= optionTwo_milliSeconds){ digitalWrite(ledPin_Option_2, HIGH); }

    The condition of the if statement requires that the time we held the button be longer than or equal to the time we set for option number two.

    If you recall, option number two was set to occur with at least 2 seconds of button press time. Since we held the button for three seconds, this if statement will get executed.

    And all we do is write HIGH voltage to our "option 2" LED, making it illuminate.

    What if we had only held the button for one second – then what would happen?

    If this were case, then the first if statement condition would not have been met, but a subsequent else-if statement only requires the button hold time be 100 milliseconds or more – so the second else-if statement would get executed, which turns on the "option 1" LED.

    //option 1 - Execute the first option if the button is held for the correct amount of time else if(pressLength_milliSeconds >= optionOne_milliSeconds){ digitalWrite(ledPin_Option_1, HIGH); }//close if options

    Basically, if we hold the button a long time, the second option gets executed. If we hold the button a short time, the first option gets executed.

    If we wanted to add more options, we add the longer hold options at the top, and the shorter hold options at the bottom.

    I wouldn't try to squeeze too many options in a small span of time or it might drive the end user crazy trying figure out the timing.

    Nor would I try to add more than three options for a single button within a given context, or else you chance making your potential end user want to beat you up.

    To finish up the sketch, we reset our button press timing variable to zero. This ensures that next time the button is pressed and held, we will start from time zero again.

    Try On Your Own Challenge:
    1. Add another option, which turns off both LEDs. Try adding it before the first option (you will have to adjust the timing) and then after each option.
    2. How tight can you squeeze the option time together? Experiment and determine what is a good rule of thumb.
    Download:

    PDF of this Arduino Tutorial

    16 min
  • Understanding HIGH and LOW Arduino Pin States

    If you are just getting started with Arduino, you might be wondering what the heck all this HIGH and LOW stuff everyone is talking about really means.

    At first I just figured everyone using micro-controllers was just on some type of emotional roller-coaster, until I began to realize that HIGH and LOW are actually abstractions.

    But let us not talk in abstractions - let us talk in concrete numbers and get down to the bottom of what HIGH and LOW actually mean.

    This Tutorial Will Include:
    1. What the heck is a PIN state?
    2. What are the values for Arduino Pin States?
    3. Practical Application: What do we see in practice?
    13 min
  • Floating Pins, Pull-Up Resistors and Arduino

    Floating Pins on Arduino have always been a bit of mystery to me. It just never made much sense. I did this video more for my own sake - just to concrete it in my brain. I hope it can add some solidarity to your understanding as well...

    11 min
  • Throw out your breadboard! Dr. Duino: An Arduino Shield for debugging and developing Arduino projects

    In the last couple of episodes we have talked about Arduino shields and breakout boards. In this video, we will review a specific Arduino shield that makes developing projects and debugging sketches on the Arduino extremely easy - it's called Dr. Duino.Arduino Shield for Arduino Shields

    The Dr. Duino is an Arduino Shield. As would expect, it fits snuggly on top of the Arduino headers and has pin headers of it's own, which can easily accept a shield on top.

    The first thing you notice about Dr. Duino is that it's built like a doughnut - it's got a giant hole in the middle! (FYI - It does not taste like said pastry…)

    There are a couple reasons for this form factor, but foremost is that even after you stack another shield on top of Dr. Duino, you still have easy physical access to all its resources.

    Dr. Duino Hardware Resources

    What resources are we talking about? Dr. Duino has 4 momentary pushbuttons, 5 LEDs, 6 potentiometers, an RS232 connector, and a bunch of access points for 5 volts, 3.3 volts and ground, oh yeah, and a siren (piezo buzzer).

    So how do you access these resources and why would you even care to? Both great questions!

    The core feature that allows us to use all the hardware I just talked about are groups of three "jumper" pins that surround the board.

    Almost every pin on the Arduino is associated with a set of these jumper pins. The middle pin is connected directly to the Arduino pin.

    The pin on the left is connected to the pin header on top of the Dr. Duino. The pin on the right is connected to the one of the hardware resources on the Dr. Duino board.

    With each set of these three jumper pins we get a plastic encased "jumper" - it fits snug on top 2 pins, and electrically connects them together.

    For every Arduino pin that has associated Dr. Duino jumper pins we have 2 decisions:

    1) We can route the Arduino pin to the shield above by connecting the center pin to the jumper pin on the left. This passes the Arduino pin directly up to the shield if you have one attached, bypassing the Dr. Duino.

    2) Or, we can route the Arduino pin to the resource on the Dr. Duino shield, bypassing the shield above. Each pin has a different "resource" or piece of hardware that you can use.

    Here is a quick mnemonic for remembering how to place the "jumper" on the "jumper pins" – Left goes Up (to the shield above), Right goes to Down (to the Dr. Duino).

    Digital pins 5, 6, 10, 11, and 13 have LEDs attached. Digital Pins 7, 8, 9 and 12 have momentary pushbuttons attached. Analog pins A0 - A5 have potentiometers attached and digital pin 3 is attached to the piezo buzzer.

    All these asset are extremely helpful.

    For example, let's say I am prototyping with a motor shield. What I want is for every time I press a button, then a servo moves 180 degrees. Instead of having to bust out a breadboard and jumper wires, I simply move a jumper on the Dr. Duino and I now have a preinstalled button. As long as the shield is not using that pin for anything, it's fair game to route that pin to the Dr. Duino.

    Or, maybe I want to turn a potentiometer to get the servo to move. Once again - scrap the breadboard - just move a jumper on one of the analog pins and you have a built in potentiometer - in fact, every analog pin has its own dedicated potentiometer! Trying to put 6 potentiometers on a breadboard and run jumpers is a major pain - having these built right in and so compact is great.

    Potentiometers are great for simulating sensor inputs too. Say you are developing a project that lights up LEDs based on inputs from a pressure sensor - but you don't have the sensor yet. No worries - just use the potentiometer built in to the Dr. Duino to simulate the input from the pressure sensor.

    Another great resource is the piezo buzzer. Most of us get used to using the serial window to debug our sketches. For example - we use the Serial .print() function to send a value to the serial monitor window when some threshold is met.

    What this ends up doing is dividing our visual attention from the Arduino to the computer monitor. With the piezo buzzer, you can get the same information, but be able to look at your Arduino - it allows you to incorporate more of your senses into the debugging process.

    All of these features add up to a great little development package - it's like a breadboard full of components all connected on a handy shield. So if I am traveling I don't have to pack away a bunch of pieces-parts, I can just throw the Dr. Duino in with my Arduino and go for it.

    A Tool for Learning Arduino Code

    Another great use of these resources is when you are learning to use Arduino. If you are really trying to jump right into the code, then setting up the circuit can sometimes be a hassle, even if its just a button and an LED. (Yes - I am that lazy!)

    With Dr. Duino you can work through practically every sketch in the examples section of the Arduino IDE with only some minor code changes (mostly pin designations changes), without having to add a single piece of hardware.

    That's a time saver (especially when it's hard enough to carve out time to learn). Especially if having to place the components just feels like another barrier to practicing your Arduino coding.

    A Debugging Tool

    So far, most of my love for the Dr. Duino has come from this development aspect, but the Dr. Duino has actually been designed to help you debug your sketches when using shields.

    Microcontrollers (like the Arduino uses) are truly at the threshold of hardware and software. The more you know about both the physical and virtual, the better off you will be.

    Here in also lies a challenge – you can screw up both the hardware and the software.

    A big part of debugging your work is tracking down where the issue exists – is it the copper or code? (Or both !@#$)

    The Dr. Duino's key feature of being able to direct an Arduino pin to or away from a shield you are using enables you to more quickly establish the starting point of debugging.

    Once you master how the rerouting works, the Dr. Duino will be a go-to shield for developing and debugging.

    With such a diverse set of applications it's no wonder Arduino continues to grow wildly popular. Shields like the Dr. Duino make it that much more fun to use Arduino as a rapid prototyping tool.

    Special Bonus

    If you are interested in getting your hands on a Dr Duino, you can buy them directly from the Dr. Duino website.

    The creator of Dr. Duino (Guido Bonelli) has been kind enough to offer Open Source Hardware Group readers a free shipping code.

    Free Shipping Code: OSHWG_RULES (So true…)

    12 min
  • 3 Ways to Use Acceleration in an Arduino Sketch

    This lesson covers three easy ways to implement some form of acceleration in your Arduino sketches.

    It will cover the following:

    1. Linear Acceleration
    2. Exponential Acceleration
    3. Messing Around with the Square Root function

    What is Acceleration?

    Acceleration is the rate of change of something over time. Acceleration would help describe things like:

    • Have fast can a stopped car get to 88mph?
    • If I do a wing-suit jump from the top of Mount Everest, how fast will I be when I am gliding over base camp?
    • How can I gently slow down the movement of a mechanical servo before it comes to rest?

    We can implement acceleration when programming Arduino quite easily with just a couple lines of code.

    The basic concept is that a variable will need to change - how it changes over the course of the sketch is what we are interested in. Below are three examples of how we can code change in variables so they display acceleration.

    Linear acceleration

    Linear acceleration is a constant rate of change. It can be represented by a sloped line on an x-y axis.

    A simple way to use it is to increment or decrement a variable every time through a loop like below...

    for (int i = 0; i

    You can see in the code above, that each time through the 'for loop', the accel variable is decreasing by 100. If accel started at 1000, this is what accel would look like with respect to the number of iterations of the for loop.

    You can see that the accel variable decreases at a set rate over time in a linear fashion.

    Exponential Acceleration

    Exponential acceleration changes over time too, but it does so more dramatically (It's basically a drama queen). At first the rate of change in the variable is small, but once it begins to increase it really takes off!

    You can use the power function built into the Arduino IDE to make a variable change exponentially. The power function syntax is quite easy.

    pow( base, exponent )

    The parameter base is the number that get raised to the power of the exponent. Like 2 to the 5th power. Below is an example of how you might code it to change a variable exponentially over the course of a 'for loop'.

    for (int i = 0; i

    Looking at a chart of accel over the course of the 'for loop', we can see that the way it changes is much different than linear acceleration:

    The theme for exponential growth is that is starts out slow, but once it starts going it really get's going!

    Using the Square Root Function for Acceleration

    The square root function can also provide some flavor to the output from your Arduino. It's syntax is simple:

    sqrt( x )

    It returns the square root of the number or variable that you put in the parenthesis. Below is an easy way to use it in a 'for loop':

    for (int i = 0; i

    Let's look at how accel changes over the course of the for loop(let's say it started at 10,000):

    You can see the a rapid deceleration in the value of the accel variable of the 'for loop'.

    Practice With Acceleration:

    If you want to take a look at what these types of acceleration will do in "real" life, then set up the circuit below.

    You Will Need:
    • (10) LEDs
    • (10) 220 Ohm Resistors
    • (1) Jumper Wire
    • Arduino Board
    • Bread Board
    • Black Marker
    Set Up The Circuit:

    Start by by placing one resistor at pin 2 on your Arduino. Connect the other end of the resistor to the breadboard and then connect the long leg of the LED to the resistor. Place the short leg of the LED in the outer ground rail of the bread board.

    Then add 9 more resistors and LEDs across - you should stop at Pin 11 on the Arduino. Then add a jumper wire from GND on the Arduino to the shared ground on the breadboard. (All LEDs not pictured).

    That is pretty much it for the circuit. Below are the code examples for the different ways to program acceleration that we talked about:

    Here is the code for the linear acceleration: /* Linaer ( Constant ) Acceleration The circuit: * LEDs from pins 2 through 11 to ground created 2006 by David A. Mellis modified 30 Aug 2011 by Tom Igoe Modifeid 21 Jul 2014 by Michael James This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Array */ //Declare and initialize variables // The higher the number, the slower the timing. int timer = 1000; // an array of pin numbers to which LEDs are attached int ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // the number of pins (i.e. the length of the array) int pinCount = 10; void setup() { // the array elements are numbered from 0 to (pinCount - 1). // use a for loop to initialize each pin as an output: for (int thisPin = 0; thisPin Serial Monitor) Serial.print("Back ... LED #"); Serial.print(thisPin); Serial.print(" = "); Serial.println(timer); }//close for //reset timer for next for loop timer = 1000; // loop from the highest pin to the lowest: for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { // turn the pin on: digitalWrite(ledPins[thisPin], HIGH); delay(timer); // turn the pin off: digitalWrite(ledPins[thisPin], LOW); //decrement timer timer = timer - 100; //let's take a look at the numbers via the serial monitor window (Tools > Serial Monitor) Serial.print("Forth ... LED #"); Serial.print(thisPin); Serial.print(" = "); Serial.println(timer); }//close for //reset timer for next for loop timer = 1000; }//close loop Here is the code for the exponential acceleration: /* Exponential Acceleration The circuit: * LEDs from pins 2 through 11 to ground created 2006 by David A. Mellis modified 30 Aug 2011 by Tom Igoe Modifeid 21 Jul 2014 by Michael James This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Array */ //Declare and initialize variables // The higher the number, the slower the timing. float timer = 1000; // an array of pin numbers to which LEDs are attached int ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // the number of pins (i.e. the length of the array) int pinCount = 10; void setup() { // the array elements are numbered from 0 to (pinCount - 1). // use a for loop to initialize each pin as an output: for (int thisPin = 0; thisPin Serial Monitor) Serial.print("Back ... LED #"); Serial.print(thisPin); Serial.print(" = "); Serial.println(timer); }//close for //reset timer for next for loop // loop from the highest pin to the lowest: for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { // turn the pin on: digitalWrite(ledPins[thisPin], HIGH); delay(timer); // turn the pin off: digitalWrite(ledPins[thisPin], LOW); //decrement timer timer = pow(2, thisPin); //let's take a look at the numbers via the serial monitor window (Tools > Serial Monitor) Serial.print("Forth ... LED #"); Serial.print(thisPin); Serial.print(" = "); Serial.println(timer); }//close for //reset timer for next for loop timer = 1000; }//close loop Here is the code for messing around with the square root: /* square root The circuit: * LEDs from pins 2 through 11 to ground created 2006 by David A. Mellis modified 30 Aug 2011 by Tom Igoe Modifeid 21 Jul 2014 by Michael James This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Array */ //Declare and initialize variables // The higher the number, the slower the timing. float timer = 1000; // an array of pin numbers to which LEDs are attached int ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // the number of pins (i.e. the length of the array) int pinCount = 10; void setup() { // the array elements are numbered from 0 to (pinCount - 1). // use a for loop to initialize each pin as an output: for (int thisPin = 0; thisPin Serial Monitor) Serial.print("Back ... LED #"); Serial.print(thisPin); Serial.print(" = "); Serial.println(timer); }//close for //reset timer for next for loop timer = 1000; // loop from the highest pin to the lowest: for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { // turn the pin on: digitalWrite(ledPins[thisPin], HIGH); delay(timer); // turn the pin off: digitalWrite(ledPins[thisPin], LOW); //decrement timer timer = sqrt(timer); //let's take a look at the numbers via the serial monitor window (Tools > Serial Monitor) Serial.print("Forth ... LED #"); Serial.print(thisPin); Serial.print(" = "); Serial.println(timer); }//close for //reset timer for next for loop timer = 1000; }//close loop

    I hope the lesson was helpful. The idea of adding acceleration to a sketch can seem a bit intimidating at first, but as you can see, it turns out to be a rather simple addition of code.

    Try on Your Own Challenge:
    1. Can you make the fade amount of an LED accelerate/deccelerate?
    2. Can you add a potentiometer to the circuit above and find a way to control the amount of acceleration - either linear, exponential or otherwise.
    18 min

About Learn Programming and Electronics with Arduino

From the publisher's feed

Video lessons on learning programming and electronics with Arduino. This is part of our Arduino Crash Course and Arduino Course for Absolute Beginners. It's designed to take someone with little or…