Button activated LCD

I am having trouble getting the code right to have an lcd display different messages depending on which button i press.

this is the sketch i am using. I would really appreciate any feedback letting me know where i am going wrong

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 3, 2, 1, 0);

int inPin1 = 6;
int inPin2 = 7;
int inPin3 = 8;
int inPin4 = 9;
int inPin5 = 10;
int inPin6 = 0;
int inPin7 = 0;
int inPin8 = 0;
int inPin9 = 0;
int inPin10 = 0;

int bstate1 = 0;
int bstate2 = 0;
int bstate3 = 0;
int bstate4 = 0;
int bstate5 = 0;
int bstate6 = 0;
int bstate7 = 0;
int bstate8 = 0;
int bstate9 = 0;
int bstate10 = 0;

void setup()
{
lcd.begin(40, 2);

pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
pinMode(inPin3, INPUT);
pinMode(inPin4, INPUT);
pinMode(inPin5, INPUT);
pinMode(inPin6, INPUT);
pinMode(inPin7, INPUT);
pinMode(inPin8, INPUT);
pinMode(inPin9, INPUT);
pinMode(inPin10, INPUT);
lcd.print(“Awaiting Conversational Input…”);
}

void loop()
{
bstate1 = digitalRead(inPin1);
bstate2 = digitalRead(inPin2);
bstate3 = digitalRead(inPin3);
bstate4 = digitalRead(inPin4);
bstate5 = digitalRead(inPin5);
bstate6 = digitalRead(inPin6);
bstate7 = digitalRead(inPin7);
bstate8 = digitalRead(inPin8);
bstate9 = digitalRead(inPin9);
bstate10 = digitalRead(inPin10);

Serial.println(bstate1);
if( bstate1 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);

// }

if( bstate2 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);

}

if( bstate3 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);
}

if( bstate4 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);
}

if( bstate5 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);
}

if( bstate6 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);
}

if( bstate7 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);
}

if( bstate8 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);
}

if( bstate9 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);
}

if( bstate10 == HIGH )
{
lcd.setCursor(0, 0);
lcd.print(“Hello, My name is Benjamin”);
lcd.setCursor(0, 1);
lcd.print(“You may call me Ben.”);
}

else
{
lcd.setCursor(0,0);
lcd.print(“Awaiting Conversational Input…”);
}
}

You are not telling us what your problem is.

You are printing the same message to the LCD no matter what button is pressed.

You are not debouncing the buttons.

oh, oops sorry. The problem is that it doesn't even wait for the buttons to be pressed. I Changed all the messages, so that they are just stating what button they are from, i.e. button number 1, button number 2, etc.... but it shows all the messages cycling extremely quickly. I am currently debouncing them all to see if it works.

What I am trying to do is have message number one come up when i press button number 1 and only stay on as long as i press button one.

The problem is that it doesn't even wait for the buttons to be pressed.

Sure, you did not tell it to wait for a button press ;)

You could do that with an inner loop reading all the pins until one gets high. Or you could change the output code to do nothing if there is no active input pin.

BTW: the { } structures of your 'if' clauses seams wrong. The way you put it it will only check inPIN1, if it's high it will check all the others too, if it's low it will not check the others, but do the else clause. I do not think this is what you want...

errm, sorry, what I wrote is not exactly what I meant:

Your code will check all the input pins, but it will only react on these readings if inPIN1 is high.

OMG! thanx.... ok now i am figuring it out. ::Sigh:: i've been staring at this screen for too long.

Thank you so much, it has started to work.

What I am trying to do is have message number one come up when i press button number 1 and only stay on as long as i press button one.

Once you send data to the LCD that data will be displayed as long as power is still applied. In order for a message to disappear when you release a button you will have to specifically clear the display.

Don

In addition to the code problems, check how you have hooked up the buttons. If you are not using pull-up or pull-down resistors then the digitalRead() will randomly fluctuate between HIGH and LOW.

What I am trying to do is have message number one come up when i press button number 1 and only stay on as long as i press button one.

A rewrite of your application using for loops so its less bulky. It is not the most efficient implementation or complete. It uses pin2,3,4,5 and it needs tinkering to meet your needs but it will get you started.

void setup()
{
  Serial.begin(9600);
  Serial.println("Start");  // just to check serial IO
  
  // initialize pins 2 - 6 
  for (int i = 2; i< 6; i++)
  {
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);    // internal pull up resistor - prevents fluctuating pin
  }
}

void loop()
{
  // b holds the pinnumber or -1 if no pin is low.
  int b = CheckButtons();
  ShowMessage(b);
  delay(100);
}

// returns the number of the first pin found that is LOW (grounded)
int CheckButtons()
{
  for (int i=2; i<6; i++) 
    {
      if (digitalRead(i) == LOW ) return i;
    }
 return -1;
}

void ShowMessage(int i)
{
  Serial.println(i);      // to be replaced by real messages
  switch(i) {
    case 2: Serial.println("Two"); break;
    case 3: Serial.println("Three"); break;
    // other the same
    default: Serial.println("ClearScreen"); break;
  }
}