|
本帖最后由 andywwf 于 2022-2-3 15:47 编辑
我有一閃燈程序預備給小朋友裝上積木車上.
程序已弄好, 單獨閃燈測試是沒有問題.
但一結合按鈕開關, 就不行了.
閃燈程序如下:
1) 接上電源 -> LED_1閃2次, 換LED_2閃2次.......loop (兩燈交替互閃)
2) 按一下開關 -> 關閉所有LED
3) 再按一弄開關 -> 返回第一點 (LED_1閃2次, 換LED_2閃2次.......loop)
但問題出現了, 因為預設是一接電是LED ON, 閃燈沒有問題, 關也OK, 就是再開2閃燈就變成同步一起閃了.
請問是什麼原因?
謝謝
- // constants won't change. Used here to set a pin number:
- #define ledPin1 8 // police light A
- #define ledPin2 9 // police light B
- #define WW_ON2 60
- #define WW_OFF2 300
- const int buttonPin = 2; // the pin that the pushbutton is attached to
- // Variables will change:
- int ledState1 = 0; // ledState used to set the LED
- int ledState2 = 0;
- // Variables will change:
- int buttonPushCounter = 0; // counter for the number of button presses //because if (buttonPushCounter % 3 == 0)
- int buttonState = 0; // current state of the button
- int lastButtonState = 0; // previous state of the button
- /*
- // Generally, you should use "unsigned long" for variables that hold time
- // The value will quickly become too large for an int to store
- unsigned long previousMillis = 0; // will store last time LED was updated
- unsigned long previousMillis2 = 0;
- */
- unsigned long FLASH_START_TIME = 0;
- unsigned long FLASH_START_TIME3 = 300;
- /*
- // constants won't change:
- const long interval = 500; // interval at which to blink (milliseconds)
- const long interval2 = 100;
- */
- void setup() {
- // set the digital pin as output:
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
-
- pinMode(buttonPin, INPUT); // initialize the button pin as a input:
- Serial.begin(9600); // initialize serial communication:
- }
- void loop()
- {
- // read the pushbutton input pin:
- buttonState = digitalRead(buttonPin);
- // compare the buttonState to its previous state
- if (buttonState != lastButtonState) {
- // if the state has changed, increment the counter
- if (buttonState == HIGH) {
- // if the current state is HIGH then the button went from off to on:
- buttonPushCounter++;
- Serial.println("on");
- Serial.print("number of button pushes: ");
- Serial.println(buttonPushCounter);
- } else {
- // if the current state is LOW then the button went from on to off:
- Serial.println("off");
- }
- // Delay a little bit to avoid bouncing
- delay(50);
- }
- // save the current state as the last state, for next time through the loop
- lastButtonState = buttonState;
-
- // here is where you'd put code that needs to be running all the time.
- // check to see if it's time to blink the LED; that is, if the difference
- // between the current time and last time you blinked the LED is bigger than
- // the interval at which you want to blink the LED.
- unsigned long currentMillis = millis();
-
- // turns on the LED every four button pushes by checking the modulo of the
- // button push counter. the modulo function gives you the remainder of the
- // division of two numbers:
- if (buttonPushCounter % 2 == 0) {
- //.......................................................................... LED1
- if(millis()>FLASH_START_TIME){
- if(millis()-FLASH_START_TIME<=WW_ON2)
- digitalWrite(ledPin1,HIGH);
- else{
- if(millis()-FLASH_START_TIME<=WW_ON2*2)
- digitalWrite(ledPin1,LOW);
- else{
- if(millis()-FLASH_START_TIME<=WW_ON2*3)
- digitalWrite(ledPin1,HIGH);
- else{
- if (millis()-FLASH_START_TIME<=WW_ON2*4)
- digitalWrite(ledPin1,LOW);
- else{
- if(millis()-FLASH_START_TIME<=WW_ON2*5)
- digitalWrite(ledPin1,HIGH);
- else{
- if(millis()-FLASH_START_TIME<=(WW_ON2*5+WW_OFF2))
- digitalWrite(ledPin1, LOW);
- else{
- FLASH_START_TIME=millis();
- }
- }
- }
- }
- }
- }
- }
- //.......................................................................... LED2
- if(millis()>FLASH_START_TIME3){
- if((millis()-FLASH_START_TIME3)<=WW_ON2)
- digitalWrite(ledPin2,HIGH);
- else{
- if(millis()-FLASH_START_TIME3<=WW_ON2*2)
- digitalWrite(ledPin2,LOW);
- else{
- if(millis()-FLASH_START_TIME3<=WW_ON2*3)
- digitalWrite(ledPin2,HIGH);
- else{
- if (millis()-FLASH_START_TIME3<=WW_ON2*4)
- digitalWrite(ledPin2,LOW);
- else{
- if(millis()-FLASH_START_TIME3<=WW_ON2*5)
- digitalWrite(ledPin2,HIGH);
- else{
- if(millis()-FLASH_START_TIME3<=(WW_ON2*5+WW_OFF2))
- digitalWrite(ledPin2,LOW);
- else{
- FLASH_START_TIME3=millis();
- }
-
- }
- }
- }
- }
- }
- }
-
- // set the LED with the ledState of the variable:
- digitalWrite(ledPin1, ledState1);
- digitalWrite(ledPin2, ledState2);
-
- }else {
- digitalWrite(ledPin1, LOW);
- digitalWrite(ledPin2, LOW);
-
- }
- }
复制代码
|
|