Switching LCD

Hi
Each time I pressing button the LED is ON and OFF = OK, but LCD is not , the text appears in both lines after 2 pressings and stays that way, should be switching between (“hello 1”) and (“hello2”).
I tried // lcd.clear(); probably incorrectly.

#include <LiquidCrystal.h>


LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);


#include <stdio.h>


#include <stdlib.h>





const int button = PB11;            // GPIO 8 for the button


const int led = PC13;               // GPIO 7 for the LED


int ledflag = 0;                 // LED status flag


  byte z,z0,z1;


  int ur,ul,urr,ull;





void setup() {


  lcd.begin(16, 2);


  pinMode(button, INPUT_PULLDOWN);         // define button as an 
input


  pinMode(led, OUTPUT);          // define LED as an output


  digitalWrite(led, LOW);        // turn output off just in case


  


}





void loop() {


  if (digitalRead(button) == HIGH) { // if button is pressed


    if (ledflag == 0) {           // and the status flag is LOW


      ledflag = 1;                // make status flag HIGH


      digitalWrite(led, HIGH);


        lcd.setCursor(0, 0);


      lcd.print("hello  1");// and turn on the LED


      //lcd.clear();


    }                           //


    else {                        // otherwise...


      ledflag = 0;                // make status flag LOW


      digitalWrite(led, LOW);


         lcd.setCursor(0, 1);


      lcd.print("hello2");// and turn off the LED


     // lcd.clear();


    }


    delay(1000);                    // wait a sec for the


  }                               // hardware to stabilize


 //  updateDisplay(); 


}                                 // begin again
            lcd.setCursor(0, 0);
            lcd.print("hello  1");// and turn on the LED

.
.
.

            lcd.setCursor(0, 1);
            lcd.print("hello2");// and turn off the LED

You set row 0 for "hello1" and row 1 for "hello2".

Are you expecting the hello messages to be on the top row only?

I want to display only one of them at the time. When "hello 1" is displayed "hello 2" should be erased and vice versa.

So you've got:

        if (ledflag == 0) 
        {           
            // and the status flag is LOW
            ledflag = 1;                // make status flag HIGH
            digitalWrite(led, HIGH);
            lcd.setCursor(0, 0);
            lcd.print("hello  1");// and turn on the LED
            //lcd.clear();
    
        }

You had, but commented out, lcd.clear(): Where else in this section of code might that line go to get what you want?

Suppose you have have something on the screen now and you've push the button and you're now executing this. Walk through the code line by line and think about where you would clear it in relation to printing "hello 1".

Then repeat this thinking in your other 'else'.

BTW, there are better ways to check for keypresses but we'll leave that for later, after you've sorted this bit.

Hi,

 pinMode(button, INPUT_PULLDOWN);         // define button as an input

Unfortunately there is no INPUT_PULLDOWN parameter in Arduino.

What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,

 pinMode(button, INPUT_PULLDOWN);         // define button as an input

Unfortunately there is no INPUT_PULLDOWN parameter in Arduino.
INPUT_PULLDOWN is for stm32f, for arduino is just INPUT + pulldown resistor.

stm32 =const int button = PB11;

pinMode(button, INPUT_PULLDOWN);         // define button as an input

arduino = // GPIO 8 for the button + pull down resistor

pinMode(button, INPUT);

Do you need the drawing?

I did it

  digitalWrite(led, HIGH);
  lcd.clear();[color=#222222][/color]
  lcd.setCursor(0, 0);[color=#222222][/color]
  lcd.print("hello  1");// and turn on the LED

BTW, there are better ways to check for keypresses but we'll leave that for later, after you've sorted this bit ?

Vik321:
Do you need the drawing?

Yes, please, BUT not a Fritzy picture.
If you have an external PULL DOWN resistor you do not need to use.

pinMode(button, INPUT_PULLDOWN);         // define button as an input

This is enough;

pinMode(button, INPUT);         // define button as an input

What model Arduino are you using?
What IDE did you write this code?
Tom... :slight_smile:

Hi,
You do not need to clear the display each time, this will produce a flicker, also you have to rewrite the entire LCD screen each time.
Try this, you just fill the positions on the screen with blanks where you want to rewrite information.

   digitalWrite(led, HIGH);
   lcd.setCursor(0, 0);
   lcd.print("        ");// fill with blanks
   lcd.setCursor(0, 0); // overwrite new data
   lcd.print("hello  1");

Tom... :slight_smile:

I am using STM32F103, the difference compare to arduino in this case is only in pins names and no needs for pool down resistors. Yes I know about - fill with blanks - but I put this in wrong line, thanks to Blackfin sugestion I solved the problem. I didn't know that fill with blanks is solution for flickering.
Thanks to both of you.

Vik321:
I am using STM32F103, the difference compare to arduino in this case is only in pins names and no needs for pool down resistors. Yes I know about - fill with blanks - but I put this in wrong line, thanks to Blackfin sugestion I solved the problem. I didn’t know that fill with blanks is solution for flickering.
Thanks to both of you.

But it makes a big difference trying to compile your code if we don’t know the controller.
Good to see you have a solution.
Tom… :slight_smile:

Now I want to replace “hello 1” by bar graph, the screen is switching properly but bar graph is not responding to input signal on PA7 and PB0.

#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <stdio.h>
#include <stdlib.h>
///////////////////////
#define GAIN 5.5
#define STEP 10


 byte a1[8] = {0b00000,0b10101,0b10101,0b10101,0b10101,0b10101,0b10101,0b00000};
  byte a2[8] = {0b00000,0b10100,0b10100,0b10100,0b10100,0b10100,0b10100,0b00000};
  byte a3[8] = {0b00000,0b10000,0b10000,0b10000,0b10000,0b10000,0b10000,0b00000};
  byte z,z0,z1;
  int ur,ul,urr,ull;
/////////////////////
const int button = PB11;            // GPIO 8 for the button
const int led = PC13;               // GPIO 7 for the LED
int ledflag = 0;                 // LED status flag

void setup() {
  lcd.begin(16, 2);
  pinMode(button, INPUT_PULLDOWN);         // define button as an
  pinMode(led, OUTPUT);          // define LED as an output
  digitalWrite(led, LOW);        // turn output off just in case
  ///////////////////
lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3);


  pinMode(PA6,INPUT);// A0 - аналоговый вход
  pinMode(PB0,INPUT);// A1 - аналоговый вход
  /////////////
}
void loop() {
//////////////
 // urr = log(analogRead(PA6))*GAIN;
 // ull = log(analogRead(PB0))*GAIN;
 /////////////
  if (digitalRead(button) == HIGH) { // if button is pressed
    if (ledflag == 0) {           // and the status flag is LOW
      ledflag = 1;                // make status flag HIGH
      digitalWrite(led, HIGH);
   
      /*
        lcd.clear();
      // lcd.print("        ");// fill with blanks
      lcd.setCursor(0, 0);
      lcd.print("hello  1");// and turn on the LED
      */
  urr = log(analogRead(PA6))*GAIN;
  ull = log(analogRead(PB0))*GAIN;
 
  lcd.setCursor(0,1);lcd.print("R");
  lcd.setCursor(0,0);lcd.print("L");
   // lcd.clear();
   if(urr<ur){ur=ur-1;delay(STEP);}else{ur = urr;}
  for(z=0,z0=0,z1=0;z<=ur;z++,z1++){if(z1>2){z1=0;z0++;}
   if(z1==1){lcd.setCursor(z0+1,1);lcd.write((uint8_t)0);lcd.setCursor(z0+2,1);lcd.print(" ");}}
   if(z1==3){lcd.setCursor(z0+1,1);lcd.write((uint8_t)1);}
   if(z1==2){lcd.setCursor(z0+1,1);lcd.write((uint8_t)2);}

if(ull<ul){ul=ul-1;delay(STEP);}else{ul = ull;}
  for(z=0,z0=0,z1=0;z<=ul;z++,z1++){if(z1>2){z1=0;z0++;}
   if(z1==1){lcd.setCursor(z0+1,0);lcd.write((uint8_t)0);lcd.setCursor(z0+2,0);lcd.print(" ");}}
   if(z1==3){lcd.setCursor(z0+1,0);lcd.write((uint8_t)1);}
   if(z1==2){lcd.setCursor(z0+1,0);lcd.write((uint8_t)2);}
      /////////////////////
    }
  else {                        // otherwise...
ledflag = 0;                // make status flag LOW
 digitalWrite(led, LOW);
       //lcd.print("        ");// fill with blanks
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("hello2");// and turn off the LED
 }
    delay(1000);                    // wait a sec for the
  }                               // hardware to stabilize
}

Vik321:
Now I want to replace “hello 1” by bar graph, the screen is switching properly but bar graph is not responding to input signal on PA7 and PB0.

  pinMode(PA6,INPUT);// A0 - аналоговый вход

pinMode(PB0,INPUT);// A1 - аналоговый вход
 /////////////
}
void loop() {
//////////////
// urr = log(analogRead(PA6))*GAIN;
// ull = log(analogRead(PB0))*GAIN;

PA6 and PB0 in your code.
Are you getting data from those analog inputs?
Have you got some simple code just reading the inputs and simply displaying the raw data?
Sorry had to edit your code so I could read it;

#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <stdio.h>
#include <stdlib.h>
///////////////////////
#define GAIN 5.5
#define STEP 10




byte a1[8] = {0b00000, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101, 0b00000};
byte a2[8] = {0b00000, 0b10100, 0b10100, 0b10100, 0b10100, 0b10100, 0b10100, 0b00000};
byte a3[8] = {0b00000, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b00000};
byte z, z0, z1;
int ur, ul, urr, ull;
/////////////////////
const int button = PB11;            // GPIO 8 for the button
const int led = PC13;               // GPIO 7 for the LED
int ledflag = 0;                 // LED status flag


void setup()
{
  lcd.begin(16, 2);
  pinMode(button, INPUT_PULLDOWN);         // define button as an
  pinMode(led, OUTPUT);          // define LED as an output
  digitalWrite(led, LOW);        // turn output off just in case
  ///////////////////
  lcd.createChar(0, a1); lcd.createChar(1, a2); lcd.createChar(2, a3);




  pinMode(PA6, INPUT); // A0 - аналоговый вход
  pinMode(PB0, INPUT); // A1 - аналоговый вход
  /////////////
}
void loop()
{
  //////////////
  // urr = log(analogRead(PA6))*GAIN;
  // ull = log(analogRead(PB0))*GAIN;
  /////////////
  if (digitalRead(button) == HIGH)
  { // if button is pressed
    if (ledflag == 0)
    { // and the status flag is LOW
      ledflag = 1;                // make status flag HIGH
      digitalWrite(led, HIGH);


      /*
        lcd.clear();
        // lcd.print("        ");// fill with blanks
        lcd.setCursor(0, 0);
        lcd.print("hello  1");// and turn on the LED
      */
      urr = log(analogRead(PA6)) * GAIN;
      ull = log(analogRead(PB0)) * GAIN;


      lcd.setCursor(0, 1); lcd.print("R");
      lcd.setCursor(0, 0); lcd.print("L");
      // lcd.clear();
      if (urr < ur)
      {
        ur = ur - 1;
        delay(STEP);
      }
      else
      {
        ur = urr;
      }
      for (z = 0, z0 = 0, z1 = 0; z <= ur; z++, z1++)
      {
        if (z1 > 2)
        {
          z1 = 0;
          z0++;
        }
        if (z1 == 1) {
          lcd.setCursor(z0 + 1, 1);
          lcd.write((uint8_t)0);
          lcd.setCursor(z0 + 2, 1);
          lcd.print(" ");
        }
      }
      if (z1 == 3)
      {
        lcd.setCursor(z0 + 1, 1);
        lcd.write((uint8_t)1);
      }
      if (z1 == 2)
      {
        lcd.setCursor(z0 + 1, 1);
        lcd.write((uint8_t)2);
      }


      if (ull < ul)
      {
        ul = ul - 1;
        delay(STEP);
      }
      else
      {
        ul = ull;
      }
      for (z = 0, z0 = 0, z1 = 0; z <= ul; z++, z1++)
      {
        if (z1 > 2)
        {
          z1 = 0;
          z0++;
        }
        if (z1 == 1)
        {
          lcd.setCursor(z0 + 1, 0);
          lcd.write((uint8_t)0);
          lcd.setCursor(z0 + 2, 0);
          lcd.print(" ");
        }
      }
      if (z1 == 3)
      {
        lcd.setCursor(z0 + 1, 0);
        lcd.write((uint8_t)1);
      }
      if (z1 == 2)
      {
        lcd.setCursor(z0 + 1, 0);
        lcd.write((uint8_t)2);
      }
      /////////////////////
    }
    else
    { // otherwise...
      ledflag = 0;                // make status flag LOW
      digitalWrite(led, LOW);
      //lcd.print("        ");// fill with blanks
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("hello2");// and turn off the LED
    }
    delay(1000);                    // wait a sec for the
  }                               // hardware to stabilize
}

Tom… :slight_smile:

This is arduino pins bar graph

#define GAIN 15
#define STEP 10

#include <LiquidCrystal.h>
  LiquidCrystal lcd(2, 3, 4, 5, 6, 7);// RS,E,D4,D5,D6,D7
  byte a1[8] = {0b00000,0b10101,0b10101,0b10101,0b10101,0b10101,0b10101,0b00000};
  byte a2[8] = {0b00000,0b10100,0b10100,0b10100,0b10100,0b10100,0b10100,0b00000};
  byte a3[8] = {0b00000,0b10000,0b10000,0b10000,0b10000,0b10000,0b10000,0b00000};
  byte z,z0,z1;
  int ur,ul,urr,ull;
 
  

void setup() {
  lcd.begin(16, 2);// LCD 16X2
  lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3);
 // analogReference(INTERNAL);
  pinMode(A0,INPUT);// A0 
  pinMode(A1,INPUT);// A1 
}

void loop() {
  urr = log(analogRead(0))*GAIN;
  ull = log(analogRead(1))*GAIN;
 
  lcd.setCursor(0,1);lcd.print("R");
  lcd.setCursor(0,0);lcd.print("L");
  
   if(urr<ur){ur=ur-1;delay(STEP);}else{ur = urr;}
  for(z=0,z0=0,z1=0;z<=ur;z++,z1++){if(z1>2){z1=0;z0++;}
   if(z1==1){lcd.setCursor(z0+1,1);lcd.write((uint8_t)0);lcd.setCursor(z0+2,1);lcd.print(" ");}}
   if(z1==3){lcd.setCursor(z0+1,1);lcd.write((uint8_t)1);}
   if(z1==2){lcd.setCursor(z0+1,1);lcd.write((uint8_t)2);}

   if(ull<ul){ul=ul-1;delay(STEP);}else{ul = ull;}
  for(z=0,z0=0,z1=0;z<=ul;z++,z1++){if(z1>2){z1=0;z0++;}
   if(z1==1){lcd.setCursor(z0+1,0);lcd.write((uint8_t)0);lcd.setCursor(z0+2,0);lcd.print(" ");}}
   if(z1==3){lcd.setCursor(z0+1,0);lcd.write((uint8_t)1);}
   if(z1==2){lcd.setCursor(z0+1,0);lcd.write((uint8_t)2);}
   
  delay(1);
}

Hi,
Can I suggest you forget log function for the moment and just see if you can get a working bar graph using raw data from a pot on the analog input.

Tom... :slight_smile: