Project showcase
Pressing the Button Changes the Color of the RGB LED

Pressing the Button Changes the Color of the RGB LED

Use buttons to change the LED color.

  • 788 views
  • 0 comments
  • 3 respects

Components and supplies

Apps and online services

About this project

We turned off the most basic LED and then we did it. It changes the color of the LED, and the color of the LED was really beautiful!

Code

goodArduino
oh
void setup()
{
//start the serial port a@ 9600bps
 Serial.begin(9600);
 //set RGB pins to OUTPUT
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 pinMode(bluePin, OUTPUT);
}

void loop()
{
 //read and store the audio from Envelope pin
 sound = analogRead(soundPin);
 //map sound which in a quiet room a clap is 300
 //from 0 to 3 to be used with switch case
 scale = map(sound, 0, 300, 0, 3);

 //print values over the serial port for debugging
 Serial.print(sound);
 Serial.print("  ");
 Serial.println(scale);

//switch case on scaled value
 switch (scale)
 {
 //if 0 RGB = Blue
 case 0:
 digitalWrite(redPin, LOW);
 digitalWrite(greenPin, LOW);
 digitalWrite(bluePin, HIGH);
 break;
 //if 1 RGB = Green
 case 1:
 digitalWrite(redPin, LOW);
 digitalWrite(greenPin, HIGH);
 digitalWrite(bluePin, LOW);
 break;
 //if 2 RGB = Yellow
 case 2:
 digitalWrite(redPin, HIGH);
 digitalWrite(greenPin, HIGH);
 digitalWrite(bluePin, LOW);
 break;
 //if 3 RGB = Red
 case 3:
 digitalWrite(redPin, HIGH);
 digitalWrite(greenPin, LOW);
 digitalWrite(bluePin, LOW);
 break;
 //default off
 default:
 digitalWrite(redPin, LOW);
 digitalWrite(greenPin, LOW);
 digitalWrite(bluePin, LOW);
break;
 }
}

Schematics

Comments

Add projectSign up / Login