停止虚空功能

时间:2019-04-22 00:41:05

标签: arduino-uno void

当我再次按下按钮时,我想停止LED闪烁。如果我再次按下,请重新启动。

我的意思是: 当我按一个时,指示灯开始闪烁。 当我推另一个=时,它停止闪烁。 永远如此。

我尝试通过void oneoffloop()功能来做到这一点 与return;一起使用,但这是行不通的。

我该怎么办? 请帮助

/*
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 个答案:

没有答案