I was having trouble with inputs and outputs on a new UNO connected to an 8 relay board, so I decided to create a simple sketch to test digital 0-13 as outputs, and strange outputs occur after the first run on the For loop, and then instead of just repeating, it does some crazy outputs (sometimes several at one time) and it takes several minutes before the For loop repeats my code again.
Is this UNO troubled or my code?
void setup() {
for (int i=0; i<=13; i++) pinMode (i, OUTPUT);
for (int i=0; i<=13; i++) digitalWrite(i, HIGH); //Initialize all relays off (HIGH is off)
}
void loop() {
for (int i=0; 1<=13; i++){
digitalWrite (i, LOW);
delay (1000);
digitalWrite (i,HIGH);
}
delay (500);
}
Crazy behavior, you all should try it! makes crazy outputs after the first run, and about a minute of so later acts fine again. So interesting (troubling up till now), there must be a good use for this?
Well what happens is that your condition to continue the loop 1 <= 13 is always true so your variable i keeps incrementing super fast past 13 and issues the digitalWrite which do nothing on pins that do not exist. This for loop never ends and your loop never loops
Int are signed numbers coded on 2 bytes on your UNO, so after a while your int reaches 32,767 And then overflows and becomes negative at -32,768, then keeps growing and hit again 0 after a while.