How to get the status of a pin?

Hey, I've built a device with contacts and I want to know whether the contacts are together or not by testing if the voltage is high or low. Somehow I don't get any data.
Ca anyone help?

int pinPx = 24;
int x = 1;
const long intervall = 20000;
bool zeitspanne = true;
float zeit_now = millis();

void setup() {
  Serial.begin(9600);
  pinMode(pinPx, INPUT);

}

void loop() {
  zeit_now = millis();

  while (zeitspanne == true) {
    if (digitalRead(pinPx) == HIGH) {
      Serial.write(x);
      zeitspanne = false;
    }
    if (millis() > (zeit_now + intervall)) {
      zeitspanne = false;

    }
  }

  pinPx = pinPx + 4;
  x = x + 1;
  zeitspanne = true;
}

Welcome to the forum

digitalRead() will return the HIGH/LOW state of the input pin but only if the pin is wired correctly and the logic of the sketch matches the wiring

Wire the contacts to take the pin LOW when they are closed and use INPUT_PULLUP in the pinMode() for the pin to keep it HIGH when the contacts are not closed

You might want to see why what you have is doing exactly what you said:

void loop() {
  Serial.println(digitalRead(pinPx);

  delay(333);
}

Upload it and play with the input. See if you are able to see the switch return two values as you change it.

Printing is useful for seeing where you are in the flow, and what the values of variables are that inform said flow.

a7

Hey, sorry, but I don't really understand the second part that you wrote. May you explain it further?

Her are several examples of how you might wire a switch to an Arduino


Originator - @LarryD

I suggest that you use the arrangement shown at S3 and detect LOW on the pin using digitalRead() to determine that the switch is closed

I think I used S2 but i do not have an external pull up and don't know what that means. So this might be the problem.

Please post a schematic how your contacts are wired. Which Arduino are you using?

Are really all contact pins by 4 apart? And you don't intialize those pins (pinMode) in setup().

millis() returns an unsigned long. Not a float. All variables regarding millis() should be unsigned long.

This will fail after 49 days :wink:

Some comments in your code would it make clearer what you want to do ( and to check if the code really does so ).

I‘m using a MEGA 2560 and you‘ll fnd a schematic attached (I hope it‘s even helpful, have never done something like that).

Yeah, they should be 4 apart but I‘ll initiate them.

And why after 49 day??

Thank you!!

A pullup resistor is used to keep the input pin in a HIGH state when the switch is not closed and you arrange the circuit to take the in LOW by connecting it to GND when the switch is closed. The resistor can be an actual external component of a suitable value, say 10K Ohms, or more conveniently a resistor internal to the processor chip that you can turn on using INPUT_PULLUP in pinMode() for the input pin

So I don‘t need that if I don‘t want it to stay high?

Because after 49 and a bit days the value of millis() will roll over back to zero and start counting up again

However, if you do the comparison by testing whether the current value of millis() minus the timer start time exceeds the required period then rolling over to zero does not affect the calculation

The buttons in your schematic create a short circuit ( between 5V and Gnd ) when pressed.

millis() returns an unsigned long (32Bits). So after 4294967296 ms it will overflow. And that are about 49.7 days. If you use the correct unsigned math it will even then give the correct result:

if ( (millis() - zeit_now) > intervall)) {

I don't understand. If you want to check if it is high or low it must have a distinct level: HIGH or LOW. An open INPUT has no distinct level ( it's 'floating' ) and you will get arbitrary results.

I couldn't draw this, but it's connected to the ground otherwise. So it's never in the air actually...

Oh, thanks.

Got a resistor in between.

I see no resistors

"in between" where ?

You can draw this with pencil and paper and post a photo. Schematics that are not correct are useless.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.