Hey guys,
Im building a controller interface for my robot platform and i need to scroll through pages of a LCD Using two buttons.
The main idea is too press the forward button the LCD scrolls a page forward and same with Moving Back.
I have spent about 4 or 5 hours trying to sort out the debouncing, The main idea works, it just scrolls through way too quick.
Ive tried everything i could think of but i just cant get it right, Any Ideas?
Heres my code:
#include <LiquidCrystal.h>
int buttonB1 = 6; //Button to make scroll increase
int buttonF1 = 7; //Button to make decrease
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //LCD STUFF
//.....DEBOUNCING VARIABLES.......
int del = 50;
boolean buttonB;
boolean buttonF;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = del;
int buttonState1;
int lastButtonState1 = LOW;
long lastDebounceTime1 = 0;
long debounceDelay1 = del;
//........................
int scroll; //THE MAIN VARIABLE THAT SCROLLS
int pages = 7; //The number of pages to scroll
void setup() {
lcd.begin(16, 2); //Start The LCD
Serial.begin(9600); //Serial for debugging
pinMode(buttonB1, INPUT); //Usual Stuff
pinMode(buttonF1, INPUT);
scroll = 1; //Start at page 1
}
void loop() {
debounce(); //Do all the debouncing
if (buttonB == HIGH ){ //If The backward button is high decrease scroll by 1
delay(300); //To try to slow the scrolling down
scroll = (scroll - 1);
previous = scroll;
}
else if (buttonF == HIGH){ //If The forward button is high increase scroll by 1
delay(300);
scroll = (scroll + 1);
previous = scroll;
}
if (scroll > pages) scroll = 1; //If scroll is greater then the number of pages (7) scroll equals 1
else if (scroll < 1) scroll = pages; //If scroll is less than 1 scroll equals 7
delay(10);
Serial.println(scroll);//For Debuging
//...............................
if (scroll == 1) fun1(); //If statments to run the functions
else if (scroll == 2) fun2();
else if (scroll == 3) fun3();
else if (scroll == 4) fun4();
else if (scroll == 5) fun5();
else if (scroll == 6) fun6();
else if (scroll == 7) fun7();
}
//............FUNCTIONS............
void fun1(){
delay(5); //Give the LCD some time
lcd.clear(); //Clear the LCD
lcd.write("1"); //Write some data to LCD
}
void fun2(){
delay(5);
lcd.clear();
lcd.write("2");
}
void fun3(){
delay(5);
lcd.clear();
lcd.write("3");
}
void fun4(){
delay(5);
lcd.clear();
lcd.write("4");
}
void fun5(){
delay(5);
lcd.clear();
lcd.write("5");
}
void fun6(){
delay(5);
lcd.clear();
lcd.write("6");
}
void fun7(){
delay(5);
lcd.clear();
lcd.write("7");
}
void debounce() { //Debouncing Function
//........COPIED FROM DEBOUNCE EXAMPLE...........
int reading = digitalRead(buttonF1);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}
buttonF = buttonState;
lastButtonState = reading;
//.............DEBOUNCE SECOND BUTTON...................
int reading1 = digitalRead(buttonB1);
if (reading1 != lastButtonState1) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay1) {
buttonState1 = reading1;
}
buttonB = buttonState1;
lastButtonState1 = reading1;
}
Thanks In advance,
Alex