//libraries
#include <LiquidCrystal_I2C.h>
//define lcd pin and size
LiquidCrystal_I2C lcd(0x27, 16, 2);
//led pin
int red = 11;
int green = 10;
int blue = 9;
//buttons pin
int pOne = 7;
int pTen = 6;
int pHundred = 5;
int pNext = 4;
int pOK = 3;
int pReset = 2;
//value of button
int valOne = 0;
int valTen = 0;
int valHundred = 0;
int valNext = 0;
int valOK = 0;
int valReset = 0;
//value that says what color it is
int color = 0;
//value of the led
int valRed = 0;
int valGreen = 0;
int valBlue = 0;
void setup() {
//led
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
//buttons
pinMode(pOne, INPUT);
pinMode(pTen, INPUT);
pinMode(pHundred, INPUT);
pinMode(pNext, INPUT);
pinMode(pOK, INPUT);
pinMode(pReset, INPUT);
//lcd setup
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("R:");
lcd.setCursor(7, 0);
lcd.print("G:");
lcd.setCursor(0, 1);
lcd.print("B:");
}
void loop() {
//buttons value
valOne = digitalRead(pOne);
valTen = digitalRead(pTen);
valHundred = digitalRead(pHundred);
valNext = digitalRead(pNext);
valOK = digitalRead(pOK);
valReset = digitalRead(pReset);
if (valNext == 1) { //If the "Next" button is pressed, the value of the color variable increases
color = color + 1;
delay(300);
}
if (color >= 3) { //If the value of the "color" variable is greater than or equal to 3, it returns the value of the variable to 0. Because there are three colors
color = 0;
}
//set a maximum brightness value of each color
if (valRed >= 256) {
valRed = 0;
lcd.setCursor(3, 0);
lcd.print(" "); //clear the value in the display
}
if (valGreen >= 256) {
valGreen = 0;
lcd.setCursor(10, 0);
lcd.print(" ");
}
if (valBlue >= 256) {
valBlue = 0;
lcd.setCursor(3, 1);
lcd.print(" ");
}
//If the color chosen is red and the button pressed is pOne increases the value of the variable. And so on
if (color == 0 and valOne == 1) {
valRed = valRed + 1;
delay(300);
}
if (color == 0 and valTen == 1) {
valRed = valed + 10;
delay(300);
}
if (color == 0 and valHundred == 1) {
valRed = valRed + 100;
delay(300);
}
if (color == 1 and valOne == 1) {
valVGreen = valGreen + 1;
delay(300);
}
if (color == 1 and valTen == 1) {
valGreen = valGreen + 10;
delay(300);
}
if (color == 1 and valHundred == 1) {
valGreen = valGreen + 100;
delay(300);
}
if (color == 2 and valOne == 1) {
valBlue = valBlue + 1;
delay(300);
}
if (colore == 2 and valTen == 1) {
valBlue = valBlue + 10;
delay(300);
}
if (color == 2 and valHundred == 1) {
valBlue= valBlue + 100;
delay(300);
}
//write the rgb values on the LCD
lcd.setCursor(3, 0);
lcd.print(valRed);
lcd.setCursor(10, 0);
lcd.print(valGreen);
lcd.setCursor(3, 1);
lcd.print(valBlue);
if (valOK == 1) { //If the "OK" button is pressed, the LED takes on the color of the variable values
analogWrite(red, valRed);
analogWrite(green, valGreen);
analogWrite(blue, valBlue);
}
if (valReset == 1) { //if the "Reset" button is pressed, each value is reset
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
valRed = 0;
valGreen = 0;
valBlue = 0;
color = 0;
lcd.setCursor(3, 0);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(3, 1);
lcd.print(" ");
}
}
Comments