LCD push button

Hello !! I'm kinda playing around with my arduino and i'm attempting to learn how to display stuff on my LCD. I want it to display a certain message when my push button is released, and then another message when i push the button down. I followed the guide for the liquid Crystal and powered up my arduino perfectly. Now i want to get a little tricky with the push button. I set up my push button as a digital input (7). I tried this code but couldn't get it to work. Any suggestions ?!?!

#include <LiquidCrystal.h>

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

int pin=7;
int state=0;

void setup() {
  Serial.begin(9600);
  pinMode(pin,INPUT);
  lcd.begin(16,2);
}
  void loop() {
state=digitalRead(pin);

if (state=HIGH)
  lcd.setCursor(0,0);
  lcd.print("I am new to this");
if (state=LOW)
  lcd.setCursor(0,1);
  lcd.print("hellow");


Serial.println(state);
delay(500);
  }

Hint., look at the difference between = and ==
if (state=HIGH)
OR
if (state==HIGH)

Also braces enclosing the conditional code.

if (state=HIGH)
  lcd.setCursor(0,0);
  lcd.print("I am new to this");
if (state=LOW)
  lcd.setCursor(0,1);
  lcd.print("hellow");

presumably should be

if (state==HIGH) {
  lcd.setCursor(0,0);
  lcd.print("I am new to this");
}
if (state==LOW) {
  lcd.setCursor(0,1);
  lcd.print("hellow");
}

Unless your intending is very misleading.


Rob

Hey ! I got it to work without using if statements. Here's my new code. I'll try using if statements just to get a hang of both. I'll look into = and ==. Thanks guys !!

#include <LiquidCrystal.h>

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

int pin=7;
int state=0;

void setup() {
  Serial.begin(9600);
  pinMode(pin,INPUT);
  lcd.begin(16,2);
}
  void loop() {
state=digitalRead(pin);

switch(state) {
  case LOW:
  lcd.clear();                //clears LCD screen
  lcd.setCursor(0,0);         // set the cursor to column 0, line 1
                              // (note: line 1 is the second row, since counting begins with 0):
  lcd.print("Hello !");       // prints text onto assigned cursor position
  break;                      //exit from loop
  case HIGH:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("I pushed the");
  lcd.setCursor(0,1);
  lcd.print("button");
  break;
}
Serial.println(state);
delay(500);
  }

New question now :smiley:

When i push the button to switch to the next text, is there a way that i can push it, and it will hold the message even when i release the button ?

Detect the fact that the button was just pressed (not still pressed) and only change the LCD text if that is the case.

if button is high and button was low
    update LCD

And BTW, use CODE tags not QUOTE for code.


Rob

I will try that !! and yea, sorry. Must have clicked quote when i meant code. Thanks for the tip though. I'll let you know my progress.