Question

So this is probably not that hard, but I'm a bit stumped. I have a button that is the "Take picture" button in a larger control panel. Right now the LED flips on for 10 seconds then flips off. I'd like it to blink for the first 5 seconds, then stay solid for the last 5 seconds. Not sure how to go about that, I've tried some guessing, but no go. Here's what I have so far, its currently long and ugly:

// 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
Was it helpful?

Solution

replace your delay with a call to a custom function that blinks for you (blinkLED, in this case) and then reduce the on time from 10 secs to 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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top