Display mode on LCD with two binary conditions.

I have assigned two pins on the ATMEGA328 microcontroller with the Arduino bootloader. I am using pin 7 as Q1 and assigned it that name as a function, and pin 8 as Q2 and assigned it that name as a function. When there are various HIGH or LOW conditions met on those pins I want it to display what mode the circuit is in. The LCD does display the mode but I believe it refreshes so fast that the LCD screen is blurry. Is there a way I can optimize the code so that it will only refresh the LCD screen when the conditions have changed? Below is the code I am using for that section of the program. Also do I need to clear the LCD screen every time I write to it or can I just print to it without clearing it because on line two of the LCD screen I am printing a different function later in the program but want to retain what is printed on line one of the LCD screen.

Thanks

#include <LiquidCrystal.h>

LiquidCrystal lcd(11, 10, 5, 4, 3, 2);

// setup the pins to be used
void setup()
  {
    pinMode (7, INPUT); // Q1 for mode select
    pinMode (8, INPUT); // Q2 for mode select
  }


//////MAIN LOOP\\\\\\\

void loop()
  {
    modelcd();
  }

///////functions\\\\\\\\\ 

// Display mode on LCD screen
void modelcd()
  {
    int Q1 = digitalRead(7);
    int Q2 = digitalRead(8);
      if (Q1 == LOW && Q2 == LOW)
        {
          lcd.begin(16,2);
          lcd.setCursor(0,0);
          lcd.print("RIBBON CONTROL");
        }
      if (Q1 == HIGH && Q2 == LOW)
        {
          lcd.begin(16,2);
          lcd.setCursor(0,0);
          lcd.print("PUSH BUTTON");
        }
      else
        {
          lcd.begin(16,2);
          lcd.setCursor(0,0);
          lcd.print("LIGHT CONTROL");
        }
   }

Is there a way I can optimize the code so that it will only refresh the LCD screen when the conditions have changed?

Have two boolean variables save the last state of the input pins. When these variables do not agree with the read value of the pins then do your "if" stuff. Note: you may want to consider debouncing your inputs before determining if the inputs have changed.

"Also do I need to clear the LCD screen every time "
I usually clear the line.

LarryD:

Is there a way I can optimize the code so that it will only refresh the LCD screen when the conditions have changed?

Have two boolean variables save the last state of the input pins. When these variables do not agree with the read value of the pins then do your "if" stuff. Note: you may want to consider debouncing your inputs before determining if the inputs have changed.

Can you give me an example of how to do that?
And I have the buttons hardware debounced.

Why are you repeating the lcd.begin() every time through loop() ? It only needs to be used once in setup().

You asked whether you needed to clear the screen each time, but you are not actually clearing the screen which would usually be done with lcd.clear(). Are you sure that the current logic works ? As written, if Q1 is LOW and Q2 is LOW then the LCD will show LIGHT CONTROL because of the else command on the second if and it can never show RIBBON CONTROL for other than a fleeting moment. Is this the cause of the flickering I wonder ?

Can you please explain what you mean by

I have assigned two pins on the ATMEGA328 microcontroller with the Arduino bootloader. I am using pin 7 as Q1 and assigned it that name as a function, and pin 8 as Q2 and assigned it that name as a function.

Everything you wanted to know about switches (maybe not).
http://www.ladyada.net/learn/arduino/lesson5.html

/*
 *  Alternating switch
 */

int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int buttonState;                // variable to hold the last button state

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}
void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val

  if (val != buttonState) {          // the button state has changed!
    if (val == LOW) {                // check if the button is pressed
      Serial.println("Button just pressed");
    } else {                         // the button is -not- pressed...
      Serial.println("Button just released");
    }
  }

  buttonState = val;                 // save the new state in our variable
}

You may be interested in:
Also State Machine

Can you please explain what you mean by

I have assigned two pins on the ATMEGA328 microcontroller with the Arduino bootloader. I am using pin 7 as Q1 and assigned it that name as a function, and pin 8 as Q2 and assigned it that name as a function.

[/quote]

I have the Q1 and Q2 of an IC chip passing states depending on which mode the circuit is in. I know this is functioning because the circuit does switch states and I used a logic probe to verify the outputs. The Q1 and Q2 of that chip is attached to pin 7 and pin 8 on the microcontroller. Right now there is only three mode the circuit switches between.

The code below reflects the changes I have made per your suggestions.

#include <LiquidCrystal.h>

LiquidCrystal lcd(11, 10, 5, 4, 3, 2);

// setup the pins to be used
void setup()
  {
    pinMode(7, INPUT); // Q1 for mode select
    pinMode(8, INPUT); // Q2 for mode select
    lcd.begin(16,2);
  }

//////MAIN LOOP\\\\\\\

void loop()
  {
    modelcd();
  }

///////functions\\\\\\\\\ 

// Display mode on LCD screen
void modelcd()
  {
    int Q1 = digitalRead(7);
    int Q2 = digitalRead(8);
      if (Q1 == LOW && Q2 == LOW)
        {
          lcd.setCursor(0,0);
          lcd.print("RIBBON CONTROL  ");
        }
      if (Q1 == HIGH && Q2 == LOW)
        {
          lcd.setCursor(0,0);
          lcd.print("PUSH BUTTON     ");
        }
      if (Q1 == HIGH && Q2 == HIGH)
        {
          lcd.setCursor(0,0);
          lcd.print("LIGHT CONTROL   ");
        }
       else
        {
        }
  }

Try:

#include <LiquidCrystal.h>

LiquidCrystal lcd(11, 10, 5, 4, 3, 2);
boolean lastQ1;
boolean lastQ2;
boolean Q1;
boolean Q2;

// setup the pins to be used
void setup()
{
//  Serial.begin(9600);
  pinMode(7, INPUT); // Q1 for mode select
  pinMode(8, INPUT); // Q2 for mode select
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  lastQ1 = digitalRead(7);
  lastQ2 = digitalRead(8);

  lcd.begin(16,2);
}

//////MAIN LOOP\\\\\\\

void loop()
{
  Q1 = digitalRead(7);
  Q2 = digitalRead(8);

  if (lastQ1 != Q1 || lastQ2 != Q2)
  {
    lastQ1 = Q1;
    lastQ2 = Q2;
    modelcd();
  }

  // Do other stuff

}   // END of loop()


///////functions\\\\\\\\\ 

// Display mode on LCD screen
void modelcd(void)
{
  //  int Q1 = digitalRead(7);
  //  int Q2 = digitalRead(8);
  if (Q1 == LOW && Q2 == LOW)
  {
    lcd.setCursor(0,0);
    lcd.print("RIBBON CONTROL  ");
//    Serial.println("RIBBON CONTROL  ");
  }
  if (Q1 == HIGH && Q2 == LOW)
  {
    lcd.setCursor(0,0);
    lcd.print("PUSH BUTTON     ");
  }
  if (Q1 == HIGH && Q2 == HIGH)
  {
    lcd.setCursor(0,0);
    lcd.print("LIGHT CONTROL   ");
//    Serial.println("LIGHT CONTROL   ");
  }
  else
  {
  }
}

I made some changes in the previous code.

brucebly:
Can you please explain what you mean by

I have assigned two pins on the ATMEGA328 microcontroller with the Arduino bootloader. I am using pin 7 as Q1 and assigned it that name as a function, and pin 8 as Q2 and assigned it that name as a function.

I have the Q1 and Q2 of an IC chip passing states depending on which mode the circuit is in. I know this is functioning because the circuit does switch states and I used a logic probe to verify the outputs. The Q1 and Q2 of that chip is attached to pin 7 and pin 8 on the microcontroller. Right now there is only three mode the circuit switches between.

The code below reflects the changes I have made per your suggestions.

#include <LiquidCrystal.h>

LiquidCrystal lcd(11, 10, 5, 4, 3, 2);

// setup the pins to be used
void setup()
  {
    pinMode(7, INPUT); // Q1 for mode select
    pinMode(8, INPUT); // Q2 for mode select
    lcd.begin(16,2);
  }

//////MAIN LOOP\\\\\\\

void loop()
  {
    modelcd();
  }

///////functions\\\\\\\\\ 

// Display mode on LCD screen
void modelcd()
  {
    int Q1 = digitalRead(7);
    int Q2 = digitalRead(8);
      if (Q1 == LOW && Q2 == LOW)
        {
          lcd.setCursor(0,0);
          lcd.print("RIBBON CONTROL  ");
        }
      if (Q1 == HIGH && Q2 == LOW)
        {
          lcd.setCursor(0,0);
          lcd.print("PUSH BUTTON     ");
        }
      if (Q1 == HIGH && Q2 == HIGH)
        {
          lcd.setCursor(0,0);
          lcd.print("LIGHT CONTROL   ");
        }
       else
        {
        }
  }

[/quote]
It was the mention of the bootloader that intrigued me.
Your code looks like it should work. Assuming that it works does the LCD still flicker ?

I would suggest that you tidy up by removing the else as it is doing nothing at the moment, but replace the three stand alone ifs with if, else if and a second else if. That way once a test succeeds the subsequent ifs will not be executed as it would be superfluous to do so. I see that you have added spaces to the end of the lcd literals which avoids the needs to clear the screen or the line.