// Code licensed under GPL-v3
// Simplified mockup of an Arduino Nano-based light control system
// All Digital pins and one Analog pin on the Arduino Nano used
// D0 - D2 and D4 - D7 are for LCD
// D3 is for the PHR (digital)
// D8 - D13 are for buttons TODO - Change D13 to LED control
// A0 is for the PHR (analog)
// Pinout for the Arduino Micro is as follows:
// D11, D12, D7 - D10 are for LCD (reverse order)
// LiquidCrystal lcd(11, 12, 10, 9, 8, 7);
// D13 is for LED Control
// D18 - D23 are for buttons
// A6 is PHR (analog)
// D5 is PHR (digital)
#include <LiquidCrystal.h>
LiquidCrystal lcd(0, 1, 4, 5, 6, 7);
//E - 1
//RS - 0
//BKLT - 2
//D1-D4 - 4-7
const int bkltPin = 2;
const uint8_t buttonPins[] = {8, 9, 10, 11, 12, 13};
// B_1 - Left [a]
const int buttonLeft = buttonPins[0];
// B_2 - Up [w]
const int buttonUp = buttonPins[1];
// B_3 - Right [d]
const int buttonRight = buttonPins[2];
// B_4 - Down [s]
const int buttonDown = buttonPins[3];
// B_5 - Enter [e]
const int buttonEnter = buttonPins[4];
// B_6 - Back [q]
const int buttonBack = buttonPins[5];
// const int phrPinD = 3;
// May need to be moved to A0 (pin 19)
const int phrPinA = A0;
// editable PHR limit
int phrLimit = 500;
const int ledOutPin = 3;
bool backlight = true;
int button;
uint8_t down[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00111,
0b00010,
0b00000,
};
uint8_t up[8] = {
0b00000,
0b00010,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
};
uint8_t right[8] = {
0b00000,
0b00000,
0b00010,
0b00011,
0b00010,
0b00000,
0b00000,
0b00000,
};
uint8_t left[8] = {
0b00000,
0b00000,
0b01000,
0b11000,
0b01000,
0b00000,
0b00000,
0b00000,
};
uint8_t ohm[8] = {
//Found out this character is 244 or 154 depending on model
0b00000,
0b01110,
0b10001,
0b10001,
0b01010,
0b11011,
0b00000,
0b00000,
};
void wait(){
delay(800);
}
void pause(){
delay(150);
}
void bottom_clear(){
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
}
int bkltctl(int state){
switch (state){
case 0:
digitalWrite(bkltPin, LOW);
break;
case 1:
digitalWrite(bkltPin, HIGH);
break;
}
}
int lightctl(int state){
switch (state){
case 0:
digitalWrite(ledOutPin, LOW);
break;
case 1:
digitalWrite(ledOutPin, HIGH);
break;
}
}
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2);
lcd.print("Setting up ");
lcd.setCursor(0,1);
lcd.print("Custom Chars");
lcd.createChar(2, up);
lcd.createChar(3, down);
lcd.createChar(4, right);
lcd.createChar(5, left);
lcd.createChar(6, ohm);
pinMode(bkltPin, OUTPUT);
if (backlight){
bkltctl(1);
}
wait();
bottom_clear();
lcd.setCursor(0,1);
lcd.print("Buttons");
for (uint8_t i = 0; i < sizeof(buttonPins); i++){
pinMode(buttonPins[i], INPUT_PULLUP);
}
wait();
bottom_clear();
lcd.print("PHR");
//pinMode(phrPinD, INPUT_PULLUP);
wait();
bottom_clear();
lcd.print("LEDs");
pinMode(ledOutPin, OUTPUT);
lightctl(1);
wait();
lightctl(0);
lcd.clear();
lcd.print("Done!");
lcd.setCursor(0,1);
lcd.print("Press Enter \x06");
}
void main_menu(){
String menu[] = {"Current Reading","Passive Mode", "Settings", "--------------"};
int max_op = sizeof(menu)-1;
String arrow; //up or down arrow based on location
int op_one = 0;
int op_two = 1;
int selection = 0;
bool selected = false;
while (!selected){
lcd.setCursor(0,0);
lcd.print("\x04" + menu[op_one]);
lcd.setCursor(0,1);
lcd.print(" " + menu[op_two]);
if (op_two == 1){
lcd.setCursor(15,1);
//down arrow
arrow = "\x03";
}
else if (op_two == 2) {
lcd.setCursor(15,0);
//up arrow
arrow = "\x02";
}
lcd.print(arrow);
// Process Up Button
if (digitalRead(buttonUp) == LOW){
pause();
// Cap minimum selection to 0
if (selection > 0 && op_one > 0){
selection--;
}
if ( op_one > 0 ){
op_one--;
op_two--;
}
lcd.clear();
}
// Process Down Button
if (digitalRead(buttonDown) == LOW){
pause();
// Cap maximum selection to maximum
if (selection == max_op){
selection = 0;
op_one = 0;
op_two = 1;
}
else if (selection < max_op && op_two < max_op){
selection++;
}
if ( op_two < max_op ){
op_one++;
op_two++;
}
lcd.clear();
}
if (digitalRead(buttonEnter) == LOW){
switch (selection){
case 0:
live_reading();
break;
case 1:
selected = true;
passive();
break;
case 2:
pause();
settings_menu();
break;
}
}
}
}
void live_reading(){
bool back = false;
while (!back){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PHR Reading");
lcd.setCursor(4,1);
lcd.print(/*String(digitalRead(phrPinD)) + "|" + */String(analogRead(phrPinA)));
lightmon();
pause();
if (digitalRead(buttonBack) == LOW || digitalRead(buttonEnter) == LOW){
back == true;
break;
}
}
}
void passive(){
bkltctl(0);
lcd.clear();
bool back = false;
do {
lightmon();
if (digitalRead(buttonBack) == LOW){
bkltctl(1);
back = true;
main_menu();
}
} while (!back);
}
void settings_menu(){
lcd.clear();
String menu[] = {"Backlight", "PHR Range", "--------------"};
int max_op = sizeof(menu)-1;
String arrow; //up or down arrow based on location
int op_one = 0;
int op_two = 1;
int selection = 0;
bool selected = false;
while (!selected){
lcd.setCursor(0,0);
lcd.print("\x04" + menu[op_one]);
lcd.setCursor(0,1);
lcd.print(" " + menu[op_two]);
if (op_two == 1){
lcd.setCursor(15,1);
//down arrow
arrow = "\x03";
}
else if (op_two % 2) {
lcd.setCursor(15,0);
//up arrow
arrow = "\x02";
}
lcd.print(arrow);
// Process Up Button
if (digitalRead(buttonUp) == LOW){
pause();
// Cap minimum selection to 0
if (selection > 0 && op_one > 0){
selection--;
}
if ( op_one > 0 ){
op_one--;
op_two--;
}
lcd.clear();
}
// Process Down Button
if (digitalRead(buttonDown) == LOW){
pause();
// Cap maximum selection to maximum
if (selection < max_op && op_two < max_op){
selection++;
}
if ( op_two < max_op ){
op_one++;
op_two++;
}
lcd.clear();
}
if (digitalRead(buttonBack) == LOW){
pause();
main_menu();
break;
}
if (digitalRead(buttonEnter) == LOW){
pause();
switch (selection){
case 0:
setting_bklt();
break;
case 1:
phr_range();
break;
}
}
}
}
void setting_bklt(){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Backlight");
bool done = false;
while (!done){
bool state = digitalRead(bkltPin);
lcd.setCursor(0,1);
if (state == HIGH){
lcd.print("\x05 On \x04");
}
if (state == LOW){
lcd.print("\x05 Off \x04");
}
if (digitalRead(buttonLeft) == LOW || digitalRead(buttonRight) == LOW){
pause();
digitalWrite(bkltPin, !state);
bottom_clear();
}
if (digitalRead(buttonEnter) == LOW || digitalRead(buttonBack) == LOW){
pause();
done = true;
settings_menu();
break;
}
}
}
void phr_range(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PHR Limit");
bool done = false;
do {
lcd.setCursor(0,1);
lcd.print("\x05 "+ String(phrLimit) +"\x06 \x04");
if (digitalRead(buttonLeft) == LOW){
pause();
if (phrLimit > 250){
phrLimit -= 250;
}
bottom_clear();
}
if (digitalRead(buttonRight) == LOW){
pause();
if (phrLimit < 1500){
phrLimit += 250;
}
bottom_clear();
}
if (digitalRead(buttonEnter) == LOW || digitalRead(buttonBack) == LOW){
pause();
done = true;
settings_menu();
break;
}
lightmon();
} while (!done);
}
void lightmon(){
if (analogRead(phrPinA) > phrLimit){
digitalWrite(ledOutPin, HIGH);
}
if (analogRead(phrPinA) <= phrLimit){
digitalWrite(ledOutPin, LOW);
}
}
void loop() {
// put your main code here, to run repeatedly:
lightmon();
if(digitalRead(buttonEnter) == LOW){
pause();
lcd.clear();
main_menu();
}
}