Timed physical button

Good night friends
i’m setting up another project, now with arduino uno.
I already got a part of what I wanted, a slider for a led strip and a blynk button to turn off.
Now comes the part I can’t get:
A physical button to turn on the same led strip, but a button that lasts for about 30 seconds.
For example, I open the door of the house and this light turns on for 30 seconds and then goes out.

This is the code I already use:


#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>


char auth[] = "xxxxxx";

#define mosfetPin 6
#define botao 4

BlynkTimer timer;
//int t1;

// Toggle LED
void ledBlynk()
{
  digitalWrite(mosfetPin, !digitalRead(mosfetPin));
}

BLYNK_WRITE(V0) {  

    //Blynk.syncVirtual(V1);
    int value = param.asInt();
    analogWrite(mosfetPin, value);
    if (value == 0) { 
        Blynk.virtualWrite(V1,0);
        Blynk.setProperty(V1,"offLabel","OFF");
    }else {
        Blynk.virtualWrite(V1,1);
        Blynk.setProperty(V1,"onLabel","ON");
    }
}

// BUTTON
BLYNK_WRITE(V1) {
    int value = param.asInt();
    digitalWrite(mosfetPin,value);
    if (value==0) {
        
        // change button to off
        Blynk.virtualWrite(V0,0);
    }else{        
        // change button to on
        
        Blynk.virtualWrite(V0,255);
    }
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth);
 
   // Configure LED and timer
  pinMode(mosfetPin, OUTPUT);
  pinMode(botao, INPUT);
//  t1 = timer.setInterval(500L, ledBlynk);
 // timer.disable(t1);
}

void loop()
{
  Blynk.run();
    timer.run();
}

You have two choices with the button pin - either…

  • Check it regularly to see if it has been pressed, using a timed function that polls the pin every 100ms or so

  • Attach an interrupt to the pin. This will trigger a function when the button is pressed.

Once you’ve detected the button press then use either a lambda timer or a regular timeout timer process to turn off your lights after the appropriate interval.

Pete.

that’s the problem, I don’t know where I start

Well, I’ve given you some pointers, start by researching the concepts I’ve discussed.

I guess that the real issue is a lack of C++ coding skills? The best way to learn is by trial and error, building your knowledge and confidence along the way.
Start by creating a timer (like the one that’s commented-out in your code) that calls a function which uses a digitalRead command to get the status of a pin. Print the result to the serial monitor and observe what happens when you connect that pin to GND and then to VCC.

Pete.

a little boost. Find the library Countimer_Master.
Set the count down timer for 30min.
Start the timer when the switch is made.
For the timer end function set the pin low.

I use this for my irrigation set to 20min per area.