How can I break the loop although it haven't finish 10 seconds?

Dear all,

Currently I am using OneButton.h library (GitHub - mathertel/OneButton: An Arduino library for using a single button for multiple purpose input.) to code my push button project.

When I press button1, it will loop for 10sec and print TRIG, and after that it will break the loop.

However, now I want to program it where when I pressed button1, it will jump to void(singleclick) function, but right now, during the 4th second, I pressed other button, thus Arduino will break the 10sec loop and run other function.

Thus, I need help how to make sure Arduino actively read other button and when pressed, it will break the current loop/function to other related functions.

#include <OneButton.h>

OneButton button1(2, true);
OneButton button2(3, true);
OneButton button3(4, true);
OneButton button4(5, true);

int led1=12;
int led2=11;
int led3=10;
int led4=9;

int flag;

unsigned long mode;

void setup()
{

  • Serial.begin(9600);*

  • pinMode(led1,OUTPUT);*

  • pinMode(led2,OUTPUT);*

  • pinMode(led3,OUTPUT);*

  • pinMode(led4,OUTPUT);*

  • button1.attachClick(singleclick1);*

  • button1.attachLongPressStop(longclick);*

  • button2.attachClick(singleclick2);*

  • button3.attachClick(singleclick3);*

  • button4.attachClick(singleclick4);*

  • digitalWrite(led1,HIGH);*

  • digitalWrite(led2,HIGH);*

  • digitalWrite(led3,HIGH);*

  • digitalWrite(led4,HIGH);*

}

void loop()
{

  • button1.tick();*

  • delay(20);*

  • button2.tick();*

  • delay(20);*

  • button3.tick();*

  • delay(20);*

  • button4.tick();*

  • delay(20);*

  • Serial.println(“No button”);*

}

void singleclick1()
{

  • mode=millis();*

  • flag=1;*

  • while(flag==1)*

  • {*

  • Serial.println(“button1”);*

  • digitalWrite(led1,HIGH);*

  • digitalWrite(led2,LOW);*

  • digitalWrite(led3,LOW);*

  • digitalWrite(led4,LOW);*

  • if(millis()-mode>10000)*

  • {*

  • Serial.println(“TRIG”);*

  • flag=0;*

  • button2.tick();*

  • delay(20);*

  • button3.tick();*

  • delay(20);*

  • button4.tick();*

  • delay(20);*

  • return;*

  • }*

  • }*
    }

void singleclick2()
{

  • Serial.println(“button2”);*
  • digitalWrite(led1,LOW);*
  • digitalWrite(led2,HIGH);*
  • digitalWrite(led3,LOW);*
  • digitalWrite(led4,LOW);*
    }

You have to put the software-millis-timer in the loop().

There should be just one place where you call the .tick() functions.

void loop()
{
  button1.tick();
  button2.tick();
  button3.tick();
  button4.tick();
}

Do not use a while-loop to wait for something.
Keep in mind that the loop() runs over and over again. The more and faster the loop() runs, the more responsive the sketch will be.
Do something in the loop() that needs to be done, and get out of there as quick as possible.

A 'bool' variable can be used to turn on the millis-timer. For example 'enableTimer'.
Put the millis-timer in the loop(), and control it with the 'enableTimer'.
Can you give the variable 'mode' an other name please ? For example 'previousMillis'.

See my millis_single_delay.ino how the bool 'enabled' is used to turn on and off the millis-timer.

OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you later need to post that) from the IDE and click the icon.

In fact, the IDE itself has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. While you were lucky so far, If you do not post it as “code” it can easily be quite garbled and is generally more difficult to read due to the font.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

Why do we think this is more important than just having your question answered? Because it is really difficult to write code properly - as with any other task requiring care - if everything is not well organised!

State machines are the key to doing several things at once.

Also see Nick G's tutorial about doing several things at once: Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

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