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?

asked Mar 1, 2018 at 6:10
1
  • go through the loop() code listing step by step ... write values of variables on a sheet of paper as you go along Commented Mar 1, 2018 at 6:54

1 Answer 1

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.

answered Mar 1, 2018 at 6:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.