// RGB and R, G, B LED demo
/*
https://arduinogetstarted.com/tutorials/arduino-button
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD(0x27, 20, 4);
const int pinR = 3;
const int pinG = 5;
const int pinB = 6;
const int pinY = 2;
const int Btn1 = 7;
const int Swt1 = 8;
const int sw00 = 9;
const int potR = A0;
const int potG = A1;
const int potB = A2;
void setup() {
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinY, OUTPUT);
pinMode(potR, INPUT);
pinMode(potG, INPUT);
pinMode(potB, INPUT);
pinMode(Swt1, INPUT);
pinMode(Btn1, INPUT_PULLUP);
pinMode(sw00, INPUT_PULLUP);
LCD.init();
LCD.backlight();
LCD.setCursor(1, 0);
LCD.print("Starting up ...");
delay(100);
LCD.clear();
}
//Button states
int lastState = HIGH;
int currentState;
//when Switch is LOW switch is closed HIGH open
void ButtonCheck()
{
// read the state of the switch/button:
currentState = digitalRead(Btn1);
if (lastState == LOW && currentState == HIGH)
{
lastState = currentState;// save the last state
digitalWrite(pinY, HIGH);
delay(200);
digitalWrite(pinY, LOW);
delay(200);
}
else if (lastState == HIGH && currentState == LOW)
{
lastState = currentState;// save the last state
digitalWrite(pinY, HIGH);
delay(200);
digitalWrite(pinY, LOW);
delay(200);
}
}
//LCD Spinner
/*
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
*/
//Writing Light to RGB LED
//reading sliders
int readPot(int pin)
{
return map(analogRead(pin), 0, 1023, 0, 255);
}
//formating read values
void RGB_color1()
{
int red_light_value1 = readPot(potR);
int green_light_value1 = readPot(potG);
int blue_light_value1 = readPot(potB);
Write_RGB_Color(red_light_value1, green_light_value1, blue_light_value1);
}
//Color sycle values
void Colorsycle()
{
delay(1000);
Write_RGB_Color(255, 0, 0); // Red
delay(1000);
Write_RGB_Color(0, 255, 0); // Green
delay(1000);
Write_RGB_Color(0, 0, 255); // Blue
loop();
}
//Writing values
void Write_RGB_Color(int red_light, int green_light, int blue_light)
{
analogWrite(pinR, red_light);
analogWrite(pinG, green_light);
analogWrite(pinB, blue_light);
LCD.setCursor(0, 1);
LCD.println(red_light);
LCD.setCursor(4, 1);
LCD.println(green_light);
LCD.setCursor(8, 1);
LCD.println(blue_light);
LCD.setCursor(0, 2);
int red_light1 = (red_light * 100) / 255;
LCD.println(red_light1);
LCD.setCursor(4, 2);
int green_light1 = (green_light * 100) / 255;
LCD.println(green_light1);
LCD.setCursor(8, 2);
int blue_light1 = (blue_light * 100) / 255;
LCD.println(blue_light1);
}
//Switch
int SWlastState = HIGH;
int SWcurrentState;
void loop()
{
ButtonCheck(); //check button
//LCD
LCD.setCursor(1, 0);
LCD.println("R");
LCD.setCursor(5, 0);
LCD.println("G");
LCD.setCursor(9, 0);
LCD.println("B");
//check switch
SWcurrentState = digitalRead(sw00);
if (SWcurrentState == LOW && SWlastState == HIGH)
{
Colorsycle();
}
else
{
RGB_color1();
SWlastState = SWcurrentState;
}
//checked switch + took action
}