Pedro147:
How might the for loops be modified to reduce code size and increase speed execution if I may ask.
Tried to find the previous discussion on the subject several years back but I believe it is in the 'old' forums database. I'll just post the modifications and let the discussion reemerge as a result.
Most processors are able to count down to zero faster and with less code than counting up, comparing the counter to a 'ceiling' value and deciding whether to loop again or not.
#define N_ELEMENTS(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))
const uint8_t pinLED_0 = 3;
const uint8_t pinLED_1 = 4;
const uint8_t pinLED_2 = 5;
const uint8_t pinLED_3 = 6;
const uint8_t pinLED_4 = 7;
const uint8_t pinLED_5 = 8;
const uint8_t pinLED_6 = 9;
const uint8_t pinLED_7 = 10;
const uint8_t pinsLEDS[] = { pinLED_0, pinLED_1, pinLED_2, pinLED_3, pinLED_4, pinLED_5, pinLED_6, pinLED_7 };
const uint8_t LED_OFF = LOW;
const uint8_t LED_ON = HIGH;
char flagsLEDS = B10000001;
void setup ()
{
// Serial.begin(9600);
for ( int i = N_ELEMENTS(pinsLEDS); i--; )
{
pinMode(pinsLEDS[i], OUTPUT);
}
}
void loop ()
{
for ( int mask = 0b10000000, i = N_ELEMENTS(pinsLEDS); i--; mask >>= 1 )
{
digitalWrite(pinsLEDS[i], ((mask & flagsLEDS) ? LED_ON : LED_OFF));
// Serial.print(i);
// delay(1000);
}
}