Problem with turning LCD off

So, I'm new to the Arduino language and I'm trying to program something I thought would be pretty simple:

If a pin (6) on the Arduino is high, display a short message on the LCD, and if that same pin goes low, turn off the lcd screen. I'm pretty sure the circuit is built correctly, because I have gotten it to work with some simpler codes, but now I'm having a hard time.

I am not getting any errors, but when I compile, the LCD will display some random symbols (?, _, o, and arrows). I've tried a few different versions of the code and sometimes I will get it to display my desired message, but not when it's supposed to (ie, the message will show up even though port 6 (powervalp06) == low). Unfortunately I seem to have deleted or saved over the code that was partially working.

I am receiving the correct display text now, however, it is always being displayed. The lcd is not turning off when pin 6 is low as I would like.
To add to that: The serial command I have set up within the circuit tells me that pin 6 is low (0), but as I mentioned before, the lcd is still displaying the message instead of turning off.

My latest code is below. Can anyone help?

#include <LiquidCrystal.h> //include LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //interface pins 

int power = 6; //power switch connected to pin 6
int powervalp06 = 0; //value of pin 6

void setup() {
  pinMode(power,INPUT); //sets 6 as input
  lcd.begin(16,2);
  lcd.clear();
  Serial.begin(9600);
}

void loop() {
  
  powervalp06 = digitalRead(power); 
  
  Serial.print("powervalp06 = ");       // prints a label
  Serial.println(powervalp06);   // prints a tab
  delay(500);
  
  if ((digitalRead(powervalp06) == HIGH)) {
    
    lcd.print("Hello! Press");
    lcd.setCursor(0,1);
    lcd.print("button to begin.");
    lcd.noCursor();
  }
  else {
    lcd.noDisplay ();
  }
}

If a port (6) on the Arduino is high

Which Arduino as 6 ports? Pins and ports are not the same thing, by a factor of 8.

  pinMode(power,INPUT); //sets 6 as input

But, without using the internal pullup resistor. So, you need an external pullup or pulldown resistor, or something supplying a distinct HIGH or LOW. What is wired to the PIN?

Go back to basics. Forget about the button being pressed or not for now and just print something to the LCD. If that does not work then check your wiring to the LCD and the integrity of all joints, jumper leads and plugs. Push/pull/wiggle the leads. Does anything change ? Have you got the LCD backlight turned on and contrast adjusted with a suitable potentiometer ?

PaulS:

If a port (6) on the Arduino is high

Which Arduino as 6 ports? Pins and ports are not the same thing, by a factor of 8.

  pinMode(power,INPUT); //sets 6 as input

But, without using the internal pullup resistor. So, you need an external pullup or pulldown resistor, or something supplying a distinct HIGH or LOW. What is wired to the PIN?

I'm sorry, I meant pin! I'll change that.
I am also using a pull down resistor connected to the switch.

UKHeliBob:
Go back to basics. Forget about the button being pressed or not for now and just print something to the LCD. If that does not work then check your wiring to the LCD and the integrity of all joints, jumper leads and plugs. Push/pull/wiggle the leads. Does anything change ? Have you got the LCD backlight turned on and contrast adjusted with a suitable potentiometer ?

Hello! Thanks for the comment. One of the wires must have gotten mixed up. I am now receiving the proper display words, and not the symbols. Unfortunately, the text is always being displayed using the same code as above, and does not change depending on whether or not pin 6 is high or low.

So, some progress.

Now, which pin is this reading ?

  if ((digitalRead(powervalp06) == HIGH))

UKHeliBob:
So, some progress.

Now, which pin is this reading ?

  if ((digitalRead(powervalp06) == HIGH))

It is supposed to be reading pin 6, but now that you mention it I don't know if that is specified anywhere. Is there a way to differentiate between which pin you're assigning it to and what value (high or low) it is set at?

EDIT: No, I'm wrong. It should be reading pin 6 from this line:

  powervalp06 = digitalRead(power);

EDIT: No, I'm wrong. It should be reading pin 6 from this line:

That assigns a value of 0 (LOW) or 1 (HIGH) to powervalp06;

So, when you use:

if ((digitalRead(powervalp06) == HIGH))

Which pin did you read from? The answer, of course, is either pin 0 or pin 1. Is that the pin you want to be reading from? I don't think it is.

PaulS, thank you! That makes a lot of sense and it did fix my problem. :smiley:

I do have one more question though, because it still isn't working exactly how I would like: should my loop be continuously running from the code I'm using? When I run it, the message will be displayed if pin 6 starts high and will turn off when it goes low, but it will not turn back on when pin 6 goes high unless I restart the program.

    lcd.noDisplay ();Turns the LCD display off. Where are you turning it on again ?

Oops, good point! Sorry, I'm still new to this so I miss a lot of the simple things. :relaxed:

Thanks for your help!

No worries. BTDTGTTS many times.

Is it working now that you are turning the display on again ?

UKHeliBob:
No worries. BTDTGTTS many times.

Is it working now that you are turning the display on again ?

Haha, well, I think I actually fried the ATmega. I was moving things around without shutting off the power. Oops. :blush:
I believe that it will work with that change though. I don't think I'm going to be able to replace it today, so I'll find out soon!

No worries. BTDTGTTS many times.

You left out an F there...

I was moving things around without shutting off the power. Oops.

Yeah, ouch. I hope you learned something from this, though.