Hello dear friends, sorry about the long time I took to answer, but I was traveling without my computer. How could I survive?
Well, I´ll show the code that I was creating to learn about functions and programming. As I said, I´m making a clock and I want create two pages. Every time that I press the switch button I want that the sketch perform a complete clear in the LCD to enter new informations. So the sketch runs this clear funciton infenitely, so there is a fight between the new informations command and the clear command.
Please note: - the code ins´t ready yet.
- some variables names are write in portuguese
- botao1() is a retention button function to switch between lcd1() and lcd2() functions that changes the LCD display informations.
Hope you understand, sorry about some newbie C++ writings
Thank you for any help!!!
================================================================
#include <LiquidCrystal.h>
#define pinBotao 4
#define pinLED 2
bool botao1();
void lcd1();
void lcd2();
const int rs = 5, en = 6, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
pinMode(pinBotao, INPUT);
pinMode(pinLED, OUTPUT);
lcd.begin(16, 2);
}
void loop() {
if( botao1() ) {
lcd1();
}
else {
lcd2();
}
}
bool botao1() {
#define tempoDebounce 99
bool estadoBotao;
static bool estadoAntBotao;
static bool estadoRet = true;
static unsigned long delayBotao = 0;
if( (millis() - delayBotao) > tempoDebounce ) {
estadoBotao = digitalRead(pinBotao);
if( estadoBotao && (estadoBotao != estadoAntBotao) ){
estadoRet = !estadoRet;
delayBotao = millis();
}
estadoAntBotao = estadoBotao;
}
return estadoRet;
}
void lcd1(){
digitalWrite(pinLED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("pagina 1");
lcd.setCursor(0, 1);
lcd.print("pagina 1");
}
void lcd2(){
digitalWrite(pinLED, LOW);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("pagina 2");
lcd.setCursor(4, 1);
lcd.print("pagina 2");
}
Botao_reten__o.ino (1.23 KB)