Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
int RPM=100; //change this to turn flash on/off.
bool show=true;
long lastChange=0;
void loop(){
if((millis()-lastChange)>=500){ //change state every 500mS (flash rate)
lastChange=millis(); //remember when we changed
show=!show; //change state
}
if((RPM>0) && (RPM<200) && show) //if ALL are true
digitalWrite(LED_BUILTIN,HIGH); //turn on LED
else //otherwise
digitalWrite(LED_BUILTIN,LOW); //turn it off
}
This all seems rather complicated.
Well on the first call it doesn't but on subsequent calls it would. That's easy enough to fix by seting timer_cmp = 0 in the case where your done flashing the LED.You do realize that your code has the same glitch.
Plus, when millis rolls over you LED will have a fixed state for a very long time.
Also, your readable and understandable code doesn't reference rpm at all!
It just seems to be an over complicated LED flasher.
if((millis()-lastRead)>rate){
lastRead=millis();
.....
You are assuming that your code gets called at least once per millisecond.
The wrap condition can be handled as a special case but the complexity involved isn't warranted
Consider if timer_cmp=4294967295 and millis is one less. If your routine is called 2mS later then millis() will be zero and timer_cmp=4294967295 - no flashing for 49 days.
The wrap condition doesn't require any special case code just do it the way I said in my last post. I.E. calculate elapsed time.
Whoops, typo, normally I use uint32_t and changed to long for beginner clarity. Oh well.
Mike.
Edit, BTW, how are there issues with my code when it does exactly what was requested? You seem to have added additional conditions.
You've added an assumption. Nowhere is this stated! Stop assuming. And stop adding features.The LED is being used to indicate low RPM warning light.