Push button turning on a led for certain time

I have wasted multiple days to try and come up with a code that when the button is pressed it turns on a led for 300ms and when the button is released do nothing plz help

Hello grisbar

Welcome to the worldbest Arduino forum ever.

Post your sketch, well formated, with well-tempered comments and in so called code tags "< code >" and schematic to see how we can help.

Have a nice day and enjoy coding in C++.

This is I think the closest I been to what I want to achieve but it doesn't work

int ledState = LOW;
unsigned long previousMillis = 0;  //will store last time LED was blinked
const long period = 500;         // period at which to blink in ms

void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  pinMode(3,INPUT_PULLUP);
}

void loop() {
  int sensorVal = digitalRead(3);
  // put your main code here, to run repeatedly:

 unsigned long currentMillis = millis(); // store the current time
if (sensorVal == LOW){
  if (currentMillis - previousMillis >= period) { // check if 1000ms passed
   previousMillis = currentMillis;   // save the last time you blinked the LED
   if (sensorVal == LOW){
digitalWrite(13,HIGH);
 } else {
ledState = LOW;
}

 }
}
}

Hello grisbar

Consider these mods of your sketch:

unsigned long previousMillis = 0;  //will store last time LED was blinked
const long period = 500;         // period at which to blink in ms
constexpr uint8_t SensorPin {3};
constexpr uint8_t LedPin {13};

void setup()
{
  // put your setup code here, to run once:
  pinMode(LedPin, OUTPUT);
  pinMode(SensorPin, INPUT_PULLUP);
}
void loop()
// put your main code here, to run repeatedly:
{
  unsigned long currentMillis = millis(); // store the current time
  int sensorVal = digitalRead(SensorPin);
  if (sensorVal == LOW)
  {
    previousMillis = currentMillis;
    digitalWrite(LedPin, HIGH);
  }
  if (currentMillis - previousMillis >= period && digitalRead(LedPin) == HIGH)
  {
    digitalWrite(LedPin, LOW);
  }
}

Have a nice day and enjoy coding in C++.

Thanks for the rapid response
But I want the time the led is on to start as soon as I press down the button

This does...

// https://forum.arduino.cc/t/push-button-turning-on-a-led-for-certain-time/1147387
// https://wokwi.com/projects/370088234566127617

#define buttonPin 2
#define ledPin 3

bool ledState;

unsigned long ledOnMillis;
unsigned long ledOffMillis;
unsigned long durationMillis = 500;

void setup() {
  Serial.begin(115200);
  pinMode (ledPin, OUTPUT);
  pinMode (buttonPin, INPUT_PULLUP);
  digitalWrite(ledPin, LOW);
}

void loop() {

  if (!digitalRead(buttonPin)) { // test button
    ledOnMillis = millis();
    digitalWrite(ledPin, HIGH);  // LED ON
    ledState = 1; // set LED flag
    delay(150); // debounce the button press
    Serial.print("TIME ON  ");
    Serial.println(ledOnMillis);
  }

  if ((millis() - ledOnMillis > durationMillis) && ledState) { // difference between button to now
    digitalWrite(ledPin, LOW); // LED OFF
    ledOffMillis = millis();
    ledState = 0; // clear LED flag
    Serial.print("TIME OFF ");
    Serial.println(ledOffMillis);
    Serial.print("DURATION ");
    Serial.println(ledOffMillis - ledOnMillis); // show duration
  }
}

Remove the Serial.print() statements to make the timing more accurate.

Hello grisbar

Be more specific about what the task of the sketch is.

Well I asked a friend for any advice and he said to just ask the chat gpt bot and I did so . It made mistakes 5 times but at the end I told him to use the edge state Library and it sent me what I wanted

#include <Arduino.h>

const int buttonPin = 3;   // The pin where the push button is connected
const int ledPin = 4;      // The pin where the LED is connected

int buttonState = 0;       // Variable to store the state of the button
int lastButtonState = 0;   // Variable to store the previous state of the button
bool ledOn = false;        // Flag to indicate whether the LED is currently on
unsigned long previousMillis = 0;   // Variable to store the previous time

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);  // Set the button pin as INPUT
with internal pull-up resistor
  pinMode(ledPin, OUTPUT);            // Set the LED pin as OUTPUT
}

void loop() {
  // Read the state of the button
  buttonState = digitalRead(buttonPin);

  // Check for button press event
  if (buttonState != lastButtonState && buttonState == LOW) {
    // Button is pressed, toggle the LED and set the previous time
    ledOn = !ledOn;
    digitalWrite(ledPin, ledOn ? HIGH : LOW);
    previousMillis = millis();
  }

  // Check if the LED is on and if 300 milliseconds have passed
  if (ledOn && (millis() - previousMillis >= 300)) {
    // Turn off the LED
    ledOn = false;
    digitalWrite(ledPin, LOW);
  }

  // Store the current button state for the next iteration
  lastButtonState = buttonState;
}

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