digitalRead statement issue

Hi, I would like to pose a question for the Arduino reference (Arduino Reference - Arduino Reference) regarding digitalRead command as I'm quite new to these c:

May I know why is it so that when I wrote (under void setup)

Arduino uno
#include <LiquidCrystal.h>

const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LLiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//VOID SETUP//

void setup() {
pinMode(2, INPUT);
pinMode(4, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(4,HIGH);
}
//VOID LOOP//

void loop(){
if (digitalRead(2) == HIGH){
lcd.clear();
lcd.print("TYPE: GAIN");
lcd.cursor(0, 1);
lcd.print("CLASS: 14");
delay(1000);
}
}

The code above reads out the connection and lcd.prints on my display when I have set it to LOW + no connection was made between pin 2 & 4 (pin 2 & 4 are also untouched).

And also how do I resolve the matter and making sure I am able to read the connection properly through the if statement.

Thanks a bunch!!!

Marcuswdf:
Hi, I would like to pose a question for the Arduino reference (Arduino Reference - Arduino Reference) regarding digitalRead command as I'm quite new to these c:

May I know why is it so that when I wrote (under void setup)

Arduino uno
#include <LiquidCrystal.h>

const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LLiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//VOID SETUP//

pinMode(2, INPUT);
pinMode(4, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(4,HIGH);

//VOID LOOP//

void loop(){
if (digitalRead(2) == HIGH){
lcd.clear();
lcd.print("TYPE: GAIN");
lcd.cursor(0, 1);
lcd.print("CLASS: 14");
delay(1000);
}
}

The code above reads out the connection and lcd.prints on my display when I have set it to LOW + no connection was made between pin 2 & 4 (pin 2 & 4 are also untouched).

And also how do I resolve the matter and making sure I am able to read the connection properly through the if statement.

Thanks a bunch!!!

Where is setup() ? ? ?

ieee488:
Where is setup() ? ? ?

Don't worry I wrote it out on forums, the code runs well but my if statement doesn't even work

I have no idea what you are trying to say.

.

ieee488:
I have no idea what you are trying to say.

.

aye sorry mate, im asking why the pins 2 & 4 are not always set on HIGH or LOW when i issue out a digitalWrite ()

furthermore, I'm not sure if it's a problem with my coding and the way I wrote it.

Marcuswdf:
aye sorry mate, im asking why the pins 2 & 4 are not always set on HIGH or LOW when i issue out a digitalWrite ()

furthermore, I'm not sure if it's a problem with my coding and the way I wrote it.

It may be the way you wrote it. But, you’ve not shown how you write it!

The devil is in the details.

void setup() {

pinMode(2, INPUT);
  pinMode(4, OUTPUT);
  digitalWrite(2, LOW);
  digitalWrite(4,HIGH);
}

This code will make pin 2 a floating input. If it's not connected to anything then it will pick up stray electrical fields from the air and can read high or low at random.

Also, why are you writing to an input?

BulldogLowell:
It may be the way you wrote it. But, you’ve not shown how you write it!

The devil is in the details.

hi sir i wrote out the code, but only for this particular section i posted that i am unsure about. Sorry ;p

MorganS:
This code will make pin 2 a floating input. If it's not connected to anything then it will pick up stray electrical fields from the air and can read high or low at random.

Also, why are you writing to an input?

that might be where i went wrong.

I wrote to the input because i thought the arduino board can read inputs
by making 4 the output, it (4) can send an electrical signal to the input (2) and thus the if statement comes in to check the condition if(digitalRead(2) == HIGH) is being fulfilled.

i am still trying to understand how circuits work and yes kind sir, is there a way you can advise me on to make sure what I am planning to do works.

sorry for any confusion guys!

You will see very old Arduino code where they do sometimes write HIGH to an input. This turns on the pullup resistor built into the input circuit. The modern method is to use pinMode(pin_name, INPUT_PULLUP);

What you apear to have said is that you want pin 2 to read the status of pin 4. Is that correct?

If so, did you connect them together with a piece of wire?

MorganS:
You will see very old Arduino code where they do sometimes write HIGH to an input. This turns on the pullup resistor built into the input circuit. The modern method is to use pinMode(pin_name, INPUT_PULLUP);

ahh i seem to be getting it. thank you so much sir

GarethMoffatt:
What you apear to have said is that you want pin 2 to read the status of pin 4. Is that correct?

If so, did you connect them together with a piece of wire?

yes, i do want to read the status of pin 2. but i didnt connect it together to check if the 'if (condition)' works