Need Help on my Simple Project on LED!

Hi Guys!

I have this little project that will activate a single LED by using Two Pushbuttons. The scenario is that, after pressing PB1, I must wait for 5 seconds before pressing PB2 to make the LED lit.

I have ideas like using millis, however still dont know where to start.

When is our assignment due?

Expand on that.

Yes, millis is definitely way to go on this.

So you need to split this problem into smaller pieces like, how do you make sure 5 seconds has pasted since pushing PB1 button. How do you know when pressing PB2, that timeout has passed...

Next week, Monday. Like how? Can you give me a head start?

Can you give me a head start for this? Like just a simple code??

Look for Blink Without Delay in the IDE examples.
Post your schematic so we know how your hardware is wired. Hand drawn is ok.

Do you have a flowchart or pseudocode to work from?

Firstly, turn off your caps lock and edit the title of your topic.

Here your start point using bounce2 library.

//Global
unsigned long prevMillis = 0;
bool pressedOneTime = false;
#define LED_PIN LED_BUILTIN

void setup() {
}

void loop() {
  if (button1.pressed() && (pressedOneTime == false)) {
    pressedOneTime = true;
    prevMillis = millis();
  }
  
  if (pressedOneTime == true) {
    if ((millis() - prevMillis) > 5000UL) {
      if (button2.pressed()) {
        digitalWrite(LED_PIN, HIGH);
        pressedOneTime = false;
      }
    }
  }
}

Study a little bit and finish the code.

If you press pb2 too early, do you have to start over?

Is the 5 sec from when you first push the switch, or from when you let up?

How long past 5 sec will it wait? 5 more sec? Forever?

Okay, I'll check on that. So, far I have this schematic.

Noted on that about my Title.

Okay, thank you so much for this.

Like either of the two, as long as you pressed the other button, you must wait 5 seconds to press the other one. Or else the LED won't activate.

Good question. Will, after 5 seconds, the time duration is only up to 8 seconds.

That's not a schematic. All your green lines overlap each other. I can't tell what's what.

Hello kingzz21

Check, try and fine tune this Q&D sketch to your needs:

constexpr int ButtonPins[] {A0, A1};
constexpr int LedPin {9};
enum PB {One, Two};
void setup(void)
{
  for (auto ButtonPin : ButtonPins) pinMode(ButtonPin, INPUT_PULLUP);
  pinMode(LedPin, OUTPUT);
}
void loop(void)
{
  static int timerFired = LOW;
  static unsigned long stamp;
  const unsigned long duration = 5000;

  if (digitalRead(ButtonPins[One]) ? LOW : HIGH == HIGH) stamp = millis(), digitalWrite(LedPin, LOW), timerFired = LOW;

  if (millis() - stamp >= duration) timerFired = HIGH;

  digitalWrite(LedPin, digitalRead(ButtonPins[Two]) ? LOW : HIGH && timerFired);
}

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

I like thinking of these problems as states in a finite state machine:

// junk.gv -- state machine diagram
digraph finite_state_machine {

  IDLE;
  WAITING;
  LED;

  IDLE->WAITING[label="if(PB1)"];
  WAITING->LED[label="if (PB2 & time>5sec)"];
  LED -> IDLE[label = "?"]; 
  WAITING -> IDLE[label = "?"];
}

(Try it live at graphviz online)

This is still a bit unclear-- If you hold PB2 down for 5+ seconds would that count as waiting 5 sec, or would touching PB2 early make you have to start over? (like WAITING ->IDLE[label="if(PB2) && time<5sec)";

Come on guys, let's not scare king off. He needs to learn to crawl before he can run like a pro. Keep it simple. You're throwing third semester stuff at him.

I like knowing what it is supposed to do before jumping into code. From @kingzz21's description, is it clear what should happen if you hold down both buttons for 6 seconds and release them?

How do you know if the code is doing the right thing without a design?

Here's @paulpaulson's tightly packed code in wokwi:

Predict tapping the A0 button, and then immediately holding A1 for longer than 5 seconds. Then try it out and see if it works as expected, as specified, and as intended.

ETA: Try predicting holding A1 closed for 10 sec after startup.

Rotate your green buttons (select and "R" or edit the JSON "rotate").

Thank you @paulpaulson

Sorry for that, I just have that circuit. Well, they are not overlapping. They are connected on their designated I/O pins.