A if-condition is performed as long as the condition is met. If however the if-condition is met and the execution of the code contained in the if-condition is done once and if this satisfies my program requirements, is it better to include a extra condition so that the if-condition based code is executed only once, or should I keep for simplicity and just let it run as long as the original condition is met?
For example: an if-condition is based on a digital sensor input, and when the condition is met then a LED is lit up. Now as long as the condition is met, a digital write will be done to set the LED digital output high, in fact continually writing a hig to this particular output.
Would it be better programming practice to write code so that this digital write to high is done only once?
Or is it as good to continuously write a high to the LED output as long as the if condition is being met (making for some less complex programming)?
brice3010:
A if-condition is performed as long as the condition is met. If however the if-condition is met and the execution of the code contained in the if-condition is done once and if this satisfies my program requirements, is it better to include a extra condition so that the if-condition based code is executed only once, or should I keep for simplicity and just let it run as long as the original condition is met?
For example: an if-condition is based on a digital sensor input, and when the condition is met then a LED is lit up. Now as long as the condition is met, a digital write will be done to set the LED digital output high, in fact continually writing a hig to this particular output.
Would it be better programming practice to write code so that this digital write to high is done only once?
Or is it as good to continuously write a high to the LED output as long as the if condition is being met (making for some less complex programming)?
IF your program has nothing else to do, continuously writing HIGH or LOW to a pin changes nothing. For you example, you need to know when to change from writing HIGH to writing LOW.
LEDs don't care. The Arduino pin doesn't care. A relay control doesn't care.
Paul
Paul_KD7HB:
IF your program has nothing else to do, continuously writing HIGH or LOW to a pin changes nothing. For you example, you need to know when to change from writing HIGH to writing LOW.
LEDs don't care. The Arduino pin doesn't care. A relay control doesn't care.
Paul
Thanks; indeed rather simple program that just checks 2 input levels from swimming pool level sensor, so lots of delays and slow acting.
Radio amateur since 1955, 34 years of programming,..!! Wow, that makes for some experience!
brice3010:
A if-condition is performed as long as the condition is met. If however the if-condition is met and the execution of the code contained in the if-condition is done once and if this satisfies my program requirements, is it better to include a extra condition so that the if-condition based code is executed only once, or should I keep for simplicity and just let it run as long as the original condition is met?
For example: an if-condition is based on a digital sensor input, and when the condition is met then a LED is lit up. Now as long as the condition is met, a digital write will be done to set the LED digital output high, in fact continually writing a hig to this particular output.
Would it be better programming practice to write code so that this digital write to high is done only once?
Or is it as good to continuously write a high to the LED output as long as the if condition is being met (making for some less complex programming)?
It really depends. As one poster mentioned, if you just want to change the state of a pin that drives a LED, continuing to let the IF test make the determination doesn't hurt anything, is likely doing nothing, and is taking up less code space than only taking the action based on whether its already been done.
On the other hand if you are updating an LCD display, you might find that continuing to do so based purely on the "IF" statement results in a flickering display that is both annoying to look at, and probably is taking up unnecessary cpu resources. So in a case like that, you might update the LCD IF the button is pushed AND it was NOT pushed the last time you looked.
Then there are times when you need a happy medium. Again, consider a visual display like an LCD displaying something like temperature. In a case like that you might want to update the display IF the button is pushed AND a half second has passed since the last time you took the action. Thus, you are taking the action just when the IF is satisfied, while pacing the frequency of the action to whatever seems most appropriate.
Thanks for those enlightening answers, karma added to the both of you.
In my particular case I just realised that when the sensor state changes I need to start a timer, so I do have to make sure that particular piece of code is addressed just once. So PeterPan321's suggestion in your second paragraph is pertinent (state change detection).