help with code

Hi everybody!! thanks for your patience!! I´m a newbie in programming the arduino and I have a doubt with the code I´m writing and got stucked…

I have a button connected at pin A0 and two relays connected to two digital ouputs (12 and 13). what I want to do is that if I press the button for the first time, the output at pin 12 must go high, wait for some miliseconds and then go down (that´s the easy part).

here´s come the problem I have… now, if a press the same button for the second in an interval of 5 seconds after it had been pressed before, now pin 13 must go high, delay and the go down.

Resuming… I press the button one time and have one output, I press it again within a 5 second period, the other output must go high.

I´ve tried using the millis() function, but I can´t get it to work

I wrote another code to count the button presses in a period of time, but it doesn´t work as I expect…

Here´s the last thing I wrote

const int BOTON=A0;
const int LED=LED_BUILTIN;
int val;
int cnt;

void setup() {
pinMode(BOTON,INPUT);
pinMode(LED,OUTPUT);
Serial.begin(9600);

}

void loop() {
val=digitalRead(BOTON);
if (val==HIGH){
unsigned long starttime=millis();
cuenta=1;
while(millis()-starttime<10000){
if(val==HIGH){
cnt=cnt+1;
delay(10);
Serial.println(cnt);
}
}
}
}

Can any of you help to resolve this? I need to finish this in order to build everything…

thank you!!!

First, it would help us if you reformatted your code before you post it using Ctrl-T when the cursor is in the IDE’s source code window. Second, please read the posts at the top of this Forum, especially the one that says “Read This Before Posting…”. Third, those posts will tell you how to use code tag(i.e., the ‘</>’ symbol you see above) for posting your code. Fourth, if the first if (val == HIGH) is true but you don’t call digitalRead() to reassign val again, how can the second if expression within the while loop not be true?

  while (millis() - starttime < 10000)
    {
      if (val == HIGH)
      {
        cnt = cnt + 1;
        delay(10);
        Serial.println(cnt);
      }
    }

The program will not enter this while loop unless val is HIGH. It’s value is not changed in the while loop so why test its value again ?

Do you have a pulldown resistor on the input or is it floating at an uncertain (possibly HIGH) voltage when the button is not pressed ?

I have a pull down resistor

cmdima:
I have a pull down resistor

Tha'ts good news.

How about my other question ?

what I want to do is that when I press the button for the first time do something and if I press the button again in the following 5 seconds do other thing.

that´s why I´m using while and the millis...

that´s why I´m using while and the millis...

But why are you testing val which you know must be HIGH inside the while loop ?

Incidentally, using millis() inside a while() for timing is a bad way to do it.

I can see that... I´m thinking of using a for instruction with 1 ms delay...

I´m thinking of using a for instruction with 1 ms delay...

Stop thinking nasty thoughts and do it the right way. Let loop() run freely and check check time through loop() how much time has elapsed.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Thank you very much for the information and your help!! I solved it with the millis. I did some tests and worked!!