issue with reading analog inputs and for()

Hey Guys.

At the moment i´m trying to build a midi controller.

But i already have problems with reading faders on the anlog inputs.

Because i dont know how many faders my controller will has at the end i tried to automise the script so that i just need to change one definition in my config file and the programm does the rest.

Heres the code:

#define faderanzahl 2 //how many faders are involved
int AnalogInput = {A0,A1,A2,A3,A4,A5};
int fader[faderanzahl - 1];
int faderAlt[faderanzahl - 1];
void setup() {
Serial.begin(9600);
}
void loop() {
for(int i = 0; i < faderanzahl; i++){

fader = 0.4 * map(analogRead(AnalogInput_),0,1023,0,127) + 0.6 * faderAlt*;_
if(fader _!= faderAlt){
Serial.print(“Nummer “);
Serial.print(i);
Serial.print(”\t”);
Serial.println(fader);

}_

faderAlt _= fader;
}
}[/sub]*
The issue is that it 1. always prints the things in the if() 2. it calculates wrong numbers in fader = 0.4 *
if i cut out the for and change every i to like 0 it works fine. But as soon as i try to read in all analog inputs with for it ***** up.
Does anyone know why this doesnt work?
Cheers_

(deleted)

You don't need map(). just divide by 8.

You should not use floats to calculate ints.

I would guess that round-off errors would never let fader = fader_alt.

Just try:

fader = analogin()/8;

with no smoothing and see if that works.

I’m pretty sure your code has italics, because you meant to use code tags, but, at the last minute, that slipped your mind.

for(int i = 0; i < faderanzahl; i++){
   
    fader[i] = 0.4 * map(analogRead(AnalogInput),0,1023,0,127) + 0.6 * faderAlt[i];

Your loop iterates with i equal to 0 and 1, but your arrays are defined to hold one element. Why are the array sizes and the number of iterations of the loop different?

No, this is the correct C idiom. So it should work, or it has been off by one since K&R!

The for loop breaks down to:

I=0;

while (I<faderanzahl){
//loop body
I++
}

so I is 0 at when it goes into the while loop. The body executes. I is incremented before the body is executed. at that time I = fadernanzahl so the while is exited.

I think you know this PaulS!

spycatcher2k:

fader = 0.4 * map(analogRead(AnalogInput),0,1023,0,127) + 0.6 * faderAlt;

Explain what you THINK this line does.

First i had something like

fader1 = analogRead(AnalogInput)
fader2 = 0.4 * fader1 + 0.6 * faderAlt
fader3 = map(fader2,0,1023,0,127)

so i tried to put all this in one and that is the result.
So i think it just does what it needs to do to 1. read the anlogin, 2. map it to midi format and 3. smooth it but like a all in one solution.



KeithRB:
Just try:

fader = analogin()/8;

with no smoothing and see if that works.

i did but thnigs are going creepy. as soon as one fader is != 0 it starts spamming the Serial.print things. AND one fader gives out 0 - 137 and the other one something like 0-150 but jumps back to 9 when i stop moving the fader.



PaulS:

for(int i = 0; i < faderanzahl; i++){

fader[i] = 0.4 * map(analogRead(AnalogInput),0,1023,0,127) + 0.6 * faderAlt[i];



Your loop iterates with i equal to 0 and 1, but your arrays are defined to hold one element. Why are the array sizes and the number of iterations of the loop different?

the only thing i see here is that the array analoginput has a different size than the iterations of the loop. the simple reason is that i havent found a way to automate put in A0, A1 to An in the array analoginput based on the number which is defined at the top with #define faderanzahl x

cheers

KeithRB: No, this is the correct C idiom. So it should work, or it has been off by one since K&R!

Yes, except: int fader[faderanzahl - 1];

I think you know this PaulS!

I know that. But,

#define faderanzahl 2 //how many faders are involved

so the loop WILL iterate twice.

But, that array sizes are (incorrectly) faderanzahl - 1, so the loop WILL read from and write to the arrays beyond the end. After that happens, all bets are off with respect to what the Arduino will do.

Arrrgghh!

I don't know how I missed that. I blame the Italics.

Sorry, PaulS, I impugned your knowledge and I apologise. But last time I was correct!

I blame the Italics.

Of course. I assumed that OP wrote the proper code, but that the forum mangled it because OP didn't post it correctly. That might not be a valid assumption.

Sorry, PaulS, I impugned your knowledge and I apologise. But last time I was correct!

No problem. I was wrong once. 8)

Surprising that there are 8 responses, yet nobody pointed out that this:

if(fader != faderAlt){

can NEVER be true, and will be optimized away by the compiler. It is comparing the memory address of fader to the memory address of faderAlt. Obviously, those two addresses will NEVER be the same!

Nor will these lines do what the OP seems to think:

      Serial.println(fader);
...
     faderAlt = fader;

Regards, Ray L.

"No problem. I was wrong once. 8)" But then he realized he had made a mistake ;)

Surprising that there are 8 responses, yet nobody pointed out that this:

Is that what OP’s code REALLY looks like?

PaulS: Is that what OP's code REALLY looks like?

No. If you "Quote" the original post you get to see the un-mangled form:

   if(fader[i] != faderAlt[i]){

johnwasser: Yes, except: int fader[faderanzahl - 1];

So if this isnt the correct way to do it, can you tell me how to do it correctly? I just want that the array size is bigger when i change the number under #define faderanzahl 2 to like 5 or 6. The reason why i want this: Im programming a midi controller and i cant test all things with the number of faders the final controller will have. At the end im coming up with a controller with like 40 to 50 buttons, 30 encoders and like 10 faders. Im testing it here with only a few of each and when all works perfectly i will just add the other buttons and faders and then i just want to change only a few things and dont want to rewrite the whole code....

PaulS: I know that. But,

#define faderanzahl 2 //how many faders are involved

so the loop WILL iterate twice.

But, that array sizes are (incorrectly) faderanzahl - 1, so the loop WILL read from and write to the arrays beyond the end. After that happens, all bets are off with respect to what the Arduino will do.

im not a good programmer but what i know: in computer language all start with 0. so position number 1 is 0 in computer language, 2 is 1 and so on. So when i write in the config a 4 for 4 faders, my array needs to be 3 to get 4 values in (0,1,2,3) or am i wrong at this point?

Edit:

Because johnwasser and PaulS said something about my arrays i just tried to change int fader[faderanzahl - 1] to fader[faderanzahl].

Now all my code i wrote in the first post is running perfectly. But i think my array is always 1 too big?!

Maybe someone can explain that to me, just for the understanding.

When you initialize an array with 2 elements you use int array[2]. The elements will be numbered 0 and 1.

masterdrummer: im not a good programmer but what i know: in computer language all start with 0. so position number 1 is 0 in computer language, 2 is 1 and so on. So when i write in the config a 4 for 4 faders, my array needs to be 3 to get 4 values in (0,1,2,3) or am i wrong at this point?

Gabriel explained it well, but I just wanted to emphasize that not all languages make the first element of an array "0". Some start at 1, while others have something like "OPTION BASE 1" that will let you select 1 or 0 as your choice.

In fact: http://c-faq.com/aryptr/non0based.html

But i think my array is always 1 too big?!

No. The value in the [] is the number of elements you want in the array, NOT the index value of the last element.

#define faderanzahl 2
int fader[faderanzahl];

creates an array with two elements. The index values are 0 and 1.

PaulS: No. The value in the [] is the number of elements you want in the array, NOT the index value of the last element.

#define faderanzahl 2
int fader[faderanzahl];

creates an array with two elements. The index values are 0 and 1.

Ahh okay. That makes sense. Thank you guys for your nice help. i really appreciate that.

THANKS!! :)