所以这可能不是那么难,但我有点难过。我有一个按钮,它是一个较大的控制面板中的“拍照”按钮。现在LED翻转10秒然后翻转。我希望它在前5秒闪烁,然后在最后5秒保持稳定。不知道怎么做,我已经尝试了一些猜测,但没有去。这是我到目前为止所拥有的,它目前漫长而丑陋:
// take a picture button
const int shoot_pin = 5;
const int shoot_led = 13;
int shoot_state = 0;
int last_shoot_state = 0;
long shoot_timer;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 10; // the debounce time; increase if the output flickers
void setup(){
Serial.begin(9600);
pinMode(shoot_pin, INPUT);
pinMode(shoot_led, OUTPUT);
}
void loop() {
int shoot_reading = digitalRead(shoot_pin);
if (shoot_reading != last_shoot_state) { lastDebounceTime = millis(); }
if ((millis() - lastDebounceTime) > debounceDelay) {
if (shoot_reading != shoot_state) {
shoot_state = shoot_reading;
if (shoot_state == HIGH) {
digitalWrite(shoot_led, HIGH);
shoot_timer = millis();
Serial.println("Counting down...5 seconds"); // for tracking
delay(5000);
Serial.println("Shooting Picture"); // for tracking - eventually will be a keypress
} // end of high
} // end of reading
}// end of that giant nested debaounce
last_shoot_state = shoot_reading;
// right now just stays lit for 10 seconds
if (millis() - shoot_timer >= 10000){ digitalWrite(shoot_led, LOW);}
} // end of loop
答案 0 :(得分:0)
通过调用为您闪烁的自定义函数来替换您的延迟(在这种情况下为blinkLED),然后将开启时间从10秒减少到5
// take a picture button
const int shoot_pin = 5;
const int shoot_led = 13;
int shoot_state = 0;
int last_shoot_state = 0;
long shoot_timer;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 10; // the debounce time; increase if the output flickers
void setup(){
Serial.begin(9600);
pinMode(shoot_pin, INPUT);
pinMode(shoot_led, OUTPUT);
}
void loop() {
int shoot_reading = digitalRead(shoot_pin);
if (shoot_reading != last_shoot_state) { lastDebounceTime = millis(); }
if ((millis() - lastDebounceTime) > debounceDelay) {
if (shoot_reading != shoot_state) {
shoot_state = shoot_reading;
if (shoot_state == HIGH) {
digitalWrite(shoot_led, HIGH);
shoot_timer = millis();
Serial.println("Counting down...5 seconds"); // for tracking
delay(5000);
blinkLED(13, 500, 500, 5);
Serial.println("Shooting Picture"); // for tracking - eventually will be a keypress
} // end of high
} // end of reading
}// end of that giant nested debounce
last_shoot_state = shoot_reading;
// turn on LED for 5 seconds
digitalWrite(shoot_led, LOW);
delay(5000);
digitalWrite(shoot_led, HIGH);
} // end of loop
void blinkLED(int pinNumber, int onLength, int offLength, int repetitions){
for(int i=0; i < repetitions; i++){
digitalWrite(pinNumber, LOW); //on
delay(onLength);
digitalWrite(pinNumber, HIGH); //off
delay(offLength);
}
}