LED show!!
If you press a button, can you guess which color the rgb LED will be?
Components and supplies
3
Resistor 221 ohm
1
RGB Diffused Common Cathode
3
Button
12
Jumper wires (generic)
1
Breadboard (generic)
1
Arduino Mega 2560
Apps and platforms
1
Arduino IDE
Project description
Code
my code
c_cpp
1const int button1 = 2; // the number of button 1 pin 2const int button2 = 3; // the number of button 2 pin 3const int button3 = 4; // the number of button 3 pin 4const int red = 8; // the number of the red pin 5const int green = 9; //the number of the green pin 6const int blue = 10; //the number of the blue pin 7 8// variables will change: 9int a = 0; // variable for reading the pushbutton status 10int b = 0; 11int c = 0; 12 13void setup() { 14 Serial.begin(9600); 15 // initialize the LED pins as an output: 16 pinMode(red, OUTPUT); 17 pinMode(green, OUTPUT); 18 pinMode(blue, OUTPUT); 19 // initialize the pushbutton pins as an input: 20 pinMode(button1, INPUT); 21 pinMode(button2, INPUT); 22 pinMode(button3, INPUT); 23} 24 25void loop() { 26 // read the state of the pushbuttons value: 27 a = digitalRead(button1); 28 b = digitalRead(button2); 29 c = digitalRead(button3); 30 31 // Show the state of pushbuttons on serial monitor 32 Serial.println(a); 33 Serial.println(b); 34 Serial.println(c); 35 36 // check if the pushbutton is pressed. 37 // if it is, the buttonState is HIGH: 38 if (a == HIGH) { 39 // turn LED on: 40 digitalWrite(red, HIGH); 41 } else { 42 // turn LED off: 43 digitalWrite(red, LOW); 44 } 45 if (b == HIGH) { 46 digitalWrite(green, HIGH); 47 } else { 48 digitalWrite(green, LOW); 49 } 50 if (c == HIGH) { 51 digitalWrite(blue, HIGH); 52 } else { 53 digitalWrite(blue, LOW); 54 } 55 // Added the delay so that we can see the output of button 56 delay(100); 57}
my code
c_cpp
1const int button1 = 2; // the number of button 1 pin 2const int 3 button2 = 3; // the number of button 2 pin 4const int button3 = 4; // the 5 number of button 3 pin 6const int red = 8; // the number of the red pin 7const 8 int green = 9; //the number of the green pin 9const int blue = 10; //the 10 number of the blue pin 11 12// variables will change: 13int a = 0; // 14 variable for reading the pushbutton status 15int b = 0; 16int c = 0; 17 18void 19 setup() { 20 Serial.begin(9600); 21 // initialize the LED pins as an output: 22 23 pinMode(red, OUTPUT); 24 pinMode(green, OUTPUT); 25 pinMode(blue, OUTPUT); 26 27 // initialize the pushbutton pins as an input: 28 pinMode(button1, INPUT); 29 30 pinMode(button2, INPUT); 31 pinMode(button3, INPUT); 32} 33 34void loop() 35 { 36 // read the state of the pushbuttons value: 37 a = digitalRead(button1); 38 39 b = digitalRead(button2); 40 c = digitalRead(button3); 41 42 // Show the 43 state of pushbuttons on serial monitor 44 Serial.println(a); 45 Serial.println(b); 46 47 Serial.println(c); 48 49 // check if the pushbutton is pressed. 50 // if 51 it is, the buttonState is HIGH: 52 if (a == HIGH) { 53 // turn LED on: 54 55 digitalWrite(red, HIGH); 56 } else { 57 // turn LED off: 58 digitalWrite(red, 59 LOW); 60 } 61 if (b == HIGH) { 62 digitalWrite(green, HIGH); 63 } else 64 { 65 digitalWrite(green, LOW); 66 } 67 if (c == HIGH) { 68 digitalWrite(blue, 69 HIGH); 70 } else { 71 digitalWrite(blue, LOW); 72 } 73 // Added the delay 74 so that we can see the output of button 75 delay(100); 76}
Downloadable files
Schematic
Schematic

Comments
Only logged in users can leave comments