I’m having a problem with an array of unsigned ints that I can’t figure out. In the code below the arduino will not reach the establishConnect() function. Nothing gets sent over the serial:
unsigned long update_interval[3] = {0, 0, 0};
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
for (int i = 0; i < sizeof(update_interval); i++) {
update_interval[i] = 0;
}
establishContact();
}
void loop()
{
//
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("."); // send an initial string
delay(300);
}
}
However if I change the array to type of int, everything works, and periods begin getting sent over the serial:
int update_interval[3] = {0, 0, 0};
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
for (int i = 0; i < sizeof(update_interval); i++) {
update_interval[i] = 0;
}
establishContact();
}
void loop()
{
//
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("."); // send an initial string
delay(300);
}
}
Because sizeof returns the number of bytes in the object, not the number of elements. You are just lucky it works with ints. You are clobbering memory.
you need sizeof(update_interval)/sizeof(update_interval[0]