I have a question about arrays in Arduino. Sorry for my bad English. I want to put numbers in an array, and I think the code below works properly.
However, it doesn't work and corrupted numbers show up in the Serial monitor. Even delay(1000)
doesn't work.
If I change ND_MAC[i]=128;
to "ND_MAC[i]=126;", it works properly. So I'm wondering if this array is char type, but I designate int to array.
How to solve it?
int ND_MAC[7];
char i=0;
void setup() {
Serial.begin(9600);
}
void loop() {
for(i=0;i<8;i++){
ND_MAC[i]=128;
Serial.print(ND_MAC[i],HEX);
}
Serial.println();
delay(1000);
}
-
6First error is iterating outside the array. ND_MAC has 7 elements (0..6).Mikael Patel– Mikael Patel2016年12月23日 12:02:46 +00:00Commented Dec 23, 2016 at 12:02
1 Answer 1
For an array of size 7, the indexes start from 0
(ND_MAC[0]
) and end at 6
(ND_MAC[6]
) as @MikaelPatel commented.
So the solution to your issue would be changing the condition in for loop to
for(i=0;i<7;i++) //Changed 8 in your code to 7
Here, the loop iterates from i = 0 to 1 < 7 (i.e., till 6) and it should work. :)
-
Oh,I had a terrible mistake...kishida– kishida2016年12月26日 12:05:47 +00:00Commented Dec 26, 2016 at 12:05
-
Oh,I had a terrible mistake... I followed your help, and then it worked properly. thank you very much!kishida– kishida2016年12月26日 12:07:53 +00:00Commented Dec 26, 2016 at 12:07
-
@kishida you're welcome....Prashanth Benny– Prashanth Benny2016年12月27日 05:36:16 +00:00Commented Dec 27, 2016 at 5:36