Danois90:
Consider this nice guide in the top of the forum instead!
I have already looked at that before and thought I understood it, but it did help to go back and see it again.
I think I was just missing the callout to set "currentMillis = millis()" and "buttonState=digitalRead(buttonPin)" at the end of my "While" loop.
It appears to be working as expected after adding those. I'll add the code below for anyone seeing this in the future.
Now I just need to figure out how to swap the input button with an NPN proximity switch! I will do some research.
// These constants won't change:
const int buttonPin = 10; // the pin that the pushbutton is attached to
const int maxRun = 2000; // the maximum time the LED will be lit
const int ledPin = 13; // the pin that the LED is attached to
// These variables will change:
int buttonState = LOW; // set current state of the button
int lastButtonState = LOW; // set previous state of the button
unsigned long timeStamp = 0; // the clock that will count how long since button press
unsigned long currentMillis;
// Setup the functions of the pins we will use:
void setup() {
// initialize the button pin as an input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin); //Read the current state of the button
// compare the buttonState to its previous state
if ((buttonState != lastButtonState) && (buttonState == HIGH)) { // if the current state is HIGH then the button went from off to on:
delay(10); // delay a little to debounce
timeStamp = millis();
currentMillis = millis();
while ((buttonState == HIGH) && (currentMillis - timeStamp <= maxRun)) {
digitalWrite(ledPin, HIGH);
currentMillis = millis();
buttonState = digitalRead(buttonPin); //Read the current state of the button
}
}
else {
digitalWrite(ledPin, LOW);
}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
delay(10); // delay a little to avoid bouncing
}