Good saturday evening. I built a led light array with 8 leds, each one connected to my Arduino Uno (328PU) digital output pins. I wanted to use the datatype byte instead of int to save space. The values of those variables will not exceed 255, the maximum value of a byte but I got strange results in this simple sketch. The leds should light up in order 1 to 8 and then swith off from 1 to 8 and repeat.
//1
byte ledPins[] = {2,3,4,5,6,7,8,9};
void setup() {
Serial.begin(9600);
//2
for(byte i = 0; i < 8; i++){
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
//3
for(byte i; i < 8; i++){
digitalWrite(ledPins[i], HIGH);
delay(1000);
}
//4
for(byte i; i < 8; i++){
digitalWrite(ledPins[i], LOW);
delay(1000);
}
}
I have commented 4 sections in the code where I used the datatype byte. I started by using in section 1 byte and in the rest int. Nothing happened. After a while I realized the problem was with the data types. Then I tested every single combination of those datatypes (see Report.png). I also tested those on an older Arduino IDE.
Case 1, where only leds 3 to 8 were lit, stayed the same as I shifted the output pins by 2 (pins 4-11).
Also, without the begin of serial everyting worked great (every 4 variables were set to byte).
It sure is no problem to use integers in this project to get it to work but this should work in theory. So where is the problem?
Have a nice day!
ElektroVirus