On/off button for code

Post has been modified

I've added an on and off button to my project and I'm having some difficulties in customizing the on and off button for the code
Example
ButtonPin1 is for turning on the LEDs
ButtonPin2 is to turn the code on and off
So that ButtonPin1 will not work unless you press ButtonPin2 and another click on ButtonPin2 stops ButtonPin1 again and so on..
Now I want ButtonPin2 to run the code when clicking on it three clicks instead of one and the third click is prolonged for two seconds and stop I want it as it is with only one click

Code

int ButtonPin1=2;
int ButtonPin2=4;

int LED1=12;
int LED2=11;
int LED3=10;

int count=0;

void setup() {
pinMode(ButtonPin1, INPUT_PULLUP);
pinMode(ButtonPin2, INPUT_PULLUP);

pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);

}

void loop() {
if(digitalRead(ButtonPin2)==LOW) {
delay(1);
while(digitalRead(ButtonPin2)==LOW);
count++;
if(count==2) count=0;
}
if (count==true){

if (digitalRead(ButtonPin1) == LOW){
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 digitalWrite(LED3, HIGH);
 delay(5000);

}
else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}

}

}

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

1 Like

I am facing a problem in inserting the code, the usual way, because I am new to the forum. Is there a solution to this problem.. Thank you

Losing the delays will help you out.
However, that means rewriting your code to use millis() timing.

1 Like

The problem is not occurring because you are new to the forum. The problem does not affect all new forum members, but it does affect many. The new members that are not affected by the problem with posting code are the few that read the forum guide before they post.

1 Like

Debouncing Switches in Arduino covers reading switches without blocking.
How to write Timers and Delays in Arduino covers getting rid of delays and using a timer to check the long press
and Multi-tasking in Arduino covers restructuring your sketch into tasks that can use millis() for timing.
multitaskingDiagramSmall

1 Like

I'm still a beginner in Arduino programming and I'm missing a lot, I will write down your instructions, thank you

Hello
add comments to your sketch to describe the desired functionality.
Use real names for variables. The variable names M1 to M6 do not help, which button is the start- and stop button?

1 Like

Thank you for letting me know, I will re-post in a simpler way .

The post has been modified to simplify the idea

Making extensive changes to a post that has already been commented on is not good manners and I note that you still did not use code tags to make the code easier to read and to copy with a single click

Your duplicate topic on the same subject has been deleted

Why did you create a new topic ?

I created a new topic because I just got permission to upload the code to facilitate the process of downloading or copying the code.. Please do not be blamed, as my ability to use the Internet is weak.

The easiest way to post code is to right click in the IDE and select "Copy for forum" then paste the clipboard into a post here. The code tags will be automatically added by the IDE

1 Like

Thank you for this information.. I really didn't know about it.. Thank you.. It will help me a lot for the next time

Then you did not read How to get the best out of this forum that was linked to in an earlier reply

In fact, I didn't read it, I was busy with other replies, I will start reading it now

enable code after every 3 button2 presses

#undef MyHW
#ifdef MyHW
int ButtonPin1 = A1;
int ButtonPin2 = A2;

#else
int ButtonPin1=2;
int ButtonPin2=4;
#endif

byte ledPins [] = { 10, 11, 12 };


int count=1;
byte but2Lst = HIGH;

void setup() {
    pinMode(ButtonPin1, INPUT_PULLUP);
    pinMode(ButtonPin2, INPUT_PULLUP);
    but2Lst = digitalRead (ButtonPin2);

    for (unsigned n = 0; n < sizeof(ledPins); n++)  {
        pinMode (ledPins [n], OUTPUT);
        digitalWrite (ledPins [n], LOW);
    }
}
void loop() {
    byte but = digitalRead(ButtonPin2);
    if(but2Lst != but)  {
        but2Lst = but;
        if (LOW == but)
            count++;
    }
    if (! (count % 3))  {
        if (digitalRead(ButtonPin1) == LOW){
            for (unsigned n = 0; n < sizeof(ledPins); n++)
                digitalWrite (ledPins [n], HIGH);
            delay(5000);
        }
        else {
            for (unsigned n = 0; n < sizeof(ledPins); n++)
                digitalWrite (ledPins [n], LOW);
        }
    }
}
1 Like

It's very kind of you to reformulate my code.. I will learn a lot from this new code.. I am grateful to you.. Thank you