Part 1 - RGB LEDs and buttons
We are going to adapt some existing code to create a random colour generator. Build the circuit and then follow the instructions below.
The circuit:
- Open the 'Button' sketch from the 'File > Examples > 02.Digital' menu in the Arduino IDE
- Change the pin numbers in the sketch to reflect the circuit you have built, try lighting the Red, Blue and Green LEDs inside the RGB LED
- Don't forget to check you have the correct Board and Port selected from the tools menu before hitting the upload button!
- Goto the Arduino website and 'Learning > Reference'. In the functions column you will find 'Random Numbers', check out the 'random()' function
- Adapt the Arduino 'Button' sketch so that it randomly selects one of the colours of the LED when the button is pressed.
- You will need to make sure that the pins you are using for your LEDs are initialised in the setup as outputs so that the Arduino will give them sufficient power to light them brightly e.g
pinMode(6, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT);
+ Hint 1
We want to use random() to control the ledPin number, it will no longer be a constant (unchanging) number therefore we need to remove the 'const' from the front where we define our pins and variables at the top of the code. Pay attention to the reference example for using random(), including creating a variable to hold the number above the code.
+ Hint 2
We need to use the random number we have created to select one of our pins, for example: if our random number is 0 choose ledPin 10.
+ Hint 3
When we hold the button it will keep creating random numbers and make the LED turn on all colours and go white, so we need to create a flag to check whether we actually need a new random number for our ledPin variable or should stick with the same one. This can just be a boolean variable that is true if we need a new random number (i.e when the button has been released) or is false when the button remains pressed.
+ The solution
Here's a way using a nested if statement to select the right pin, you could also make use of a switch case if you wanted to. Copy and paste into you Arduino IDE, it may be easier to read.
/* * We are going to use the button example sketch as the basis to * create a sketch that will randomly choose between our red, * green or blue LED when a button is pressed. * * Luke Woodbury 9 Jan 2017 * dotLib.org * */ // constants won't change. They're used here to // set pin numbers: const int buttonPin = 12; // the number of the pushbutton pin //Our ledPin will now be changed dynamically by the code so we need to //remove the 'const' from the start of the declaration int ledPin = 10; // the number of the LED pins //we want a flag to check and see if we need a new random number //we create it as a 'boolean' rather than an 'int' as it only //needs to represent two values, true or false, 0 or 1 boolean flag = 0; //something to hold our random number int randNumber = 0; // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pins as outputs, or they will be dim: pinMode(6, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); //here we check our flag to see if we need a new random number //and use it to select the right pin if (flag == 0){ //set the flag so we dont change the ledPin until the button is off flag = 1; randNumber = random(3); //use the 'random function //a nested if to select the right pin numbers if (randNumber == 0){ ledPin = 10; //Red pin } else if (randNumber == 1){ ledPin = 9; //Blue pin } else{ ledPin = 6; //Green pin } } // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); flag = 0; //reset the flag } }