Multiple scenarios overlap while using "if" statments

Hi there, i am trying to satisffy for each scenarios by using "if" statments. But it keeps on overlapping.

#include <SoftwareSerial.h> 
SoftwareSerial MyBlue(10, 11); // RX | TX 
int flag = 0; 
int ForceSens1 = A0; // S1
int ForceSens2 = A1; // S2
int ForceSens4 = A2; // S4


int buzzer = 13;
int timeLimit;
const int motor= 3;

void setup()
{
  MyBlue.begin(9600); 
 pinMode (buzzer, OUTPUT);
 pinMode (ForceSens1, INPUT);
 pinMode (ForceSens2, INPUT);
 pinMode (ForceSens4, INPUT);
 pinMode (motor, OUTPUT);
  Serial.begin(115200);
  Serial.println("Ready to connect\nDefualt password is 1234 or 0000");
  Serial.println("How long will you sit for?");
 while (!Serial.available());
pinMode(motor, OUTPUT);
 timeLimit = Serial.parseInt();
}



 void loop(){   
int forceSensReading1 = analogRead(ForceSens1);
int forceSensReading2 = analogRead(ForceSens2);
int forceSensReading4 = analogRead(ForceSens4);

  static int timer = millis();
  if ((millis()- timer) > timeLimit){
digitalWrite(motor, HIGH);
delay(4000);
digitalWrite(motor, LOW);
delay(5000);
    timer = millis();
  }
  {
if (forceSensReading1>1000&&forceSensReading2>1000&&forceSensReading4>1000)//Optimal (perfect posture) // intended neither
Serial.println("Optimal Seating");
}
{
 if ( forceSensReading1>1000&&forceSensReading2>1000&&forceSensReading4<1000)//too left (FS 4 (S4) will decrease from optimal) FS 1 might increase more than optimal
buzzerOn();
Serial.println("too left");
}
{
 if ( forceSensReading1>1000&&forceSensReading2<1000&&forceSensReading4>1000)//too right (FS 1 (S2) will decrease from optimal) FS 4 might increase more than optimal
buzzerOn();

Serial.println("too right");
}
//{
// if ( forceSensReading1>820&&forceSensReading2>400&&forceSensReading4>1020&&forceSensReading5>550&&forceSensReading6>550)//Edge of chair(FS 2,5,6 (S1,B1,B2) will in decrease lower than optimal)
//buzzerOn();

//Serial.println("User is seating at the edge of chair");
//}
{
 if ( forceSensReading1<1000&&forceSensReading2<1000&&forceSensReading4<1000)// If all values lower than optimal, nobody seating ayy
Serial.println("User not present");
}
return;
}
void buzzerOn()
{
digitalWrite(3, HIGH);
tone (buzzer, 450);
delay(500);
noTone (buzzer);
delay (500);
}

Here is the outcome on serial monitor
photo_2022-02-07_05-30-03

If the if statements conflict, then the logic is wrong.

One approach would be to draw a diagram with all the possibilities for input, and the desired outcomes. Then write the if statements accordingly.

The 'if' goes BEFORE the open bracket:

  if (forceSensReading1 > 1000 &&
      forceSensReading2 < 1000 && 
      forceSensReading4 > 1000) //too right (FS 1 (S2) will decrease from optimal) FS 4 might increase more than optimal
  {
    buzzerOn();
    Serial.println("too right");
  }

The way you wrote it, the 'if' was only controlling the 'buzzerOn();' and not the 'Serial.println()'

seems like there are too many braces and the logic can be more embedded

conside

#include <SoftwareSerial.h > 
SoftwareSerial MyBlue(10, 11); // RX | TX
int flag = 0;
int ForceSens1 = A0; // S1
int ForceSens2 = A1; // S2
int ForceSens4 = A2; // S4
int buzzer = 13;
unsigned long timeLimit;
const int motor= 3;

void setup()
{
    MyBlue.begin(9600);
    pinMode (buzzer, OUTPUT);
    pinMode (ForceSens1, INPUT);
    pinMode (ForceSens2, INPUT);
    pinMode (ForceSens4, INPUT);
    pinMode (motor, OUTPUT);
    Serial.begin(115200);
    Serial.println("Ready to connect\nDefualt password is 1234 or 0000");
    Serial.println("How long will you sit for?");
    while (!Serial.available());
    pinMode(motor, OUTPUT);
    timeLimit = Serial.parseInt();
}

void buzzerOn()
{
    digitalWrite(3, HIGH);
    tone (buzzer, 450);
    delay(500);
    noTone (buzzer);
    delay (500);
}

void loop(){
    int f1 = analogRead(ForceSens1);
    int f2 = analogRead(ForceSens2);
    int f4 = analogRead(ForceSens4);

    static int timer = millis();
    if ((millis()- timer) > timeLimit){
        digitalWrite(motor, HIGH);
        delay(4000);
        digitalWrite(motor, LOW);
        delay(5000);
        timer = millis();
    }

    //Optimal (perfect posture) // intended neither
    if (f1 > 1000)  {
        if (f4 > 1000)
            if (f2 > 1000)
                Serial.println("Optimal Seating");
            else  {
                buzzerOn();
                Serial.println("too right");
            }
        else  {
            buzzerOn();
            Serial.println("too left");
        }
    }

    else if (f2 < 1000 && f4 < 1000)
        Serial.println("User not present");

    return;
}

this is pretty much if (0 > whatevervalueentered)

Oh wow, thank you for the suggestion, it seems that was the issue. Thank you :slight_smile: