Mission 2 - Scrolling LEDs
 DuinoKit Essentials - View Only. See new "Mission Manual" forum for manual and code   Started by Daniel Alich   2017-06-27 21:48:51 -04:00   Comments: 6    Viewed: 5338

  1. Daniel Alich
    Daniel Alich Member Staff Member
    252_1511832877_Mission 2 - Scrolling LED

    /*
    Loop
    * Lights multiple LEDs in sequence, then in reverse. Demonstrates
    the use of a for() loop and arrays.
    * http://www.arduino.cc/en/Tutorial/Loop
    */
    int timer = 50; // The higher the number, the slower the timing.
    int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers used for LEDs
    int num_pins = 6; // the number of pins (i.e. the length of the array)
    void setup()
    {
    int i;
    for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
    pinMode(pins[i], OUTPUT); // set each pin as an output
    }
    void loop()
    {
    int i;
    timer=map(analogRead(A0),0,1023,1,50); // The Map function
    for (i = 0; i < num_pins; i++) { // loop through each pin...
    digitalWrite(pins[i], HIGH); // turning it on,
    delay(timer); // pausing,
    digitalWrite(pins[i], LOW); // and turning it off.
    }
    for (i = num_pins - 1; i >= 0; i--) {
    digitalWrite(pins[i], HIGH);
    delay(timer);
    digitalWrite(pins[i], LOW);
    }
    }
    Daniel Alich, 2017-06-27 21:48:51 -04:00
  2. What is this part of the code doing? Is there a place I can read more about the fucntions and concepts explored with this line of code?

    timer=map(analogRead(A0),0,1023,1,50); // The Map function
    Demian Vanderputten, 2017-11-29 14:15:30 -05:00
  3. Damian,

    I am new to the Arduino, but have been writing code since 1977. Run the code "as is" first. Then edit the code by commenting out the mysterious line of code as follows (insert two slashes and a space in front of the code):

    // timer=map(analogRead(A0),0,1023,1,50); // The Map function

    This causes the compiler to ignore the line. Save the edited code, compile it and run it. Note the difference in behavior of the six LEDs compared to the original code. The LEDs blink sequentially, in a steady, metronomic rhythm. Previously, the LEDs blinked sequentially, but the blink rate changed randomly.

    The purpose of the line of code is to use analog pin zero (A0) as a random number generater that changes the blink rate at the start of each loop. When an analog pin is not set and is not connected to anything, its value is random and varies from 0 to 1023. The Map function takes the current value of A0, forces it into a range from 1 to 50 and assigns that value to the variable "timer". If this line is commented out, the value of "timer" is always 50 (50 milliseconds), because it was set to that value in the first line of code as follows:

    int timer = 50; // The higher the number, the slower the timing.

    I hope this was helpful.

    David
    David Kutzler, 2017-12-29 22:20:54 -05:00
  4. Damien,

    See my reply above for the purpose of the Map function. Here's a way to make this "mission" more interesting. You can combine what you learned on this mission with what you can learn on mission #5, about using a potentiometer to be able to directly control the blink rate in the bank of six LEDs. Wire up the DuinoKit board as shown above, but add three more connections:  Connect one side (it doesn't matter which one) of either potentiometer to the 5-volt pin on the Arduino, connect the other side of potentiometer to a ground pin, and connect the middle pin of the potentiometer to analog pin zero ("A0"). Now copy and paste the code above into the Arduino IDE, without changing it, compile it and upload it to the Arduino. Once the LEDs light up, see what happens when you turn the potentiometer. The blink rate of the six LED bank will change with the potientiometer setting.

    In the original setup, analog pin A0 was not connected to anything, so its voltage varied randomly. Now that it's connected to the potentiometer and a 5-volt source, the setting on the potentiometer determines the voltage. The processor on the Arduino is a digital device, that doesn't really understand what the voltage in an analog pin means. But, there is something in the Arduino board, called an Analog to Digital (A/D) converter, that interprets the voltage received by an analog pin into a digital value that ranges from 0 to 1023. The code looks at the digital value that is found on analog pin A0 and the Map function forces the range from 0 to 1023 into a proportional value from 1 to 50. The code then uses that value for the time delay. By turning the potentiometer to less resistance (or higher voltage to pin A0), you increase the length of the delay, and vice versa. Now see what happens if you change the "50" in the Map statement to 100, save it, complie it and upload it.

    Have fun!
    David Kutzler, 2018-01-02 23:24:33 -05:00
  5. I tried to complete Mission 2 but am getting errors after copying the code.  Not sure what's wrong with the code I was told to copy.  New to arduinos and coding.  Please help.
    Fred Kirchner, 2019-11-05 16:11:02 -05:00
  6. Elisha Wu
    Elisha Wu Member Revesby
    Just look at the errors, solve the bugs like this wasn't verifyed
    Elisha Wu, 2020-06-25 19:38:48 -04:00
  7. (You must log in or sign up to post here)

Report Post

Write your reason

Log in | Sign up
Terms & Conditions!
Help!