JeremyBlum.com

v3.1

Tutorial 2 for Arduino: Now with more Blinky Things!

| 158 Comments

This tutorial was featured on the official Arduino blog on 3/9/2011

That’s right folks, it’s Monday, and the second installment of my arduino tutorial series is here! I spent the last week in Disney World, but now that I’m back, it’s time to get down to business! This week, I’ll show you how to read a push button, debounce its input using software, how to create a function in the arduino programming environment, and how to change LED brightness using Pulse Width Modulation (PWM). If you have no idea what PWM is, or if you are confused about analog and digital signals, go check out my TechBits episode on the topic first! Don’t forget to go browse the element14 Arduino Group; it’s a great place to post questions, and learn from others!


You can download the files associated with this episode here:

GNU GPL License Distributed under the GNU General Public (Open-Source) License.
Please Attribute and Share-Alike.


158 Comments

  1. Hi Jermey,

    The GROUND terminal of my board is not working properly. what actions can be taken to rectify this defect

  2. Hi there Jeremy, I just started reading your book and it’s awesome, but… im having some trouble understanding one thing in these line code:

    currentButton = debounce(lastButton);

    I understand that “currentButton” will be set as “debounce(lastButton)” but what’s is really the function of “lastButton” being on parenthesis as an argument.

  3. Hi Jeremy, thanks a ton for these posts and the Element 14 sponsored vids on youtube.
    I’m desperately trying to get a button counter to turn on successive banks of LED’s being, rather than stepping up the pulse width to a single LED.
    In my mind it was going to be a “simple” addition of some more pin variables as outputs (not just a single ledPin, but also an ledPin2, ledPin3 etc…. etc… for each “bank” of LED’s being turned on).
    Then I just had to define those integer variables as OUTPUTs, change the incrementation of ledLevel to +1 after each press instead of +51….and put “if” logical checks in the loop to see if the button counter ledLevel was at such and such a count, tun on an additional bank of LED’s…..this time with a digitalWrite to the pin for that bank….instead of the analogWrite used in circuit # 4 of your tutorial 02 here.

    It made perfect sense, it even compiled, but even after trying curly braces in different places to segment the code, I can’t seem to get it to work. The LED banks all just spontaneously turn on, and won’t turn off.

    I’ve seen a lot of button count registers but seem to have a hit a wall with getting the count to equate to a new bank of LED’s switched on with each passing button press….until a final press turns them all off.

    Any ideas?
    Here’s my modified code…from your original…
    /*
    Arduino Tutorials
    Episode 3
    Switch4 Program (pwm)
    Written by: Jeremy Blum
    */

    int switchPin = 2;
    int ledPin = 13;
    int ledPin2 = 12;
    int ledPin3 = 11;

    boolean lastButton = LOW;
    boolean currentButton = LOW;
    int ledLevel = 0;

    void setup()
    {
    pinMode(switchPin, INPUT);
    pinMode(ledPin, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    }

    boolean debounce(boolean last)
    {
    boolean current = digitalRead(switchPin);
    if (last != current)
    {
    delay(10);
    current = digitalRead(switchPin);
    }
    return current;
    }

    void loop()
    {
    currentButton = debounce(lastButton);
    if (lastButton == LOW && currentButton == HIGH)
    {
    ledLevel = ledLevel + 1;
    }

    lastButton = currentButton;

    if (ledLevel = 1);
    {
    digitalWrite(ledPin, HIGH);
    }

    if (ledLevel = 2);
    {
    digitalWrite(ledPin2, HIGH);
    }
    if (ledLevel = 3);
    {
    digitalWrite(ledPin3, HIGH);
    }
    if (ledLevel > 3) ledLevel = 0;
    {
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);

    }
    }

Leave a Reply

Required fields are marked *.