Hello.
In my code, i set some values to an array, when i try to display it outside the loop (the code thats enclosed by /.../) it doesn't work. When i display it in the loop it does work exactly like i want it.
It seems like the array potFixed looses its data.
Why is that?
const int numOfPotentiometers = 5;
const int potPins[] = {A0,A1,A2,A3,A4};
void setup() {
Serial.begin(9600);
}
void loop() {
MoveServo();
}
int MoveServo() {
int potRaw[] = {};
int potFixed[] = {};
for(int i = 0; i<numOfPotentiometers; i++) {
potRaw[i] = analogRead(potPins[i]);
potFixed[i] = map(potRaw[i],0,1023,0,180);
Serial.print(potFixed[i]);
Serial.print(", ");
}
delay(100);
/*
for(int i = 0; i<numOfPotentiometers; i++) {
Serial.print(potFixed[i]);
Serial.print(", ");
}
*/
Serial.println("");
}
When you say it "doesn't work" you leave a lot of room for confusion. It does something and that something didn't meet your expectations. So what was the something it did and how did that compare or contrast with what you expected.
Right now it obviously doesn't print because the code to print it is commented out with the /* and */.
AWOL: int potFixed[] = {}; You reinitialise it every time.
My first thought, but he also completely fills it and tries to print it (assuming he is uncommenting that part) in the same pass of the function so it should still print him something.
You've declared that the MoveServo function will return an int. But you have no return statement anywhere in the function body. It isn't nice to lie to the compiler.
The problem isn't scope. The problem is that this:
int potFixed[] = {};
creates an array with 0 elements. Not very useful at all and when he tries to write to it later he is going out of bounds on the array with the first write.
You MUST either give an array a number of elements OR a set of initializers that it can count when you create it.
I want it to create an empty array when i call MyServo() and then fill it in with the current potentiometer value.
What I do not understand is even though I reinitialize it every time i should still be able to print the values of potFixed (in the MoveServo function)?
I re-edited the code below to showcase how i want to print it.
int MoveServo() {
int potRaw[] = {};
int potFixed[] = {};
for(int i = 0; i<numOfPotentiometers; i++) {
potRaw[i] = analogRead(potPins[i]);
potFixed[i] = map(potRaw[i],0,1023,0,180);
}
for(int i = 0; i<numOfPotentiometers; i++) {
Serial.print(potFixed[i]);
Serial.print(", ");
}
Serial.println("");
delay(100):
}
That's no problem. But when you create this array you have to create an array that is large enough to hold what you want to put in there. If you create a 0 element array and then try to fill it you are writing out of bounds to the array.
int MoveServo() {
int potRaw[numOfPotentiometers] = {};
int potFixed[numOfPotentiometers] = {};
for(int i = 0; i<numOfPotentiometers; i++) {
potRaw[i] = analogRead(potPins[i]);
potFixed[i] = map(potRaw[i],0,1023,0,180);
}
for(int i = 0; i<numOfPotentiometers; i++) {
Serial.print(potFixed[i]);
Serial.print(", ");
}
Serial.println("");
delay(100):
}
I want it to create an empty array when i call MyServo() and then fill it in with the current potentiometer value.
Creating an array of fixed size and known initial values is NOT the same as an empty array. An empty array has room for no elements. Not useful at all.
Delta_G:
That's no problem. But when you create this array you have to create an array that is large enough to hold what you want to put in there. If you create a 0 element array and then try to fill it you are writing out of bounds to the array.
int MoveServo() {
int potRaw[numOfPotentiometers] = {};
int potFixed[numOfPotentiometers] = {};