myrtje
Published

LED show!!

If you press a button, can you guess which color the rgb LED will be?

BeginnerProtip142
LED show!!

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×12
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Button
×3
Resistor 221 ohm
Resistor 221 ohm
×3

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Code

my code

C/C++
const int button1 = 2;     // the number of button 1 pin
const int button2 = 3;    // the number of button 2 pin
const int button3 = 4;    // the number of button 3 pin
const int red =  8;      // the number of the red pin
const int green = 9;    //the number of the green pin
const int blue = 10;    //the number of the blue pin

// variables will change:
int a = 0;         // variable for reading the pushbutton status
int b = 0;
int c = 0;

void setup() {
  Serial.begin(9600);
  // initialize the LED pins as an output:
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
   // initialize the pushbutton pins as an input:
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
}

void loop() {
  // read the state of the pushbuttons value:
  a = digitalRead(button1);
  b = digitalRead(button2);
  c = digitalRead(button3);

  // Show the state of pushbuttons on serial monitor
  Serial.println(a);
  Serial.println(b);
  Serial.println(c);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (a == HIGH) {
    // turn LED on:
    digitalWrite(red, HIGH);
  } else {
    // turn LED off:
    digitalWrite(red, LOW);
  }
  if (b == HIGH) {
    digitalWrite(green, HIGH);
  } else {
    digitalWrite(green, LOW);
  }
  if (c == HIGH) {
    digitalWrite(blue, HIGH);
  } else {
    digitalWrite(blue, LOW);
  }
  // Added the delay so that we can see the output of button
  delay(100);
}

Credits

myrtje
0 projects • 0 followers

Comments