Arduino polling sensor for staying high 100ms

Hello everyone,

I am using a Arduino Mega to read some reed switches. Sometimes the signal is floating for a short time, like 1~10 ms. The problem is the long wiring between the mega and the sensor (10meters). For now there is no better solution for the long wiring.

The solution must be in the code. I want to work with millis, because the program have other things to do in meanwhile. So if the signal stays high for example 100ms than happens something. if once in the 100ms the signals go to low, nothing happens. and the program start over with this 'polling'.

I was looking for any solution on the internet. The only tutorials are with buttons, so its necessary to release the button. My sensor stays high when some must go happen.

Can give someone me a little help with a basic code? Thanks in advance :)

Grtz, Niels

So if the signal stays high for example 100ms than happens something. if once in the 100ms the signals go to low, nothing happens. and the program start over with this 'polling'.

You need to determine when the state changes to HIGH and when it changes to LOW. The state change detection example shows how to do that. When the state changes happen, you need to record when, using millis().

Periodically, you see if the state IS HIGH. If it is, you measure how long (now - then), and, if that is too long, do the magic something.

The state changing to LOW simply clears the time that the state changed to HIGH.

Thanks for your help Paul.

I made this and it's working. But sometimes the millis value is switching from positive to negative. So value is 17350 and after a next press its -17350.

Can someone tell me how to fix this? Thanks :)

// this constant won't change:
const int  buttonPin = A0;
const int ledPin = 13;
const int wait = 250;

// Variables will change:
int buttonState = 0;
int buttonn;
int lastButtonState = 0;
int previousMillis = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  buttonState = digitalRead(buttonPin);
  buttonn = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    
    if (buttonState == LOW) {
      previousMillis = millis();
      Serial.println("on");
      Serial.println(previousMillis);
    } 
    else {
      Serial.println("off");
      previousMillis = 0;
      digitalWrite(ledPin, LOW);
    }
  }
  
  if (buttonn == LOW) {
    if (millis() - previousMillis >= wait) {
        Serial.println("Magic happens");
        digitalWrite(ledPin, HIGH);
      }
  }
  
  lastButtonState = buttonState;
  delay(50); // Nice to read serial monitor.

}

Because you are using an int for previousMillis.

Make it an unsigned long.

Thanks! Script works!

const int buttonPin = A0;
const int wait = 250;

int Button = 0;
int lastButtonState = 0;
unsigned long previousButton = 0;

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

void loop() {

  Button = digitalRead(buttonPin);

  if (Button != lastButtonState) {
    if (Button == LOW) {
      previousButton = millis();
    } 
    else {
      previousButton = 0;
    }
  }
  
  if (Button == LOW) {
    if (millis() - previousButton >= wait) {
      Serial.println("HOI");
      }
  }

  lastButtonState = Button;
  delay(50); // Nice to read serial monitor.

}

Sometimes the signal is floating for a short time, like 1~10 ms

Don’t let it. Use INPUT_PULLUP in the pinMode() for the input