To get a series of 8 LEDs to turn on is sequence over a 1 second span. Then get this same series to turn off in sequence over a one second span. Finally cause this on and off pattern to repeat as long as the Arduino Uno is receiving power.
Equipment:
- 1x Arduino Uno
- 1x Breadboard
- 10x Wires
- 8x 330 Ohm resistors
- 8x LEDs
Reference:
http://www.oomlout.com/a/products/ardx/circ-02
Program Details:
The second experiment adds on to what we learned in the first experiment. Now instead of 1 LED, there are 8 LEDs; which turn on and off in a pattern. No new hardware is introduced in this experiment, it only increases the quantity of items we need to use.
A few new concepts are added in the programming section. This includes for() loops, which are used to repeat a section of code until the condition is met. This can be seen in the setup(), where the for loop is used to quickly set pins 2 - 9 to output. Arrays [] are also introduced, which are used to store groups of similar variables. This is used to make the management of variables easier, as sometimes a large amount of individual variables can be hard to track. This can be seen before the initial setup, as the ledPins[] array is used to hold the pin #s which are meant to output.
Finally to make the circuit, we pretty much followed what the diagram showed us. Two ground connections went to the (+) and (-). Then four resistors connect to each ground connection, giving a total of 8. These 8 resistors are then connected to the negative end of the LEDs. The power for the 8 LEDs is provided by wires which connect from Pins 2 - 9 directly to the positive end. Each LED gets one pin, meaning they each initially receive 5 volts.
Time to Complete:
5 minutes to code, 15 to build
Results:
Again, this is another fairly simple circuit; I had no difficulties when I put everything together. The LEDs blinked as they were supposed to.
Photo:
Tips:
The two ground columns are not necessary, as only one will suffice. Also make sure you color code your wires, as it can get confusing which wire does what when there are ten all jumbled up.
Further Work:
There are ALOT of different patterns you can try with the LEDs. This can be done by adjusting the order in which they light up, and how long they light up for.
Program Modifications:
For the code, I did delete all of the commented out loops; as they took up space and were not actually being applied in the pattern I am doing. I did replace the oneAfterAnotherNoLoop(); with oneAfterAnotherLoop();. I did this because it takes much less space this way, and is much more efficient.
Program:
//This array sets which pins will be used to output
int ledPins[ ] = {2,3,4,5,6,7,8,9}; //Creates an array of integers containing values 2 through 9
void setup()
{
for(int i = 0; i< 8; i++) //loops 8 times for the 8 indexes in array ledPins
{
pinMode(ledPins[i], OUTPUT); //Sets all of the 8 pins to Output. Indexes 0 - 7
}
}
void loop()
{
oneAfterAnotherLoop(); //Calls the method oneAfterAnotherLoop
}
void oneAfterAnotherLoop() // Method (no return)
{
int delayTime = 100; // Creates new int with value of 100
for(int i = 0; i < 8; i++) // Loops 8 times, counts forwards
{
digitalWrite(ledPins[i], HIGH); // Sets the pins to output 5V in sequence
delay(delayTime); // 1/10th second delay
}
for(int i = 8; i > 0; i--) // Loops 8 times, counts backwards
{
digitalWrite(ledPins[i], LOW); // Sets pins to ground in sequence, backwards
delay(delayTime); // 1/10th second delay
}
}
No comments:
Post a Comment