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;
}
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
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
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:
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.