Blinking mode change button

Hello
I have this simple code that contains multiple modes for blinking of the LED .. Where the button No. 1 is to change the mode of blinking and the button No. 2 is to activate the blinking mode .
Now I want to remove the button No. 2 to turn on the mode directly without a button .
But the problem is when I remove button number 2, I can't change the mode at all.
Is there a solution?

int LED1=5; 
int LED2=6;

int ButtonPin1=2; // Blinking mode change button
int ButtonPin2=3; // Blinking start button

int dt=200; // delay
int mode=1; // Blinking mode state when the arduino is turned on

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(ButtonPin1, INPUT_PULLUP);
  pinMode(ButtonPin2, INPUT_PULLUP);    
}

void loop()
{ 
 if(digitalRead(ButtonPin1)==LOW) {
   while(digitalRead(ButtonPin1)==LOW);
     mode++;
      if(mode==4) mode=1;
  }
 if  (mode==1){
 if (digitalRead(ButtonPin2) == LOW){
  analogWrite(LED1,25);
  delay(dt);
  analogWrite(LED1,50);
  delay(dt);
  analogWrite(LED1,75);
  delay(dt);
  analogWrite(LED1,100);
  delay(dt);
  analogWrite(LED1,125);
  delay(dt);
  analogWrite(LED1,150);
  delay(dt);
  analogWrite(LED1,175);
  delay(dt);
  analogWrite(LED1,200);
  delay(dt);
  analogWrite(LED1,225);
  delay(dt);
  analogWrite(LED1,250);
  delay(2000);
  digitalWrite(LED1, 0);
 }
 }
  if  (mode==2){ 
  if (digitalRead(ButtonPin2) == LOW){   
digitalWrite(LED2, HIGH);
delay(200);
digitalWrite(LED2, LOW);
delay(400);
digitalWrite(LED2, HIGH);
delay(200);
digitalWrite(LED2, LOW);
} 
}
  if  (mode==3){ 
  if (digitalRead(ButtonPin2) == LOW){   
digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED2, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(400);
digitalWrite(LED2, LOW);
delay(200);
digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED2, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(400);
digitalWrite(LED2, LOW);
} 
}  
}

Show your code with only one button. Doesn’t sound too complicated

Tips to make your code work;
Study “blink without delay” delay and buttons don’t go well together.
Study state machines

How do I do that?
I'm a beginner

You search for, then you try understanding, then write some simple sketches to see if you can get things to work.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay


Try to get your head into the idea that loop does exactly what it says and it is supposed to do this. Don’t think linear, think loop. In linear you have coded like this: turn on Walt a bit, turn off, wait a bit, turn on etc. This is called blocking code because you are expecting your program to do one thing at a time in a linear way from start to finish and delay is a happy shortcut to get the timing you want. The program pauses with each delay and literally waits a human timescale before moving on when it is designed to work at machine timescales faster than a human can imagine

The problem is that, in order for your code to be able to appear to do multiple things at once it works by looping over and over again very fast. It only takes a millisecond or two to get round and back again. In this way blinking an led is not linear. You check the led each time through loop and see how long it has been on for. If this time exceeds the delayTime then it toggles the led to off etc. Each time through loop it checks and once every hundred or thousand times it toggles as required. The great thing is that each time through loop it also checks your button to see if it has been pressed and a myriad of other things you have coded.

This is probably the most fundamental concept of coding for arduino and really this is what determines what your code should look like.

2 Likes

Thanks for the enriching notes you made for me.. I will try to analyze and benefit from these notes

I have copied that - I may use it again if you are not around to answer. OK? :face_with_raised_eyebrow:

1 Like

No problem

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