LCD display code problem

Status
Not open for further replies.
I am stuck on one simple thing.

I have 20x04 LCD display with IC2 board (the last one here): **broken link removed**

Here is what i want. If digital pin 6 is 5V, then write 5 on LCD, or if pin 6 is 0V, then write 0

void loop()
{
lcd.home ();
lcd.clear();
int x

;
if (LED2==HIGH) x=5;
else x=0;

lcd.setCursor ( 0, 2 );
lcd.print (x);

delay (200);
}

LCD always shows 0, even when it is not. And even when i reset atmega 328p

Please help me if you know what is wrong in my code
 
Here is the whole code:

What sets D6 high or low? ME I manually put DIGITAL pin 6 (PIN 12 fisically) to GND or to +5V..
It is normally 0V, and it display it says 0, but when i put 5V to it, it still says 0..
 
Code:
int LED2 = 6;

LED2 will ALWAYS be high... You assigned the variable a 6...

You need to assign a pin to an output..

Code:
pinMode(LED2, OUTPUT);

Then control it... Set it high..
Code:
digitalWrite(LED2, HIGH);
Or low..
Code:
digitalWrite(lLED2, LOW);

I don't know whether you can read a pin whilst in output mode..

ledState = digitalRead(LED2);

If not you'll need an output buffer so you can read the buffer instead of the pin...
 
Hi,

Ian's right on the money. Testing the value 6 will always equate to high.

To get to where you want to be with reading a pin you're externally toggling,
Code:
if(digitalRead(LED2)==HIGH) x=5; else x=0;
will do the trick. Or, since HIGH and true are the same internally to an Arduino, you could simplify this to
Code:
if(digitalRead(LED2)) x= 5; else x = 0;

You can read a pin in output mode so that last part is fine. I'll often use
Code:
digitalWrite(!digitalRead(LEDPIN));
to toggle a LED from whatever state is it is to the alternative on an output pin and that's all good.

Cheers !
Geoff
 
Last edited:
You are awesome ! Thank you

Here is what i did, and now it works great. Finally!


That pin 6 is input pin, it will be connected to the switch, so i dont need digitalWrite on it but i get the point
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…