Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int REED = 4;
int LED = 8;
int LED2 = 6;
int x = 9;

LiquidCrystal_I2C lcd(0x20, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

void setup()
{
Serial.begin(9600); // Used to type in characters
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines and turn on backlight
lcd.backlight(); // finish with backlight on

lcd.setCursor(3,0); //Start at character 4 on line 0
lcd.print("Hello");
}

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);
}

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!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int REED = 4;
int LED = 8;
int LED2 = 6;
int x = 9;

LiquidCrystal_I2C lcd(0x20, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

void setup()
{
pinMode(LED2, INPUT);
Serial.begin(9600); // Used to type in characters
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines and turn on backlight
lcd.backlight(); // finish with backlight on

lcd.setCursor(3,0); //Start at character 4 on line 0
lcd.print("Hello");
delay(500);
}

void loop()
{
lcd.home ();
lcd.clear();
int ledState;
int x;
ledState = digitalRead(LED2);

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

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

delay (200);
}

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.

Latest threads

New Articles From Microcontroller Tips

Back
Top