execulte command once

Hello dear friends from Arduino.cc forum.
Sorry about this newbie question, but I´m learning yet.

I need a command to be execulted just once during a funciton.
Is the lcd.clear(); to clear all LCD data for new ones...

How can I do my sketch to do this?

Thank for any help!

Patrick

Please post a full example of a program that you have a problem with.

If you really want do do what you say then set a boolean variable, let's name it lcdHasBeenCleared to false and only clear the LCD when its value is false. When you clear the LCD set the boolean to true to prevent it being cleared again.

However, there are possibly other problems with your unseen code such as updating the LCD too often rather than only when the data changes, but we shall see.

Hi,
Can you post your code so we can advise you.

What lcd is it, a graphic or text screen?

Can you post data/spec on the LCD?

Thanks.. Tom... :slight_smile:

Typically, if you want something done once, do it in setup().

If you REALLY, REALLY want your 'do this once' code buried in a function:

void MyFunction() 
{
  static bool firstTime = true;
  if (firstTime)
  {
    firstTIme = false;  // Don't do this again
    lcd.clear();
  }
}

Please note the 'static' keyword. That tells the compiler that the variable should be initialized once and the value kept for future function calls. Without it, 'firstTime' would be initialized to 'true' every time.

Hello friends!
Thank you soo much for answering!

Actually the sketch isn´t ready yet. I´m making a clock to learn about Arduino and programming as my frist project, please enjoy the following photos.
Please note: - the housing box is just for a test, I´ll change it.

  • the sentence is "Melissa dady´s love".

So what I really want is two pages, one with time and date and another with temperature and humidity records that the clock has registered, so avery time that the button is pressed, i want the sketch performing one complete screen clear, to enter new informations.

Thank you

Hi,
So I see it is a graphics display, can you post data/specs please.

You really only need to update your screen when any data displayed changes.

Some screens produce a noticeable blink when this happens, but there are ways of working around it, that is why we need to know the type of display.

Can you please post the code you have, so we can advise on the best way overwriting data on the screen.

Thanks.. Tom.. :slight_smile:
OPs display.

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)

If you clear the LCD every time you update the display you will get a lot of flicker. Put the clearing of the display inside 'botao1' where you switch between pages.

#include <LiquidCrystal.h>
const int rs = 5, en = 6, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const byte pinBotao = 4;
const byte pinLED = 2;

void setup()
{
  pinMode(pinBotao, INPUT);
  pinMode(pinLED, OUTPUT);
  lcd.begin(16, 2);
}

void loop()
{
  if ( botao1() )
  {
    lcd1();
  }
  else
  {
    lcd2();
  }
}

bool botao1()
{
  const unsigned tempoDebounce = 99;

  static bool estadoAntBotao;
  static bool estadoRet = true;
  static unsigned long delayBotao = 0;

  bool estadoBotao = digitalRead(pinBotao);
  if ((estadoBotao != estadoAntBotao) && (millis() - delayBotao) > tempoDebounce )
  {
    estadoAntBotao = estadoBotao;
    delayBotao = millis();

    if ( estadoBotao)
    {
      lcd.clear();
      estadoRet = !estadoRet;
    }
  }

  return estadoRet;
}

void lcd1()
{
  digitalWrite(pinLED, HIGH);
  lcd.setCursor(0, 0);
  lcd.print("pagina 1");
  lcd.setCursor(0, 1);
  lcd.print("pagina 1");
}

void lcd2()
{
  digitalWrite(pinLED, LOW);
  lcd.setCursor(4, 0);
  lcd.print("pagina 2");
  lcd.setCursor(4, 1);
  lcd.print("pagina 2");
}

Nice one John, thank you for you answer... It Works perfectly! Thanks again!

But, just for I learn also, if I want to perform some command just once every time I call some function, wish is the best way?

greetings
Patrick

PatrickRosa:
just once every time

That makes no sense in English. Try asking your question again.

PatrickRosa:
But, just for I learn also, if I want to perform some command just once every time I call some function, wish is the best way?

Having something happen "just once" and having something happen "every time" are mutually exclusive. I think what you want to do is put code in lcd1() and lcd2() to do the lcd.clear() only when you switch between pages. To do that you could use a global variable to keep track of which page is currently displayed. In lcd1() and lcd2() you would check that variable to see if the current page is the same as the page you are about to display. If not, you are changing pages and therefore you clear the lcd.

#include <LiquidCrystal.h>
const int rs = 5, en = 6, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const byte pinBotao = 4;
const byte pinLED = 2;
byte CurrentPage = 0;
void setup()
{
  pinMode(pinBotao, INPUT);
  pinMode(pinLED, OUTPUT);
  lcd.begin(16, 2);
}
void loop()
{
  if ( botao1() )
  {
    lcd1();
  }
  else
  {
    lcd2();
  }
}
bool botao1()
{
  const unsigned tempoDebounce = 99;
  static bool estadoAntBotao;
  static bool estadoRet = true;
  static unsigned long delayBotao = 0;
  bool estadoBotao = digitalRead(pinBotao);
  if ((estadoBotao != estadoAntBotao) && (millis() - delayBotao) > tempoDebounce )
  {
    estadoAntBotao = estadoBotao;
    delayBotao = millis();
    if (estadoBotao)
    {
      estadoRet = !estadoRet;
    }
  }
  return estadoRet;
}
void lcd1()
{
  if (CurrentPage != 1)
  {
    lcd.clear();
    CurrentPage = 1;
  }
  digitalWrite(pinLED, HIGH);
  lcd.setCursor(0, 0);
  lcd.print("pagina 1");
  lcd.setCursor(0, 1);
  lcd.print("pagina 1");
}
void lcd2()
{
  if (CurrentPage != 2)
  {
    lcd.clear();
    CurrentPage = 2;
  }
  digitalWrite(pinLED, LOW);
  lcd.setCursor(4, 0);
  lcd.print("pagina 2");
  lcd.setCursor(4, 1);
  lcd.print("pagina 2");
}