3 variables in a conditional?

I have the following function, that triggers the same midi note in 2 different channels:

  if (sensorValueH1 > (threshold +100) && KeyLastStateH1 == false) {
  MIDI_TX(128,sensornoteH1,127);
  MIDI_TXTRA(128,sensornoteH1,127);
  KeyLastStateH1 = true;
  }
  else if (sensorValueH1 < (threshold -100) && KeyLastStateH1 == true) {
  MIDI_TX(144,sensornoteH1,127);
  MIDI_TXTRA(144,sensornoteH1,127);
  KeyLastStateH1 = false;
  }

and I would like that MIDI_TXTRA only happens if val01 > 10. so I split the function in 2 functions:

  if (sensorValueH1 > (threshold +100) && KeyLastStateH1 == false) {
  MIDI_TX(128,sensornoteH1,127);
  KeyLastStateH1 = true;
  }
  else if (sensorValueH1 < (threshold -100) && KeyLastStateH1 == true) {
  MIDI_TX(144,sensornoteH1,127);
  KeyLastStateH1 = false;
  }

  if (sensorValueH1 > (threshold +100) && KeyLastStateH1 == false && val01 > 10) {
  MIDI_TXTRA(128,sensornoteH1,127);
  KeyLastStateH1 = true;
  }
  else if (sensorValueH1 < (threshold -100) && KeyLastStateH1 == true && val01 > 10) {
  MIDI_TXTRA(144,sensornoteH1,127);  
  KeyLastStateH1 = false;
  }

but this is not working. MIDI_TXTRA is not happening.

what am I doing wrong?

Throw some serial prints in there so you can see what the values of those variables are at that time. Otherwise you'll have to show us more than just this small snippet of code before we could possibly guess at how it is acting. Post the whole code and a good explanation of how you expect it to work.

well, for the first question I think there is plenty of information, so perhaps we could start by confirming this:

is it possible to have 3 variables in an if statement?

if (sensorValueH1 > (threshold +100) && KeyLastStateH1 == false && val01 > 10)

or I need for example parenthesis and such things? because as it is I get no compilation errors, but it also doesnt work as expected

without parentheses, the operators will group as per this chart:

http://en.cppreference.com/w/cpp/language/operator_precedence

So, in your case, they will group as expected.

But, as said above, in order to help you efficiently for you and us, we need to see the whole program, not just a bit of it, so we can see the interactions.

is it possible to have 3 variables in an if statement?

Yes, of course. The if statement simply tests whether the expression evaluates to true or false and acts accordingly.

I would write it as

if ( (sensorValueH1 > (threshold +100) ) && (KeyLastStateH1 == false) && (val01 > 10) )

but if it produced unexpected results I would print the value of the variables as has been advised.

3 conditions work just fine. And the order in there is fine as well. But a few spaces don't hurt and make it at least more readable.

And indent the code after a opening bracket { to make it more readable.

Sometimes writing a short test program can make things clearer:

void setup() {
  Serial.begin(9600);
  boolean KeyLastStateH1 = false;
  int sensorValueH1;
  int sensornoteH1;
  int threshold;

  // KeyLastStateH1 = true;    // change these values to test
  sensorValueH1 = 500;
  threshold = 100;
  
  switch (KeyLastStateH1) 
  {
    case true:
      if (sensorValueH1 < (threshold - 100)) {
        MIDI_TX(144,sensornoteH1,127);
        MIDI_TXTRA(144,sensornoteH1,127);
        KeyLastStateH1 = false;
      }
      break;
    
    case false:
      if (sensorValueH1 > (threshold +100)) {
        MIDI_TX(128,sensornoteH1,127);
        MIDI_TXTRA(128,sensornoteH1,127);
        KeyLastStateH1 = true;
      }
      break;
  }
}

void   MIDI_TX(int a, int b, int c)
{
  Serial.print("MIDI_TX called, parm1 = ");
  Serial.println(a);
}

void   MIDI_TXTRA(int a, int b, int c)
{
  Serial.print("MIDI_TXA called, parm1 = ");
  Serial.println(a);
}


void loop() {
}

By changing the values before the switch statement block, you can see what’s going on more clearly. I think it’s easier to see the logic with a switch-case block than an if-else block, but that’s just personal choice. You could use a Serial.available() call in loop() to make it possible to change the test values at runtime, but that’s probably not worth the effort.

Finally, use Ctrl-T in the IDE to put your code in a more common C style before posting. It makes it easier to read.

here goes the code. (sometimes I minimize the importance of having the whole code…)

unsigned char status;
#define midichannel1 0;
#define midichannel2 1;
#define midichannel3 9;

int threshold = 700;

int val01 = 0;
int lastVal01 = 0;

boolean KeyLastStateH1 = false;

int sensorValueH1;
int sensornoteH1 = 10;


void setup() {
  Serial.begin(31250);
  pinMode(4, OUTPUT);    // s0
  pinMode(5, OUTPUT);    // s1
  pinMode(6, OUTPUT);    // s2
  pinMode(7, OUTPUT);    // s3  
}

void loop() {
  
  val01 = analogRead(A0)/8;
  if (val01 != lastVal01){
  lastVal01 = val01;
  MIDI_TXPOTES(176,0,val01);
  }
  
  sensorValueH1 = analogReadMux(0);


//CHANNEL 01----------------------------------------------------------------------------------------------------
//SENSOR H1---------------------------------------------------                            
  if (sensorValueH1 > (threshold +100) && KeyLastStateH1 == false) {
  MIDI_TX(128,sensornoteH1,127);
  KeyLastStateH1 = true;
  }
  else if (sensorValueH1 < (threshold -100) && KeyLastStateH1 == true) {
  MIDI_TX(144,sensornoteH1,127);
  KeyLastStateH1 = false;
  }
//CHANNEL 02----------------------------------------------------------------------------------------------------
//SENSOR H1---------------------------------------------------                            
  if (sensorValueH1 > (threshold +100) && KeyLastStateH1 == false && val01 > 10) {
  MIDI_TXTRA(128,sensornoteH1,127);
  KeyLastStateH1 = true;
  }
  else if (sensorValueH1 < (threshold -100) && KeyLastStateH1 == true && val01 > 10) {
  MIDI_TXTRA(144,sensornoteH1,127);  
  KeyLastStateH1 = false;
  }  
}  
  
//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
  status = MESSAGE + midichannel1;
  Serial.write(status);
  Serial.write(PITCH);
  Serial.write(VELOCITY);
}

void MIDI_TXTRA(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
  status = MESSAGE + midichannel2;
  Serial.write(status);
  Serial.write(PITCH);
  Serial.write(VELOCITY);
}

void MIDI_TXPOTES(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
  status = MESSAGE + midichannel3;
  Serial.write(status);
  Serial.write(PITCH);
  Serial.write(VELOCITY);
}

int analogReadMux(int channel){
  int r0 = 0;      //(s0)
  int r1 = 0;      //(s1)
  int r2 = 0;      //(s2)
  int r3 = 0;      //(s3)

  r0 = bitRead(channel,0);    
  r1 = bitRead(channel,1);    
  r2 = bitRead(channel,2);    
  r3 = bitRead(channel,3);      

  digitalWrite(4, r0);
  digitalWrite(5, r1);
  digitalWrite(6, r2);
  digitalWrite(7, r3);
  
  return analogRead(A15);
}

What do you see when you print the values of the variables being used by the if statements ?

I dont know. I have been trying for the last half an hour to print the values and all I get is weird characters or nothing…

anyway, based on the code pasted on #7

  if (sensorValueH1 > (threshold +100) && KeyLastStateH1 == false) {
  MIDI_TX(128,sensornoteH1,127);
  MIDI_TXTRA(128,sensornoteH1,127);
  KeyLastStateH1 = true;
  }
  else if (sensorValueH1 < (threshold -100) && KeyLastStateH1 == true) {
  MIDI_TX(144,sensornoteH1,127);
  MIDI_TXTRA(144,sensornoteH1,127);
  KeyLastStateH1 = false;
  }

triggers both MIDI_TX & MIDI_TXTRA when sensorvalueH1 goes above or below the threshold.

but I would like to trigger MIDI_TXTRA in the same conditions, but also when val01 > 10.

how do I modify the function for this to happen?

camilozk: I dont know. I have been trying for the last half an hour to print the values and all I get is weird characters or nothing....

Let's see some code for that then ;)

septillion: Let's see some code for that then ;)

I would rather learn to do what I am aiming to do, before learning to print. step by step

uhmmm, print is so basic. It's the most easiest way to debug your own problems. So I would start with the basics and learn about Serial.print() It's very easy and can help you debug your own problems in the future ;)

there

unsigned char status;
#define midichannel1 0;
#define midichannel2 1;
#define midichannel3 9;

int threshold = 700;

int val01 = 0;
int lastVal01 = 0;

boolean KeyLastStateH1 = false;

int sensorValueH1;
int sensornoteH1 = 10;


void setup() {
  Serial.begin(31250);
  pinMode(4, OUTPUT);    // s0
  pinMode(5, OUTPUT);    // s1
  pinMode(6, OUTPUT);    // s2
  pinMode(7, OUTPUT);    // s3
}

void loop() {

  val01 = analogRead(A0) / 8;
  if (val01 != lastVal01) {
    lastVal01 = val01;
    MIDI_TXPOTES(176, 0, val01);
  }

  sensorValueH1 = analogReadMux(0);

  //CHANNEL 01----------------------------------------------------------------------------------------------------
  //SENSOR H1---------------------------------------------------
  if (sensorValueH1 > (threshold + 100) && KeyLastStateH1 == false) {
    MIDI_TX(128, sensornoteH1, 127);
    KeyLastStateH1 = true;
  }
  else if (sensorValueH1 < (threshold - 100) && KeyLastStateH1 == true) {
    MIDI_TX(144, sensornoteH1, 127);
    KeyLastStateH1 = false;
  }
  //CHANNEL 02----------------------------------------------------------------------------------------------------
  //SENSOR H1---------------------------------------------------
  if (sensorValueH1 > (threshold + 100) && KeyLastStateH1 == false && val01 > 10) {
    MIDI_TXTRA(128, sensornoteH1, 127);
    KeyLastStateH1 = true;
  }
  else if (sensorValueH1 < (threshold - 100) && KeyLastStateH1 == true && val01 > 10) {
    MIDI_TXTRA(144, sensornoteH1, 127);
    KeyLastStateH1 = false;
  }

  Serial.println(sensorValueH1, DEC);
}


//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
  status = MESSAGE + midichannel1;
  Serial.write(status);
  Serial.write(PITCH);
  Serial.write(VELOCITY);
}

void MIDI_TXTRA(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
  status = MESSAGE + midichannel2;
  Serial.write(status);
  Serial.write(PITCH);
  Serial.write(VELOCITY);
}

void MIDI_TXPOTES(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
  status = MESSAGE + midichannel3;
  Serial.write(status);
  Serial.write(PITCH);
  Serial.write(VELOCITY);
}

int analogReadMux(int channel) {
  int r0 = 0;      //(s0)
  int r1 = 0;      //(s1)
  int r2 = 0;      //(s2)
  int r3 = 0;      //(s3)

  r0 = bitRead(channel, 0);
  r1 = bitRead(channel, 1);
  r2 = bitRead(channel, 2);
  r3 = bitRead(channel, 3);

  digitalWrite(4, r0);
  digitalWrite(5, r1);
  digitalWrite(6, r2);
  digitalWrite(7, r3);

  return analogRead(A15);
}

You're printing to the same place as your midi is being sent, and you're printing every time through the loop.

I know that I am printing every time through the loop. the state of sensorValueH1 changes all the time through the loop

I suppose that printing it to the same place as the midi makes it illegible, so how to then?

This

  Serial.begin(31250);

is an unusual baud rate, and one that is not the default setting for the Serial object. Is your serial monitor set to this rate?

Your second pair of conditions is looking at KeyLastStateH1 to determine if the message needs to be sent, but by the time the code gets there, you first set of conditions has already set it to the other value.

if (sensorValueH1 > (threshold +100) && KeyLastStateH1 == false) {
  MIDI_TX(128,sensornoteH1,127);
  if (val01 > 10) {
    MIDI_TXTRA(128,sensornoteH1,127);
  }
  KeyLastStateH1 = true;
}
else if (sensorValueH1 < (threshold -100) && KeyLastStateH1 == true) {
  MIDI_TX(144,sensornoteH1,127);
  if (val01 > 10) {
    MIDI_TXTRA(144,sensornoteH1,127); 
  }  
  KeyLastStateH1 = false;
}

thanks. I already sorted it out

this baud rate is use for the arduino to behave as a hid. I will try to change it to improve my printing