is "if(digitalRead(2) == HIGH)" an unreliable way of going about it?

I'm going to have a sensor go off which will trigger an interrupt in my code. I'm doing this with

if(digitalRead(2) == HIGH)

but it seems to be going off randomly. Goes off with me touching the board, goes off with it sitting on the couch next to me, goes off if i'm holding it in the air by the usb cable.

Nothing is plugged into pin 2.

It also goes off if i supply 3.3v to pin 2 (the only time it should go off)

What gives? im not sure it does it when sitting on top of my laptop, so, maybe i'm jsut getting static electricity?

Thank you

Show us how your device is wired. (Not a Fritzing diagram please)
Pic of pen on paper is fine - posted inline with the 'insert image' button.
Accomnpanied by the code you're using. (posted woth </> code tags)

It sounds like your button is not being biased to the unpressed state - usually with
pinMode( pin, INPUT_PULLUP);

Once you get this working - read up on state-change examples.
That will be your next question!

unsigned long interval=99999000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
unsigned long startMillis = millis(); // grab current time
//bool ledState = false; // state variable for the LED
 
void setup() {
 Serial.begin(9600);        // Initialize serial communications with the PC
 pinMode(3, OUTPUT);
 pinMode(2, INPUT);
 digitalWrite(3, LOW);
}
 
void loop() {
  
// start lowering gate code

//if((unsigned long)(millis() - startMillis) < interval){
    while((unsigned long)(millis() - startMillis) <= interval){// check if "interval" time has passed (5000 milliseconds)
      if(digitalRead(2) == LOW){
        lowergate();
      }else if(digitalRead(2) == HIGH){
        raisegate();
      }else{
      digitalWrite(3,HIGH);
      while(1){}
    }
  }
  Serial.println("gate lowered");
  while(1){}
}

void lowergate(){
//  Serial.println("startmills is: " + startMillis)
Serial.println((unsigned long)(millis() - startMillis));
  Serial.println("lowering the gate");
  digitalWrite(3,HIGH);//change led state. these 3 lines simulate gate lifting code
//  ledState = !ledState;// "toggles" the state
  delay(100);//only to make led flash, wont be in final version
  digitalWrite(3,LOW);
  delay(100);
}

void raisegate(){
  Serial.println("sensor triggered raising gate");
  digitalWrite(3, LOW);
  while(1){}
}
pinMode(2, INPUT);

Should probably be INPUT_PULLUP

if(digitalRead(2) == LOW){
        lowergate();
      }else if(digitalRead(2) == HIGH){
        raisegate();
      }else{
      digitalWrite(3,HIGH);
      while(1){}
    }

Bear with me...
How will it ever get to the third possibility ?

if(digitalRead(2) == LOW){
        lowergate();
      }else if(digitalRead(2) == HIGH){
        raisegate();
      }else{
      digitalWrite(3,HIGH);
      while(1){}
    }

The else if is meaningless, along with the extra else

Your photo is meaningless.

3rd option needs to be removed

Not sure image will work

3rd option needs to be removed

Not sure image will work

Well, you did something right in Reply #2

bad link on phone.

OK, let's start again
Show us how your device is wired.
Pic of pen on paper is fine

OK - I give up birddbrain.
Reply #1 had everything you needed - to address the question..
PLUS

(Not a Fritzing diagram please)

There is no switch shown in your photo or (exactly like a Fritzy) diagram
There is no RFID module mentioned in your sketch

I'll keep listening - and maybe when you do too - I'll chip i again.

I don't know why you're having an attitude. You wanted a picture of How It's wired up. That is a picture of How It's wired up.

There's no switch in the picture because there's no switch on the Arduino. Why would I put a switch the picture that's not actually on the device?

And no there's no code for the card reader in the sketch. I'm not sure why this is an issue, I'm not asking a question about the RFID reader I'm asking a question about pin number 2.

But since you're resorting to childish name-calling that I'm not really interested in continuing. There's a hundred thousand other people out there willing to help people.

OK, I'll bite -

There's no switch in the picture because there's no switch on the Arduino. Why would I put a switch the picture that's not actually on the device?

Your entire thread is about how to handle a sensor on pin 2.
My answers specifically addressed that question in Reply #1
-- I acknowlege a sensor could be more complex than a simple switch - but in this case it's output can only be ON or OFF - a switch.
If you have another question - go for it in a new thread.
I would be happy to help if there is a new question, or my earlier comments didn't work.

EDIT: Added
Maybe I'm a bit harsh - but birdseed could improve communications.
So far we have a piece of code that is not actually going to be used for a digital input device that doesn't exist - in a project that is still in early development. The photos/diagram have no semblance to the question - so we're just a bit out of our depth to answer something that hasn't been asked yet.

In your diagram there is nothing connected to pin 2 of the Arduino.

you're using millis() and then you have delay() in your lowergate function
not good
delay() delays, freezes, millis()

That was just so I could see some sort of physical feedback. That delay won't be needed when I integrate this into to my main sketch. I will also be doing some more reading on the pull up thing when my mind is less tired

Hi,

In your code you are reading pin 2, when you have nothing connected to it?

Leaving an input pin open like that, does not mean it will be read as a zero. The pin is high impedance and will be picking up digital noise from your UNO.

Tom... :slight_smile:

I will also be doing some more reading on the pull up thing when my mind is less tired

When you do then look at this:- http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Your whole problem stems from you not having anything connected to an input pin you are trying to read. With nothing connected to an input pin it can read anything because it receives interference. The solution is to connect something to it, or as others have said use pinMode( 2,INPUT_PULLUP )