1

I have the sketch from James Waldby (Read RC receiver channels using Interrupt instead of PulseIn) working beautifully. I want to use a switch on my TX to start and stop a sequence of lights (emulating guns firing on a ship model). But first, I need the delayed flash routine to work. I have tried a loop counting millis() differences to separate the sequences, but the delays don't delay.

 void loop(){
  rc_read_values();//Mr. Waldby's interrupt routine
  currentMillis = millis();
  previousMillis = currentMillis;
  if(fireGun ==1){
    interval = 3000;
  }else{
    interval = 1500;
  }
  interval = 3000;
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

//if(fireGun ==1){
  analogWrite(led, 255); 
  delay(50);
/* a real gun flash doesn't cut right off; it fades away.
  This sequence simulates that  */  
  while(brightness > 0){

    // change the brightness for next time through the loop:
    brightness = brightness - fadeAmount;
    analogWrite(led, brightness);
    delay( 7);                            
  }
  brightness = 80;
}

The intention here is to flash the led every 3 seconds. The flash loop works fine, but the 3 second delay doesn't.

The delay routine works ok in a sketch by itself.

Any thoughts about what I might be doing wrong?

1 Answers1

1
  currentMillis = millis();
  previousMillis = currentMillis;

So currentMillis and previousMillis will always be exactly the same, right?

  if (currentMillis - previousMillis >= interval) {

So if 0 >= interval (which it won't be) do something?

Therefore your fading code will never be executed.

Nick Gammon
  • 38,901
  • 13
  • 69
  • 125