Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created May 3, 2020 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffersGlass/8e5eed0e46c455d0c51db57fe9e23e10 to your computer and use it in GitHub Desktop.
Save JeffersGlass/8e5eed0e46c455d0c51db57fe9e23e10 to your computer and use it in GitHub Desktop.
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int rightButton = A0;
const int incrementButton = A1;
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
int cursorIndex = 0;
int displayPositions[] = {0, 6, 12};
int numPositions = 3;
long lastIncrementButtonPressTime = 0;
long minIncrementButtonDelay = 60;
long lastRightButtonPressTime = 0;
long minRightButtonDelay = 500;
int buttonIncrement = 5;
int red = 125;
int green = 125;
int blue = 125;
void setup() {
pinMode(rightButton, INPUT_PULLUP);
pinMode(incrementButton, INPUT_PULLUP);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
displayColors();
lcd.cursor();
lcd.noBlink();
}
void loop() {
if (checkButtons()){
displayColors();
}
}
void displayColors(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RED");
lcd.setCursor(0,1);
lcd.print(red);
lcd.setCursor(6,0);
lcd.print("GRN");
lcd.setCursor(6,1);
lcd.print(green);
lcd.setCursor(12,0);
lcd.print("BLU");
lcd.setCursor(12,1);
lcd.print(blue);
lcd.setCursor(displayPositions[cursorIndex], 1);
}
boolean checkButtons(){
if (millis() > lastRightButtonPressTime + minRightButtonDelay){
if (digitalRead(rightButton) == LOW){
cursorIndex = (cursorIndex + 1) % 3;
lastRightButtonPressTime = millis();
return true;
}
}
if (millis() > lastIncrementButtonPressTime + minIncrementButtonDelay){
if (digitalRead(incrementButton) == LOW){
lastIncrementButtonPressTime = millis();
switch (cursorIndex){
case 0:
red = (red + buttonIncrement) % 255;
break;
case 1:
green = (green + buttonIncrement) % 255;
break;
case 2:
blue = (blue + buttonIncrement) % 255;
break;
}
setLedColor(red, green, blue);
return true;
}
}
return false;
}
void setLedColor(int r, int g, int b){
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
void setLedColor(int colorVector[]){
setLedColor(colorVector[0], colorVector[1], colorVector[2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment