[Project 7] – RGB LED Control With Button

You are here:
Estimated reading time: 1 min

In this project we will make RGB LED Control With Button. To understand this project  you should know working prencible RGB (RGB: Red Green Blue; LED: Light Emitting Diode ) I will use arduino uno in the circuit design program. I use usually arduino mega 2560.  You can use the other arduino cards. This project for beginners.

 

Schema:

RGB LED Pins:

Materials:

  • Arduino UNO / Other Arduino Cards   x 1
  • Breadboard   x 1
  • 330 ohm resistor    x 3
  • 10 kohm resistor     x 1
  • RGB LED  x 1
  • Button    x 1
  • Jumper Cable

Program:

//  [Project 7] - RGB LED Control With Button

const int BlueLed=10;
const int GreenLed=9;
const int RedLed=8;
const int button=7;

boolean beforeCase=LOW;
boolean nowCase=LOW;

int ledMode=0;

  void setup()
 {
 pinMode(BlueLed,OUTPUT);
 pinMode(GreenLed,OUTPUT);
 pinMode(RedLed,OUTPUT);
 pinMode(button,INPUT);
 }

   boolean buttonControl(boolean now)
  {
   boolean presentBtn = digitalRead(button);
   if(now!=presentBtn)
  {
   delay(5);
   presentBtn=digitalRead(button);
  }
   return presentBtn;
  }

    void rgbSet(int colour)
   {
 
       if(colour==1)
   {
     digitalWrite(RedLed,LOW); 
     digitalWrite(GreenLed,HIGH); 
     digitalWrite(BlueLed,HIGH);  
 }
       else if(colour==2)
{
     digitalWrite(RedLed,HIGH); 
     digitalWrite(GreenLed,LOW); 
     digitalWrite(BlueLed,HIGH);
}
       else if(colour==3)
{
     digitalWrite(RedLed,HIGH); 
     digitalWrite(GreenLed,HIGH); 
     digitalWrite(BlueLed,LOW);
}
       else if(colour==4)
{
     analogWrite(RedLed,150);   
     analogWrite(GreenLed,0); 
     analogWrite(BlueLed,150);  
}
       else if(colour==5)
{
     analogWrite(RedLed,0);   
     analogWrite(GreenLed,50); 
     analogWrite(BlueLed,50);  
}
}
         void loop()
      {
         nowCase=buttonControl(beforeCase);
         if(beforeCase==LOW && nowCase==HIGH)
      {
         ledMode++;
      }
         beforeCase=nowCase;
         if(ledMode==6)
         ledMode=0;
   
         rgbSet(ledMode);

}

Project Related Images:

 

Was this article helpful?
Dislike 0
Views: 561

Leave a Reply

Your email address will not be published. Required fields are marked *