I'm new to the arduino project but it makes a lot of fun so far.
I get a warning because of a for-loop but I don't know why. It seems the compiler doesn't like the condition in the for-loop. If I put there index < 6 it works but index < pin_count doesn't.
This here works without warning:
const int pin_array[] = {11, 10, 9, 6, 5, 3}; // LED Pins mit PWM
const int pin_count = sizeof(pin_array); // Anzahl von Pins
void setup ()
{
for(int index = 0; index < 6; index++)
{
pinMode(pin_array[index], OUTPUT); // Pins im Array als Ausgabe-Pins defnieren
}
}
void loop()
{
}
But this results in a warning:
const int pin_array[] = {11, 10, 9, 6, 5, 3}; // LED Pins mit PWM
const int pin_count = sizeof(pin_array); // Anzahl von Pins
void setup ()
{
for(int index = 0; index < pin_count; index++)
{
pinMode(pin_array[index], OUTPUT); // Pins im Array als Ausgabe-Pins defnieren
}
}
void loop()
{
}
The warning:
...\programm-2.ino:20:28: warning: iteration 6u invokes undefined behavior [-Waggressive-loop-optimizations]
pinMode(pin_array[index], OUTPUT); // Pins im Array als Ausgabe-Pins defnieren
^
const int pin_array[] <<-- why not byte?
const int pin_count <<-- why not byte?
Are the pin numbers going to exceed 255? Is the pin count going to exceed 255?
This will work with all data types (e.g., char, int, long, etc.) because the macro doesn't state the specific data type, but the compiler figures it out the type from the definition of the parameter x, so you might use it as:
for(int index = 0; ELEMENTCOUNT(pin_array); index++)
It's a perfectly understandable mistake, as is the one you'll make if you try to use the same trick inside a function, on function parameter that happens to be a pointer