This example demonstrates how to attach functions to the "lines" in the menu.
#include <LiquidCrystal.h>
#include "Button.h"
const byte LCD_RS = 12;
const byte LCD_E = 11;
const byte LCD_D4 = 5;
const byte LCD_D5 = 4;
const byte LCD_D6 = 3;
const byte LCD_D7 = 2;
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
const byte startingScreen = 2;
const bool pullup = true;
Button left(A0, pullup);
Button right(7, pullup);
Button up(8, pullup);
Button down(9, pullup);
Button enter(10, pullup);
enum FunctionTypes {
increase = 1,
decrease = 2,
};
const byte led = 6;
byte led_level = 0;
bool isFading = false;
char* isFading_text;
unsigned int fadePeriod = 100;
bool isBlinking = false;
char* isBlinking_text;
unsigned int blinkPeriod = 1000;
LiquidLine fade_line(0, 0,
"Fade - ", isFading_text);
LiquidLine fadePeriod_line(0, 1,
"Period: ", fadePeriod,
"ms");
LiquidLine blink_line(0, 0,
"Blink - ", isBlinking_text);
LiquidLine blinkPeriod_line(0, 1,
"Period: ", blinkPeriod,
"ms");
void increase_led_level() {
if (led_level < 225) {
led_level += 25;
} else {
led_level = 0;
}
analogWrite(led, led_level);
}
void decrease_led_level() {
if (led_level > 25) {
led_level -= 25;
} else {
led_level = 250;
}
analogWrite(led, led_level);
}
void fade_switch() {
led_off();
if (isFading == true) {
isFading = false;
isFading_text = (char*)"OFF";
} else {
isFading = true;
isFading_text = (char*)"ON";
isBlinking = false;
isBlinking_text = (char*)"OFF";
}
}
void increase_fadePeriod() {
if (fadePeriod < 3000) {
fadePeriod += 10;
}
}
void decrease_fadePeriod() {
if (fadePeriod > 10) {
fadePeriod -= 10;
}
}
void blink_switch() {
led_off();
if (isBlinking == true) {
isBlinking = false;
isBlinking_text = (char*)"OFF";
} else {
isBlinking = true;
isBlinking_text = (char*)"ON";
isFading = false;
isFading_text = (char*)"OFF";
}
}
void increase_blinkPeriod() {
if (blinkPeriod < 3000) {
blinkPeriod += 50;
}
}
void decrease_blinkPeriod() {
if (blinkPeriod > 50) {
blinkPeriod -= 50;
}
}
void led_off() {
led_level = 0;
analogWrite(led, led_level);
}
void buttonsCheck() {
if (right.check() == LOW) {
menu.next_screen();
}
if (left.check() == LOW) {
menu.previous_screen();
}
if (up.check() == LOW) {
menu.call_function(increase);
}
if (down.check() == LOW) {
menu.call_function(decrease);
}
if (enter.check() == LOW) {
menu.switch_focus();
}
}
void fade() {
static bool goingUp = true;
if (goingUp) {
led_level += 25;
} else {
led_level -= 25;
}
if (led_level > 225) {
goingUp = false;
led_level = 250;
}
if (led_level < 25 && goingUp == false) {
goingUp = true;
led_level = 0;
}
analogWrite(led, led_level);
}
void blink() {
static bool blinkState = LOW;
if (blinkState == LOW) {
blinkState = HIGH;
led_level = 255;
} else {
blinkState = LOW;
led_level = 0;
}
analogWrite(led, led_level);
}
void setup() {
Serial.begin(250000);
pinMode(led, OUTPUT);
lcd.begin(16, 2);
led_line.attach_function(increase, increase_led_level);
led_line.attach_function(decrease, decrease_led_level);
fade_line.attach_function(1, fade_switch);
fade_line.attach_function(2, fade_switch);
fadePeriod_line.attach_function(increase, increase_fadePeriod);
fadePeriod_line.attach_function(decrease, decrease_fadePeriod);
blink_line.attach_function(1, blink_switch);
blink_line.attach_function(2, blink_switch);
blinkPeriod_line.attach_function(increase, increase_blinkPeriod);
blinkPeriod_line.attach_function(decrease, decrease_blinkPeriod);
menu.add_screen(welcome_screen);
menu.add_screen(led_screen);
menu.add_screen(fade_screen);
menu.add_screen(blink_screen);
isFading_text = (char*)"OFF";
isBlinking_text = (char*)"OFF";
menu.update();
}
void loop() {
buttonsCheck();
static unsigned long lastMillis_blink = 0;
if ((isFading == true) && ((millis() - lastMillis_blink) > fadePeriod)) {
lastMillis_blink = millis();
fade();
menu.update();
}
static unsigned long lastMillis_fade = 0;
if ((isBlinking == true) && ((millis() - lastMillis_fade) > blinkPeriod)) {
lastMillis_fade = millis();
blink();
menu.update();
}
}