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?
-
go through the loop() code listing step by step ... write values of variables on a sheet of paper as you go alongjsotola– jsotola2018年03月01日 06:54:07 +00:00Commented Mar 1, 2018 at 6:54
1 Answer 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.