2013-11-25 2 views
0

Так что, наверное, это не так сложно, но я немного в тупике. У меня есть кнопка, которая является кнопкой «Сделать снимок» на большой панели управления. Прямо сейчас светодиод включается на 10 секунд, затем сбрасывается. Я бы хотел, чтобы он мигал в течение первых 5 секунд, а затем оставался твердым в течение последних 5 секунд. Не уверен, как это сделать, я пробовал некоторые догадки, но не пошел. Вот то, что я до сих пор, его в настоящее время долго и некрасиво:Мигает светодиодом в течение первых 5 секунд, остается твердым в течение последних 5 секунд при нажатии кнопки (Arduino)

// 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

заменить задержку с вызовом пользовательской функции, которая мигает для вас (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); 
    } 
} 
+0

Это дает мне «переменная или поле„blinkLED“объявляются недействительными ошибки. –

+0

Я не перед моим компилятором, позвольте мне взглянуть на него. – DrCord

+0

Я думаю, что я нашел проблему с мой пользовательский fucntion и обновил код, я не декларировал каждую из переменных, которые он использовал как int, что происходит, когда вы программируете на связке языков: D – DrCord

Смежные вопросы