The Arduino Uno
Here is an Arduino Uno board, the ones we use in class. The digital pins at the top are the ones we use for LEDs. It is important to note that one of the pins - digital pin 13 - has an LED built into the board itself. We use this pin to begin with because we don't need to connect an LED to the separate protoboard.
Blink program
Arduino is an open source software company, meaning anyone can create, share, and change programs used for the hardware. There are hundreds of example programs, so we use many of them to learn the basics and then modify them to create new programs. We started by looking at the basic Blink program in the Arduino examples, so we could learn how to write code for our Arduino. The Blink program makes an LED blink on and off. It seems simple, but it was important for us to completely understand the code and syntax.
Arduino programming basics: The void setup() runs when the Arduino is connected. This includes things that you want to happen once, like initializing pins on the Arduino, as we see here. It's telling the Arudino that we're going to connect something to pin 13 and make it do something. Then we have the next section void loop(). A loop, as its name suggests, goes through the code again and again and again until you stop it. We use a loop here because we want the blinking to keep going, not just turn on and off and stop. First, we set pin 13 to HIGH. This tell the LED to turn on. Then delay(); takes an amount of time in milliseconds. Here we tell it to wait 1000ms (1sec). Then we write to pin 13 and tell it to turn LOW, which turns the LED off. And because of the loop, it now returns to the beginning where we tell pin 13 to turn to HIGH. The final result is the LED blinks at 1 second intervals.
Modifying Blink program to change blink rate
Next, we wanted to change the LED to blink differently to learn how the delays and times work. All we need to do here is change the delay time. After we turn the LED to HIGH, we delay 2000ms, or 2 seconds, before turning it to LOW, and then delaying 500ms, or 1/2 of a second. So now the LED is on for 2 seconds, off for 1/2 second, and keeps going Again, it seems elementary, but seeing this code for the first time can be confusing and it's important we learn the basics.
Modifying Blink to create a pattern with 2 LEDs
We now wanted to add a second LED using the protoboard. In the code, we must initialize the second pin as an output and for this program we used digital pin 12. Then, we just add some code to make a pattern. In our program, pin 13 is on for 500ms. There is a short delay, then pin 12 is on for 500ms, then 50ms, 50ms, 50ms, with short delays between each. This creates the effect of a long blink(on 13) and then long, short, short, short (on 12). And of course, because of the loop it keeps going.
Here is an image of our Arduino and protoboard. The LED on the protoboard is connected to digital pin 12 and we used the pin already on the Arduino itself for pin 13. The red wire goes from pin 13 to the same row as the LED, then we have a resistor after the LED, and the black wire connects back to the ground pin.
Modifying Blink to create a pattern with 4 LEDs
Next, we wanted to make a pattern using 4 LEDs. We connected 4 LEDs onto the protoboard and connected them to digital pins 2, 4, 7, and 12. Again, we must first initialize these 4 pins as outputs in the void setup(); Then, we created a pattern that goes up the row of LEDs and back down. The LED in pin 2 is on for 500ms, then LEDs 4, 7, 12, 7, 4, in that order, are on for 50ms each.
Here is our protoboard and Arudino with 4 LEDs connected. Each LED has a wire from its pin on the Arduino and a resistor after it. Then all of the black wires go to the negative column on the protoboard and a final black wire connects that back to ground on the Arduino.
Blink Without Delay
Next, we learned about the Blink Without Delay program. With this program, we don't use delay(); like we have previously. This means that other code can run at the same time without being interrupted by the LED. We use millis(); and count the number of milliseconds to check if it is time to blink the LED, instead of using delay();. This program is where we learn how to write if/else statements to control the LEDs. This program will be important because we might want to use other things with the Arduino that won't be affected by code for the LEDs.
Modifying Blink Without Delay to create a pattern with 4 LEDs
Our final task for the day was to create pattern using 4 LEDs by modifying the Blink Without Delay program We needed to create variables for each LED in each pin and also for each LED state. In void setup(); we initialize those four pins as outputs. Now we come to our loop: here we used else if statements to tell the LEDs to create our pattern. Else if statements specify a new condition if the first condition is false. If LED 1 is off, then LED 1 turns on. Then if LED 2 is off, it turns on and LED 1 turns off. This continues on so that one LED is off while the three others are on, and the one LED that is off appears to travel down the line.
No comments:
Post a Comment