/* Switches and RGB LED Circuit Connections: PIN_3 => Resistor 220Ω => Red pin of RGB LED PIN_5 => Resistor 220Ω => Blue pin of RGB LED PIN_6 => Resistor 220Ω => Green pin of RGB LED PIN_8 => Pull down resistor (220Ω) => switch_1 (Vcc) PIN_9 => Pull down resistor (220Ω) => switch_4 (Vcc) */ #define R_pin 3 //give the name “R_pin” to PIN_3 #define G_pin 6 //give the name “G_pin” to PIN_6 #define B_pin 5 //give the name “B_pin” to PIN_5 #define Sw1_pin 8 //give the name “Sw1_pin” to PIN_8 #define Sw4_pin 9 //give the name “Sw4_pin” to PIN_9 //The setup() function initializes and sets the initial values //It will only run once after each powerup or reset void setup() { //Configure PIN_3, PIN_5 and PIN_6 to behave as output //Configure PIN_8 and PIN_9 to behave as input pinMode(R_pin, OUTPUT); pinMode(G_pin, OUTPUT); pinMode(B_pin, OUTPUT); pinMode(Sw1_pin, INPUT); pinMode(Sw4_pin, INPUT); } //This function loops consecutively void loop() { if(digitalRead(Sw1_pin)==0 && digitalRead(Sw4_pin)==0){ //RGB LED is OFF analogWrite(R_pin, 0); //Write 0% PWM to pin 3 analogWrite(G_pin, 0); //Write 0% PWM to pin 6 analogWrite(B_pin, 0); //Write 0% PWM to pin 5 delay(1000); // Wait for 1 second } else if(digitalRead(Sw1_pin)==0 && digitalRead(Sw4_pin)==1){ //red color for RGB = > R=255, G=0, B=0 analogWrite(R_pin, 255); //Write 100% PWM to pin 3 analogWrite(G_pin, 0); //Write 0% PWM to pin 6 analogWrite(B_pin, 0); //Write 0% PWM to pin 5 delay(1000); // Wait for 1 second } else if(digitalRead(Sw1_pin)==1 && digitalRead(Sw4_pin)==0){ //green color for RGB = > R=0, G=255, B=0 analogWrite(R_pin, 0); //Write 0% PWM to pin 3 analogWrite(G_pin, 255); //Write 100% PWM to pin 6 analogWrite(B_pin, 0); //Write 0% PWM to pin 5 delay(1000); // Wait for 1 second } else{ //if(digitalRead(Sw1_pin)==1 && digitalRead(Sw4_pin)==1) //blue color = > RGB=0,0,255 analogWrite(R_pin, 0); //Write 0% PWM to pin 3 analogWrite(G_pin, 0); //Write 0% PWM to pin 6 analogWrite(B_pin, 255); //Write 100% PWM to pin 5 delay(1000); // Wait for 1 second } }