Notifications
Clear all

Constructor Error

31 Posts
4 Users
8 Likes
1,484 Views
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@barrie

Posted by: @barrie

Should this have the keywords highlighted?

[snip]
 
//define function to turn LED's off
voidalloff(void){
for(int i = 14; i < 17; i++){
digitalWrite(14,LOW);
}
}
 
//define function to turn LED's on
voidallon(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


   
ReplyQuote
Page 3 / 3