Can someone help me with this?!

Can someone help me whith a code?
I need to make an led to stay on for 20 sec after you push a button, and after you push 5 times activate another led and if you push another time reset the process.

What have you managed so far?

Have you read this;

Useful things: code, schematic, pictures, components links

Your requirements described in English above seem trivial but are vague and open to interpretation. Code requires things to be exact and will not interpret meaning. Eg do you want it to do x only if pressed 5 times in a certain timeframe or just when the number of button presses has reached 5 no matter how long that takes. Code will do the later unless specified

that's what I managed to do so far

<int counter = 0;

void setup(){
pinMode(4, INPUT_PULLUP);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
Serial.begin(9600);

}

void loop()
{
if (counter == 0){
digitalWrite(13,LOW);
digitalWrite(12,LOW);
}
else if(counter == 1){
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}else if(counter == 2){
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}else if(counter == 3){
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}else if(counter == 4){
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}
else if(counter == 5){
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}
else if(counter == 6){
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
}
int buttonReading = digitalRead(4);
if(buttonReading==LOW){
counter++;}
counter %= 7;

Serial.println(counter);
delay(300);
}>

Edit your post :pencil2:
Put your code in code tags <|>
Read this;

If you look at any example sketches they avoid using magic numbers and instead tend to name a pin after its function such as buttonPin = 4

This makes it easy to read

You could use switch case or a less repetitive state machine

Describe what your code does and how this varies from what you want

At the moment this code only keeps my led lit for the first 5 presses, I need the led to be off and after pressing a button to stay lit for 20 seconds, after 5 presses in which it was lit for 20 seconds , another led should light up

See #4...........

As you say (although maybe not as you mean),
Just look up millis timing in the blink without delay example in the IDE. That will show you how to turn on for any set time.

Your state machine is basically using your counter when that is not the number of states you actually have. You use your counter to progress into your final state but it is not necessary for the other states.

Look up state machines and draw a diagram of your states.

And edit your code as per my previous post

States;
State 0: waiting
State 1: LED1 on for 20 sec
State 2: LED2 on for 20 sec

Conditionals;
If buttonPressed state = 1
If counter = 5 state = 2
If counter = 5 and button pressed state = 0, counter = 0

Hi, @krom97
Welcome to the forum.
What Arduino controller are you using?

Can you please post a schematic of your project?
Please do not use Fritzy, a hand drawn image will be fine.
Include ALL power supplies, component names and pin labels.

This is to see how you have configured your hardware, in particular the button.

Please note..
To add code please click this link;

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

What happens if the button is pushed before the 20 seconds is up? If the button is ignored for the 20 seconds, that's fairly easy:

int counter = 0;

const byte ButtonPin = 4;
const byte LED1Pin = 13;
const byte LED2Pin = 12;

void setup()
{
  pinMode(4, INPUT_PULLUP);
  pinMode(LED1Pin, OUTPUT);
  pinMode(LED2Pin, OUTPUT);
  Serial.begin(9600);

  digitalWrite(LED1Pin, LOW);
  digitalWrite(LED2Pin, LOW);
}

void loop()
{
  if (digitalRead(ButtonPin) == LOW)
  {
    counter++;

    if (counter >= 1 && counter <= 5)
    {
      // Turn on one LED for 20 seconds
      digitalWrite(LED1Pin, HIGH);
      delay(20000);
      digitalWrite(LED1Pin, LOW);
    }
    else if (counter == 6)
    {
      digitalWrite(LED2Pin, HIGH);
    }
    else if (counter == 7)
    {
      digitalWrite(LED2Pin, LOW);
      counter = 0;
    }
  }

  Serial.println(counter);
  delay(300);
}

Is ignored for 20 seconds
Thank you for help!

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