Arduino Tutorial 6: Functions and Loops
In this project we will be creating a device which does the following:
- Contains 1 RGB LED Light.
- Begins with the light showing Red.
- Fades from Red to Green.
- Fades from Green to Blue
- Fades from Blue to Red
- Repeats.
- For the purposes of this lesson, you MUST create 3 functions. One to fade from Red to Green, another to fade from Green to Blue… Etc.
The end result will look like this: (gif may take time to load)
(If gif is not working, check out the URL here: http://www.northerngdc.ca/img/RGBLed.gif)
Components Needed
- Arduino Uno
- Breadboard
- Various Wires
- 3x 220Ω Resistor (Red, Red, Brown, Gold colored bands)
- A RGB LED (The colorless LED with 4 pins)
The RGB LED
This project uses a different kind of LED light known as an RGB LED. It’s like your normal LED except that you can control it’s color.
You should have one RGB LED in your kits. It’s the colorless LED light with 4 pins shown below:
As indicated on the Diagram, the RGB LED has 4 pins. One for the Red component, one for the Green, one for the Blue, and one for Ground.
By combining different amounts of Red, Green and Blue (hence: RGB) you can create nearly any color in the spectrum!
This is where AnalogWrite will come in. We’ll use it together with this RGB LED to create a light that changes colors on the fly!
HINTS
Please use hints only as you need to! Strive to figure as much out yourself as possible. (To use a hint, simply tap on the boxes below)
First we should start by making the circuit for the device. Once we think we have a circuit that should work, then lets move on to the programming portion.
The Circuit
You are trying to make a circuit that can control an RGB LED light. You’ll need to use the PWM pins in order to use AnalogWrite, and they will need to be connected to the R, G and B pins of the LED itself.
Preferably yes, although you truly don’t NEED to.
The goal here will be to provide different amounts of voltages to the different pins on the LED. analogWrite does this, so why would we need a resistor?
Well, if you’ll recall, PWM (which is what analogWrite uses) only makes it look like we are providing less voltage by alternating between full and low very quickly. This can be damaging to the LED in the long term, since there are moments it will be receiving a full 5v.
It’s better practice here to use 3 resistors. (one for each pin)
Place the resistor between a wire coming from your arduino, and the LED pin. This will restrict the possible voltage that can get through.
Start by looking at the diagram on this page. The longest pin is ground, and you can use that to orient yourself.
This project is actually pretty uncomplicated on the circuit side, so no worries there!
Fine. http://www.northerngdc.ca/img/ArduinoRGB_bb.png
The Program
Aha, hold on there. Lets take it one step at a time. Begin with something you know! Perhaps the setup function?
Well to begin with, as with any sketch there’s a few things we should do
Think about creating some variables for your 3 pins. perhaps something like:
int redLED = 11;
After that, I would setup our pins to be used as outputs using the pinMode statement.
The RGB Led takes in 3 values in the form of voltages. One for red, one for green, one for blue.
If the red line has lots of voltage, and the green and blue lines have no voltage, it will appear red.
So, lets just do some really simple code and say:
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);
do all of this in the setup function (So it only happens once when we start) and we should be good to go!
The loop should simply call 3 functions (which you will make) and delay between each call. One function will fade from red to green, the next from green to blue, and finally from blue to red.
Outside of both loop and setup, write something like the following:
void yourFunctionNameHere(){
}
Any code inside the curly braces will be called when you write “yourFunctionNameHere()”
We’ll need to use a loop here. You can either use a while loop or a for loop, but for loops will be cleaner in this case.
A crash course on for loops can be found here: https://www.programiz.com/c-programming/c-for-loop
The maximum value you can output to a pin is 255. So this for loop should iterate between 0 and 255.
If you are fading from red to green, then you would make sure in the loop you set the red value to be a little lower, and the green value to be a little higher each time.
Here’s an example that may help:
for(int brightness = 0; brightness < 255; brightness++){
analogWrite(redPin, 255 – brightness);
analogWrite(greenPin, brightness);
}
Fiiiiiiiiiine! Remember to right click and choose “Save Link As” Arduino Lesson 6 Code Here
Recent Comments