Notifications
Clear all
Electronic Components & Theory
31
Posts
4
Users
8
Likes
1,484
Views
2022-09-15 12:47 pm
Should this have the keywords highlighted?
[snip]//define function to turn LED's offvoidalloff(void){for(int i = 14; i < 17; i++){digitalWrite(14,LOW);}}//define function to turn LED's onvoidallon(void){for(int i = 14; i < 17; i++){digitalWrite(14,HIGH);}}[snip]
I don't see that anyone has picked up on these errors too!
You define 'i' as an iterator/index to transverse a range, yet you don't end up using it in your 'digitalWrite(...)' function!
Ideally, you want something like the following (I have renamed variables to be more self documenting):
void all_leds_off() { for (byte index = 14; index < 17; index++) { digitalWrite (index, LOW); } } void all_leds_on() { for (byte index = 14; index < 17; index++) { digitalWrite (index, HIGH); } }
Now, you are actually using the variable to iterate through the range of gpio's, and action them accordingly.
An even better alternative might be something like the following, as your programming skills come back to you 😉
void setLeds(int state, byte low, byte high) { for (byte index = low; index < high; index++) { digitalWrite (index, state); } }
Of course, you would use it at follows:
setLeds(LOW, 14, 17); // Or... setLeds(HIGH, 14, 17);
Cheers
Page 3 / 3
Prev