I'm a beginner, I have this problem while playing around with Serial Monitor
If I disable line 27 only, then line 28 printed normally and vice versa.
But when I activate both line 27 & 28, they didn't work, the Serial Monitor printed NOTHING!
I couldn't figure why, is that just me or a serial printing problem? Would you help me to make both line work?
// board: Arduino UNO R3
//the schematic is simple: only a potentiometer connect to 5V, GND, and A0
int potPin = A0;
int read_Value;
const int num = 10;
int index = -1; // -1 because it will be increased by 1 in the first loop
// it will never be -1 again in any next loop
int array1[num];// so there is 11 variable, index from 0 to 10
void setup() {
Serial.begin(9600);
for (int i = 0; i<= num; i+=1) {
array1[i] = 1;
}
}
void loop() {
if ( index >= num) {
index = 0;
} else {
index += 1;
}
read_Value = analogRead(potPin);
Serial.println(read_Value); // Line 27
Serial.println(array1[index]); // Line 28
delay(100);
}
The little change you need to make in your sketch is this: for (int i = 0; i < num; i += 1) in order to get print out on your Serial Monitor. The reason has been explained in Post#2.
Also, as you are PRE-incrementing, the index this code needs to change or you’ll be reading one element past the end as well...
if ( index >= num - 1) {
index = 0;
} else {
index += 1;
}
TBH you’d be far better POST-incrementing. It’s easier to handle...
// board: Arduino UNO R3
//the schematic is simple: only a potentiometer connect to 5V, GND, and A0
int potPin = A0;
int read_Value;
const int max_index = 10;
int index = 0;
int array1[max_index];// so there are 10 variables, index from 0 to 9
void setup() {
Serial.begin(9600);
for (int i = 0; i < max_index; i++) {
array1[i] = 1;
}
}
void loop() {
read_Value = analogRead(potPin);
Serial.println(read_Value);
Serial.println(array1[index]);
index++;
if (index >= max_index) index = 0;
delay(100);
}