Created
December 21, 2018 23:43
-
-
Save tbhaxor/4fcfec35ce67c4df5307e98975400309 to your computer and use it in GitHub Desktop.
Controll LED With Button and Show on LCD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
// lcd pin config | |
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); | |
int selected = -1; | |
int high = 0; | |
String colors[] = {"Red", "Green", "Blue"}; | |
void setup() | |
{ | |
lcd.begin(16, 2); | |
lcd.clear(); | |
lcd.print("Configuring"); | |
// button pin config | |
pinMode(10, INPUT); | |
pinMode(11, INPUT); | |
pinMode(12, INPUT); | |
// Common Anode RGB led pin config | |
pinMode(A0, OUTPUT); | |
pinMode(A1, OUTPUT); | |
pinMode(A2, OUTPUT); | |
// initialize the button | |
digitalWrite(10, HIGH); | |
digitalWrite(11, HIGH); | |
digitalWrite(12, HIGH); | |
// off led | |
analogWrite(A0, 255); | |
analogWrite(A1, 200); | |
analogWrite(A2, 255); | |
delay(1000); | |
lcd.clear(); | |
lcd.print("Ready"); | |
delay(1000); | |
lcd.clear(); | |
lcd.print("Select Colors"); | |
} | |
// function to clear line | |
void ceol(byte line) | |
{ | |
lcd.setCursor(0, line); | |
lcd.print(" "); | |
lcd.setCursor(0, line); | |
} | |
void loop() | |
{ | |
ceol(1); | |
if (selected == -1) | |
{ | |
lcd.print("Current : None"); | |
analogWrite(A0, 255); | |
analogWrite(A1, 200); | |
analogWrite(A2, 255); | |
} | |
else | |
{ | |
lcd.print("Current : " + colors[selected]); | |
} | |
// buttons | |
bool left = digitalRead(10) == LOW; | |
bool rght = digitalRead(12) == LOW; | |
bool reset = digitalRead(11) == LOW; | |
if (left || rght || reset) | |
{ | |
if (left) | |
{ | |
selected--; | |
selected = selected < 0 ? 2 : selected; | |
} | |
if (rght) | |
{ | |
selected++; | |
selected = selected > 2 ? 0 : selected; | |
} | |
if (reset) | |
{ | |
selected = -1; | |
} | |
if (selected > -1) | |
{ | |
ceol(1); | |
lcd.print("Current : " + colors[high]); | |
} | |
switch (selected) | |
{ | |
case 0: | |
analogWrite(A0, 0); | |
analogWrite(A1, 255); | |
analogWrite(A2, 255); | |
break; | |
case 1: | |
analogWrite(A0, 255); | |
analogWrite(A1, 0); | |
analogWrite(A2, 255); | |
break; | |
case 2: | |
analogWrite(A0, 255); | |
analogWrite(A1, 255); | |
analogWrite(A2, 0); | |
break; | |
} | |
} | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment