Arduino uno Input problem

Hi, I'm trying to make a program that check's whether pin 2 is HIGH or LOW and displays a corresponding message on an LCD screen, the problem is that every time it returns the negative message. `#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(3, OUTPUT);

lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Loading");
delay(600);
lcd.setCursor(7,0);
lcd.print(".");
delay(600);
lcd.setCursor(8,0);
lcd.print(".");
delay(600);
lcd.setCursor(9,0);
lcd.print(".");
delay(600);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Loading");
delay(600);
lcd.setCursor(7,0);
lcd.print(".");
delay(600);
lcd.setCursor(8,0);
lcd.print(".");
delay(600);
lcd.setCursor(9,0);
lcd.print(".");
delay(600);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Loading");
delay(600);
lcd.setCursor(7,0);
lcd.print(".");
delay(600);
lcd.setCursor(8,0);
lcd.print(".");
delay(600);
lcd.setCursor(9,0);
lcd.print(".");
delay(600);
lcd.clear();

lcd.setCursor(0,0);
lcd.print("Tank Status");
delay(600);
lcd.setCursor(11,0);
lcd.print(".");
delay(600);
lcd.setCursor(12,0);
lcd.print(".");
delay(600);
lcd.setCursor(13,0);
lcd.print(".");
delay(600);
lcd.clear();

lcd.setCursor(0,0);
lcd.print("Tank Status");
delay(600);
lcd.setCursor(11,0);
lcd.print(".");
delay(600);
lcd.setCursor(12,0);
lcd.print(".");
delay(600);
lcd.setCursor(13,0);
lcd.print(".");
delay(600);
lcd.clear();

lcd.setCursor(0,0);
lcd.print("Tank Status");
delay(600);
lcd.setCursor(11,0);
lcd.print(".");
delay(600);
lcd.setCursor(12,0);
lcd.print(".");
delay(600);
lcd.setCursor(13,0);
lcd.print(".");
delay(600);
lcd.clear();

if(2 == HIGH){ //checks tank seal
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Seal check pass");
delay(5000);
lcd.clear();
}

else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Seal check fail");
delay(800);
lcd.setCursor(0,1);
lcd.print("Re-insert tank");
delay(8000);
lcd.clear();
}

}

void loop() {
// put your main code here, to run repeatedly:

}`

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

  if (2 == HIGH) //checks tank seal

2 will always be HIGH

Did you mean to use digitalRead(2) to read its state ?

Thanks, that fixed it. Now I feel like an idiot thou :confused:

What is connected to pin 2, and how? Schematics please....

Never mind, now it does the opposite, it only shows the positive outcome.

if(digitalRead(2) == HIGH){ //checks tank seal
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Seal check pass");
delay(5000);
lcd.clear();
}

else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Seal check fail");
delay(800);
lcd.setCursor(0,1);
lcd.print("Re-insert tank");
delay(8000);
lcd.clear();
}

just 5V or nothing. Simple "is this connected check".

I say the same and turns to other matters.

So when pin 2 is not connected to 5V it is floating at an unknown state, maybe HIGH, maybe LOW

Consider using INPUT_PULLUP in pinMode() to turn on the pullup resistor and change the circuit to take the pin LOW when the input is active and change the program logic to match. Either that or add a 10K pulldown resistor to keep pin 2 LOW when not active

Yeah, you are the very first person to ever make that mistake. :wink:

a7

I think you mean "2 == HIGH" will always be false. HIGH is 1 and "2 == 1" is false.