Stop void fonction

Asked by At

I would like to stop LEDS blinking when I push a second time on the button. And restart if I push again.

I mean: When I push ones= leds start to blink. When I push another one = it's stop to blink. And so forever.

I tried to do it in my fonction void oneoffloop() with return; but it's doesn't work.

How can I do it? please help

/*
Blink 6 LEDS 
*/
//LED pin connections
const int led0 = 2;
const int led1 = 3;
const int led2 = 4;
const int led3 = 5;
const int led4 = 6;
const int led5 = 7;
// push button start or stop 'demo mode'
const int buttonPin = 11;

int buttonState = 0; //variable for reading button state

//variables used to adjust pattern speeds (all in millis delays)
const int  ledx = 0;
const int  delaytime = 50;
const int  delaytimeX = 200;
const int  delayloop = 10;
const int  masterDelay = 10;  //changes speed of ALL Blink patterns: must be a multiple of 5
const int fadeIncriment = 15;
const int fadeIncriment2 = 15;  //determines how long the fade cycles; must be a multple of 255 
void setup() {                

pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT); 
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT); 
pinMode(led4, OUTPUT); 
pinMode(led5, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
/* LEDS layout is as follows:
        (5)(4)(3)
        (0)(1)(2)
illuminate starting at button#5 
and go clockwise the order is 543210
*/
}

//pattern routine that loops forever;
void loop(){
//read the pushbutton value into a variable
  // HIGH when it's open, and LOW when it's pressed. Turn on pin 11 when the
  // button's pressed, and off when it's not:
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {

oneoffloop();

}


 else {
 digitalWrite(led0, HIGH);   
 digitalWrite(led3, HIGH);   
 digitalWrite(led1, HIGH);   
 digitalWrite(led4, HIGH);   
 digitalWrite(led2, HIGH);   
 digitalWrite(led5, HIGH);
 digitalWrite(buttonState, HIGH);

}
}

// LED patterns are below.-------------------------------------------------------------------
void oneoffloop() 
 {  

  for (int loopspeed = 1 ; loopspeed <=201; loopspeed +=masterDelay) {
 digitalWrite(led5, LOW);   digitalWrite(led4, HIGH);   digitalWrite(led3, HIGH);  
 digitalWrite(led2, HIGH);  digitalWrite(led1, HIGH);   digitalWrite(led0, HIGH);  
      delay(loopspeed);
 digitalWrite(led5, HIGH);   digitalWrite(led4, LOW);   digitalWrite(led3, HIGH);  
 digitalWrite(led2, HIGH);  digitalWrite(led1, HIGH);   digitalWrite(led0, HIGH);  
      delay(loopspeed);     
 digitalWrite(led5, HIGH);   digitalWrite(led4, HIGH);   digitalWrite(led3, LOW);  
 digitalWrite(led2, HIGH);  digitalWrite(led1, HIGH);   digitalWrite(led0, HIGH);  
      delay(loopspeed);    
 digitalWrite(led5, HIGH);   digitalWrite(led4, HIGH);   digitalWrite(led3, HIGH);  
 digitalWrite(led2, LOW);  digitalWrite(led1, HIGH);   digitalWrite(led0, HIGH);  
      delay(loopspeed); 
 digitalWrite(led5, HIGH);   digitalWrite(led4, HIGH);   digitalWrite(led3, HIGH);  
 digitalWrite(led2, HIGH);  digitalWrite(led1, LOW);   digitalWrite(led0, HIGH);  
      delay(loopspeed);    
 digitalWrite(led5, HIGH);   digitalWrite(led4, HIGH);   digitalWrite(led3, HIGH);  
 digitalWrite(led2, HIGH);  digitalWrite(led1, HIGH);   digitalWrite(led0, LOW);  
      delay(loopspeed);
      }
      if (buttonState == 1){
      return;
      }
      }

0 Answers