//16x2 LCD
#include <LiquidCrystal_I2C.h> //SDA = A4, SCL = A5
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Defining pins for rotary encoder
const int RotaryCLK = 2; //CLK pin on the rotary encoder
const int RotaryDT = 4; //DT pin on the rotary encoder
const int RotarySW = 3; //SW pin on the rotary encoder (Button function)
//Defining variables for rotary encoder and button
int ButtonCounter = 0; //counts the button clicks
int RotateCounter = 0; //counts the rotation clicks
bool rotated = true; //info of the rotation
bool ButtonPressed = false; //info of the button
//increamental
int incr=0;
int incy=0;
int incb=0;
int incw=0;
//Statuses
int CLKNow;
int CLKPrevious;
int DTNow;
int DTPrevious;
// Timers
float TimeNow1;
float TimeNow2;
//LED things
// pins
int yellowLED = 11;
int whiteLED = 9;
int blueLED = 10;
int redLED = 6;
//statuses (1/true: ON, 0/false: OFF)
bool yellowLEDStatus = false;
bool redLEDStatus = false;
//------------------------------
//Drawing of the LCD layout
// Y R CLK
// 0 0 1
void setup()
{
//Serial.begin(9600); //we don't use the serial in this example
//------------------------------------------------------
lcd.init(); // initialize the lcd
lcd.backlight();
//------------------------------------------------------
lcd.setCursor(0,0); //Defining position to write from first row, first column .
lcd.print(" Y R B W CLK");
lcd.setCursor(0,1); //second line, 1st block
lcd.print(" 0 0 0 0 0 "); //You can write 16 Characters per line .
delay(1000); //wait 3 sec
//------------------------------------------------------
//setting up pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(yellowLED, OUTPUT); //yellow LED
pinMode(redLED, OUTPUT); //red LED
//LOW pins = LEDs are off. (LED + is connected to the digital pin)
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
//Store states
CLKPrevious = digitalRead(RotaryCLK);
DTPrevious = digitalRead(RotaryDT);
attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE);
attachInterrupt(digitalPinToInterrupt(RotarySW), buttonPressed, FALLING); //either falling or rising but never "change".
TimeNow1 = millis(); //Start timer 1
}
void loop()
{
printLCD();
ButtonChecker();
}
void buttonPressed()
{
//This timer is a "software debounce". It is not the most effective solution, but it works
TimeNow2 = millis();
if(TimeNow2 - TimeNow1 > 400)
{
ButtonPressed = true;
}
TimeNow1 = millis(); //"reset" timer; the next 500 ms is counted from this moment
}
void rotate()
{
CLKNow = digitalRead(RotaryCLK); //Read the state of the CLK pin
// If last and current state of CLK are different, then a pulse occurred
if (CLKNow != CLKPrevious && CLKNow == 1)
{
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so increase
if (digitalRead(RotaryDT) != CLKNow)
{
RotateCounter++;
if(RotateCounter > 4)
{
RotateCounter = 0;
}
}
else
{
RotateCounter--;
if(RotateCounter < 0)
{
RotateCounter = 2;
}
}
}
CLKPrevious = CLKNow; // Store last CLK state
rotated = true;
}
void printLCD()
{
if(rotated == true) //refresh the CLK
{
lcd.setCursor(14,1);
lcd.print(RotateCounter);
rotated = false;
}
}
void ButtonChecker() //this is basically the menu part. keep track of the buttonpressed and rotatecounter for navigation
{
if(ButtonPressed == true)
{
switch(RotateCounter)
{
case 1:
if (incy<4){
incy++;
if(incy==1){
analogWrite(yellowLED,map(1,0,100,0,255));
}if(incy==2){
analogWrite(yellowLED,map(10,0,100,0,255));
}if(incy==3){
analogWrite(yellowLED,map(50,0,100,0,255));
}
if(incy==4){
analogWrite(yellowLED,map(100,0,100,0,255));
}
}
else {
incy=0;
analogWrite(yellowLED, 0);
}
lcd.setCursor(2,1); // Defining positon to write from second row, first column .
lcd.print(incy);
break;
case 2:
if (incr<4){
incr++;
if(incr==1){
analogWrite(redLED,map(1,0,100,0,255));
}if(incr==2){
analogWrite(redLED,map(10,0,100,0,255));
}if(incr==3){
analogWrite(redLED,map(50,0,100,0,255));
}
if(incr==4){
analogWrite(redLED,map(100,0,100,0,255));
}
}
else {
incr=0;
analogWrite(redLED, 0);
}
lcd.setCursor(5,1); // Defining positon to write from second row, first column .
lcd.print(incr);
break;
break;
case 3:
if (incb<4){
incb++;
if(incb==1){
analogWrite(blueLED,map(1,0,100,0,255));
}if(incb==2){
analogWrite(blueLED,map(10,0,100,0,255));
}if(incb==3){
analogWrite(blueLED,map(50,0,100,0,255));
}
if(incb==4){
analogWrite(blueLED,map(100,0,100,0,255));
}
}
else {
incb=0;
analogWrite(blueLED, 0);
}
lcd.setCursor(8,1); // Defining positon to write from second row, first column .
lcd.print(incb);
break;
case 4:
if (incw<4){
incw++;
if(incw==1){
analogWrite(whiteLED,map(1,0,100,0,255));
}if(incw==2){
analogWrite(whiteLED,map(10,0,100,0,255));
}if(incw==3){
analogWrite(whiteLED,map(50,0,100,0,255));
}
if(incw==4){
analogWrite(whiteLED,map(100,0,100,0,255));
}
}
else {
incw=0;
analogWrite(whiteLED, 0);
}
lcd.setCursor(10,1); // Defining positon to write from second row, first column .
lcd.print(incw);
break;
}
}
ButtonPressed = false; //reset this variable
}