Multiple OR Conditions in an IF Statement

Hello there,

I’m having troubles with multiple OR conditions in an IF statement not producing the result I desire.

So the basic idea is for the following statement to return a upStatus of HIGH when ANY of the 4 OR’ed statements are true:

 if ((up1 >= up1cal) || (up2 >= up2cal) || (up3 >= up3cal) || (up4 >= up4cal))
 {
   upStatus = HIGH;
 }

The code works great if I use only 1 OR condition, but as soon as I use more than 1, it doesn’t work.

So for example:

 if ((up1 >= up1cal) || (up2 >= up2cal))
 {
   upStatus = HIGH;
 }

The above code works, but the first one doesn’t ever return a true value, regardless if any of the greater than or equal to conditions are true.

Am I missing something?

Full Code:

#include <Joystick.h>
#include <SPI.h>
#include <EEPROM.h>

// Create Joystick
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
 4, 0,                  // Button Count, Hat Switch Count
 false, false, false,     // X and Y, but no Z Axis
 false, false, false,   // No Rx, Ry, or Rz
 false, false,          // No rudder or throttle
 false, false, false);  // No accelerator, brake, or steering

//Debug Variables
byte bug = 0;
byte bug2 = 0;

//Button Status Variables
byte upStatus=1;
byte upStatusPrev=1;
byte leftStatus=1;
byte leftStatusPrev=1;
byte downStatus=1;
byte downStatusPrev=1;
byte rightStatus=1;
byte rightStatusPrev=1;

//Sensor Variables
float up1;
float up2;
float up3;
float up4;
int down1;
int down2;
int down3;
int down4;
int left1;
int left2;
int left3;
int left4;
int right1;
int right2;
int right3;
int right4;

//Calibration Variables
float up1cal = 1.50;
float up2cal = 1.50;
float up3cal = 1.50;
float up4cal = 1.50;
int down1cal;
int down2cal;
int down3cal;
int down4cal;
int left1cal;
int left2cal;
int left3cal;
int left4cal;
int right1cal;
int right2cal;
int right3cal;
int right4cal;
float hys=0.05;
float calibration = 0;

//Set SlaveSelect Pins
int SS1 = 6;
int SS2 = 7;

// for incoming serial data
int value = 0;

void setup() {
 //Set SlaveSelect Pin Directions
 pinMode(SS1, OUTPUT);
 pinMode(SS2, OUTPUT);

 //debug setup
 pinMode(8, INPUT);
 pinMode(9, INPUT);

 //Set SS Pins High (Initial State)
 digitalWrite(SS1,HIGH);
 digitalWrite(SS2,HIGH);
 
 // start the SPI library:
 SPI.begin();
 
 //initialize joystick library
 Joystick.begin();

 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode(SPI_MODE0);

 //Set SPI Clock Speed
 SPI.setClockDivider(SPI_CLOCK_DIV64);  
}

void loop() {
 
 //read up arrow sensors
 up1=readADC(0);
 up2=readADC(1);
 up3=readADC(2);
 up4=readADC(3);

 
 //read down arrow sensors
 down1=readADC(4);
 down2=readADC(5);
 down3=readADC(6);
 down4=readADC(7);
 
 //read left arrow sensors
 left1=readADC2(0);
 left2=readADC2(1);
 left3=readADC2(2);
 left4=readADC2(3);
 
 //read right arrow sensors
 right1=readADC2(4);
 right2=readADC2(5);
 right3=readADC2(6);
 right4=readADC2(7);
 
 Serial.print("Sensor1:");
 Serial.println(up1);
 Serial.print("Sensor2:");
 Serial.println(up2);
 Serial.print("Sensor3:");
 Serial.println(up3);
 Serial.print("Sensor4:");
 Serial.println(up4);

 //UP Arrow input compared to sensitivity value
 if ((up1 >= up1cal) || (up2 >= up2cal) || (up3 >= up3cal) || (up4 >= up4cal))
 {
   upStatus = HIGH;
 }

 if((up1 <= (up1cal-hys)) || (up2 <= (up2cal-hys)) || (up3 <= (up3cal-hys)) || (up4 <= (up4cal-hys)))
 {
   upStatus = LOW;
 }

   
 //UP ARROW PRESSED
if (upStatus!=upStatusPrev && upStatus==HIGH)
{
   Joystick.pressButton(0);
   digitalWrite(0,HIGH);
   upStatusPrev=upStatus;
}
//UP ARROW RELEASED
if (upStatus!=upStatusPrev && upStatus==LOW)
{
   Joystick.releaseButton(0);
   digitalWrite(0,LOW);
   upStatusPrev=upStatus;
}

 //If serial is available, enter config mode
 if (Serial.available() >= 1) {
   // read the incoming byte:
   calibration = Serial.parseInt();

   if(calibration >= 1)
   {
     Serial.println("Calibrating... DO NOT step on arrows.");
     delay(5000);
     up1cal = readADC(0);
     up2cal = readADC(1);
     up3cal = readADC(2);
     up4cal = readADC(3);
     down1cal = readADC(4) + 50;
     down2cal = readADC(5) + 50;
     down3cal = readADC(6) + 50;
     down4cal = readADC(7) + 50;
     delay(1000);
     
     up1cal += (calibration/100);
     up2cal += (calibration/100);
     up3cal += (calibration/100);
     up4cal += (calibration/100);
     Serial.println("Sensitivity Entered"); //Sensitivity is between 1 and 100 (1 being most sensitive)
     Serial.println(up1cal);
     Serial.println(up2cal);
     Serial.println(up3cal);
     Serial.println(up4cal);
     delay(5000);
   }
   
   }
} //end of loop




float readADC(int channel)  {

 //Refer to MCP3208 datasheet (Fig 6-1)
 byte adcPrimaryConfig = 0b00000110;
 byte adcPrimaryByteMask = 0b00001111; 
 byte adcSecondaryConfig = channel << 6;
 noInterrupts();
 digitalWrite(SS1,LOW);
 SPI.transfer(adcPrimaryConfig);
 byte adcPrimaryByte = SPI.transfer(adcSecondaryConfig);
 byte adcSecondaryByte = SPI.transfer(0);
 digitalWrite(SS1,HIGH);
 interrupts();
 adcPrimaryByte &= adcPrimaryByteMask;
 //shifts the 4 LSB of the primary byte of the 12bit digital value and OR operation to the secondary byte, which combines both into a 12bit value from the MCP3208 ADC
 int digitalValue = (adcPrimaryByte << 8) | adcSecondaryByte; 
 float value = (float(digitalValue) * 2.150) / 4096.000;
 return value;
}

int readADC2(int channel)  {

 //Refer to MCP3208 datasheet (Fig 6-1)
 byte adcPrimaryConfig = 0b00000110;
 byte adcPrimaryByteMask = 0b00001111; 
 byte adcSecondaryConfig = channel << 6;
 digitalWrite(SS2,LOW);
 SPI.transfer(adcPrimaryConfig);
 byte adcPrimaryByte = SPI.transfer(adcSecondaryConfig);
 byte adcSecondaryByte = SPI.transfer(0x00);
 digitalWrite(SS2,HIGH);
 adcPrimaryByte &= adcPrimaryByteMask;
 //shifts the 4 LSB of the primary byte of the 12bit digital value and OR operation to the secondary byte, which combines both into a 12bit value from the MCP3208 ADC
 int digitalValue = (adcPrimaryByte << 8) | adcSecondaryByte; 
 //float value = (float(digitalValue) * 2.150) / 4096.000; -> USE THIS IF NEED DECIMAL NUMBER
 return digitalValue;
}
  1. Edit your post and add code tags to make it forum compatible.
  2. Describe exactly how it “doesn’t work”.

Sorry about that, I've edited the original post.

It’s probably the lines that follow those:

 if((up1 <= (up1cal-hys)) || (up2 <= (up2cal-hys)) || (up3 <= (up3cal-hys)) || (up4 <= (up4cal-hys)))
 {
   upStatus = LOW;
 }

Perhaps one or more of those conditions is true?

Have you tried a debug print of 'upstatus'? How are you determining the outcome of the if statement?

christop:
It’s probably the lines that follow those:

 if((up1 <= (up1cal-hys)) || (up2 <= (up2cal-hys)) || (up3 <= (up3cal-hys)) || (up4 <= (up4cal-hys)))

{
  upStatus = LOW;
}




Perhaps one or more of those conditions is true?

I think you hit the nail on the head, thanks! Can’t believe I missed that haha!