Sunday, October 8, 2017

Arduino : Intermediate - Control LED RGB using Pushbutton

Description
Turns on and off a LED RGB connected to digitalpin, when pressing a pushbutton.

List Item
  1. Arduino Uno 
  2. 1 LED RGB
  3. 3 Resistor (150 to 330 ohm)
  4. 3 Pushbutton
  5. Wire jumper
Wiring Diagram

LED Pin Out

Video



Coding
/*
 Control LED RGB using Pushbutton Example

 created 08 October 2017
 by Microcontroller Malaysia
 https://microcontrollermalaysia.blogspot.my/2017/10/arduino-intermediate-control-led-rgb.html

 This example code is in the public domain

 */
const int redRGB = 2;
const int greenRGB = 3;
const int blueRGB = 4;
const int redButton = 8;
const int greenButton = 9;
const int blueButton = 10;

void setup() {
  pinMode(redButton, INPUT_PULLUP);
  pinMode(blueButton, INPUT_PULLUP);
  pinMode(greenButton, INPUT_PULLUP);
  pinMode(redRGB, OUTPUT);
  pinMode(blueRGB, OUTPUT);
  pinMode(greenRGB, OUTPUT);
}

void loop() {

  int sensorRed = digitalRead(redButton);
  int sensorBlue = digitalRead(blueButton);
  int sensorGreen = digitalRead(greenButton);

  if (sensorRed == HIGH) {
    digitalWrite(redRGB, LOW);
  } else {
    digitalWrite(redRGB, HIGH);
  }

  if (sensorGreen == HIGH) {
    digitalWrite(greenRGB, LOW);
  } else {
    digitalWrite(greenRGB, HIGH);
  }

  if (sensorBlue == HIGH) {
    digitalWrite(blueRGB, LOW);
  } else {
    digitalWrite(blueRGB, HIGH);
  }
}


No comments:

Post a Comment