Problem with if()

Hello every one,
I'm working on a music visualizer controller. The controller has 35 potentiometers. Every frequency (63 160 400 1K 2.5K 6.25K 16K) Hz has his red, green, blue, treshold and quantity potentiometer. The exact specification of what do the potentiometers do ins't that important. I'm using an Arduino mega2560 with 2 multiplexers and 1 demultiplexer. I have 7 leds on my controller one for each frequency. The leds should react to the music and be less or more precis dependins on the treshold in my case "intensitypotval[6]". The problem is that in the code the treshold seems to work only if I write a value and not if I put a variable like "intensitypotval[6]". All my potentiometers values are readen well, I already checked that. I hope that my question makes sens. :slight_smile:

#CODE


#include <AudioAnalyzer.h>// audio analyser library
#include "Mux.h"//multiplexer library
using namespace admux;// multiplexer library simplification

Analyzer Audio = Analyzer(10, 9, 0); //Strobe pin ->10  RST pin ->9 Analog Pin ->0

int  FreqVal[6];// frequency values
double FreqValhelp[6];// frequency values

//list per potentiometer type (R, G, B, intensity, quantiy)
double Rpotval[6];//list of all values of red knobs start Rpotval[0]= value knob red 63Hz
double Gpotval[6];//list of all values of green knobs start Gpotval[0]= value knob red 63Hz
double Bpotval[6];//list of all values of blue knobs start Bpotval[0]= value knob red 63Hz
double intensitypotval[6];//list of potentiometer value for intensity start potval[0]= potentiometer value of knob 63Hz
double quantitypotval[6];//list of potentiometer value to choose the quantity of the frequence to put on the led stripe

//multiplexer 1
Mux RG_mux(Pin(A7, INPUT, PinType::Analog), Pinset(29, 27, 25, 23));  // create a new Mux object

//multiplexer 2
Mux BI_mux(Pin(A8, INPUT, PinType::Analog), Pinset(37, 35, 33, 31));  //create a new Mux object

//multiplexer 3
Mux Rled_mux(Pin(6, OUTPUT, PinType::Digital), Pinset(39, 41, 43, 45));  // create a new Mux object


void setup() {
  Serial.begin(57600);  //Init the baudrate
  Audio.Init();//Init module
}


void loop() {

  Audio.ReadFreq(FreqVal);//return 7 value of 7 bands pass filiter
  //Frequency(Hz):63  160  400  1K  2.5K  6.25K  16K
  //FreqVal[]:      0    1    2    3    4    5    6

  for (int i = 0; i < 14; i++) {
    if (i < 7) {
      Rpotval[i] = RG_mux.read(i);
      Bpotval[i] = BI_mux.read(i);
      quantitypotval[i] = analogRead(9 + i);//reads values from intesity potentimeter
    } else {//if (i >= 7)
      Gpotval[i - 7] = RG_mux.read(i);
      intensitypotval[i - 7] = BI_mux.read(i);

    }
  }

  for ( int i = 0 ; i < 7; i++) {
    if (intensitypotval[i] < FreqVal[i]) { // <----- PROBLEM !!!!!!
      Rled_mux.write(FreqVal[i], i); //give the frequency impulse to the red led
    } else {
      Rled_mux.write(LOW, i); //give the frequency impulse to the red led
    }
  }
}

//    for (int i = 0; i < 10 ; i++)
//    {
//      leds[i].setRGB(( Map(valB) / 255)*FreqValhelp[Mapinteger(potvalnum)], (Map(valR) / 255)*FreqValhelp[Mapinteger(potvalnum)], (Map(valG) / 255)*FreqValhelp[Mapinteger(potvalnum)]);
//    }


double Map(double x) { //maps all value from 0-1023 to 0-255
  return map(x, 0, 1023, 0, 255);
}

double Maptoone(double x) {
  return map(x, 0, 1023, 0, 1);
}

Your post was MOVED to its current location as it is more suitable.

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Your arrays have 6 elements e.g. 'double Rpotval[6]'. What you seem to have missed is that these elements are numbered 0-5. When you try to read Rpotval[6] as you often do you're looking at a non-existent 7th element. It could read any old rubbish.

Steve

Thank you for replying :slight_smile:

I had the same though at first but then I went on the arduino page to check and this came out: "Arrays are zero indexed, that is, referring to the array initialization above, the first element of the array is at index 0". So my arrays Rpotval[6] has 7 values in it, starting from Rpotval[0] to Rpotval[6].

I came out of my problem by just putting the Problem if() just after the
intensitypotval[i] was readen.

No. The array Rpotval[6] has 6 elements in it (hence the 6) numbered from Rpotval[0] to Rpotval[5]

Ahaaaa, thanks I never stop learning :slight_smile:

Sometimes it's quite irritating to be told I don't know what I'm talking about. Good luck!