Arduino executing if statement against the condition

Hi everybody I have an if statement with 3 conditions
if (erreur == 0 && presencebloc == false && proxsensorstate == LOW)

The problem is that even if I put my variable erreur = 1 the if statement get executed which is against the first condition. I hope one of you guys have seen this before. If you need the full code just ask me. Thx in advance!

Hello

We need the full code, or ideally just enough code to reproduce the problem

Put the complete code because I can not reproduce the problem, if I change the value of any of the 3 variables the condition is not fulfilled (as expected)

The "condition" statements following the "if" are not surrounded by braces, therefore execute always, and not conditionally.

@xfpd: You seem to have a crystal ball...
But you might be right...
Another option is the presence of a ; after the )...

Does the compiler presents some warnings?

After reading @build_1971 post... Two examples:

This is missing the braces "{" and "}" around the statements following the "if" condition test

// xfpd
  erreur = 1;
  if (erreur == 0 && presencebloc == false && proxsensorstate == LOW) // NOT ENCLOSED IN BRACES { }
    Serial.println("This line will print if all conditions evaluate to true");
    Serial.println("This line will always print, even when erreur = 1");

And this one... with the semi-colon "closing" the "if" condition.

// build_1971
  erreur = 1;
  if (erreur == 0 && presencebloc == false && proxsensorstate == LOW); // <- THIS semicolon closes the 'if' condition, so...
    Serial.println("This line will always print, even when erreur = 1");
#include <Stepper.h>
int stepEtiquette = 0;
//const int jogEtiquette = ;
const int jogHoraire = 49;
const int jogAntiHoraire = 50;
//const int jogEjecteur = 13;
const int modeAuto = 51;
const int stepsEtiquette = 400;

Stepper myStepper(stepsEtiquette, A0, A1, A2, A3);

const int proxsensor = 29;
const int balayeuse = 26;
const int ejecteur = 22;
const int verintoupie = 24;
const int verinbloc = 23;
const int spindle = 25;

const int dirpin = 28;
const int steppin = 13;
const int enablestepper = 27;

bool presencebloc = false;
int erreur;

int stepPerRev = 6000;  //
const int delaiEntrePulse = 1600;
const int delaivitesserapide = 550;  //détermine la vitesse du stepper plus le chiffre est gros plus il est lent(delaymicroseconds)
const int durePulse = 5;
const int startangle = 750;
int backlash = 250;
int proxsensorstate = 0;

void setup() {
  myStepper.setSpeed(100);
  Serial.begin(9600);

  pinMode(proxsensor, INPUT_PULLUP);

  pinMode(modeAuto, INPUT_PULLUP);
  //  pinMode(jogEtiquette, INPUT_PULLUP);
  // pinMode(jogEjecteur, INPUT_PULLUP);
  pinMode(jogHoraire, INPUT_PULLUP);
  pinMode(jogAntiHoraire, INPUT_PULLUP);

  pinMode(ejecteur, OUTPUT);
  pinMode(balayeuse, OUTPUT);
  pinMode(verintoupie, OUTPUT);
  pinMode(verinbloc, OUTPUT);
  pinMode(spindle, OUTPUT);
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  pinMode(enablestepper, OUTPUT);

  Serial.begin(9600);
  Serial.println("booting");
}

void loop() {

  while (erreur == 1 || digitalRead(modeAuto) == LOW) {
    modeManuel();
  }
  while (erreur == 0 && digitalRead(modeAuto) == HIGH) {

    proxsensorstate = digitalRead(proxsensor);
    digitalWrite(enablestepper, HIGH);  //désactive la drive du stepper pour pas quelle surchauffe lorsque la machine est en attente

    if (proxsensorstate == LOW) {
      digitalWrite(balayeuse, HIGH);

    } else {
      digitalWrite(balayeuse, LOW);
    }
    if (erreur == 0 && presencebloc == false && proxsensorstate == LOW) {
      digitalWrite(enablestepper, LOW);  //active la drive du stepper pour eviter qu'il bouge si un bloc lacroche legerement pendant qu'il se met en position.
      //etiquette();

      ejection();
      proxsensorstate = digitalRead(proxsensor);
      if (proxsensorstate == HIGH) {  //sert a faire en sorte que si le bloc reste prit dans la chute, la boucle d'éjection se repete
        presencebloc = true;
        delay(50);
        Serial.println(presencebloc);
      }
      // pour evité quil retourne dans la boucle d'éjection a cause du scan time rapide et laisse le verin rentré
    }
    if (proxsensorstate == LOW) {
      digitalWrite(verinbloc, LOW);
      digitalWrite(ejecteur, LOW);
      digitalWrite(spindle, LOW);
      digitalWrite(balayeuse, LOW);
      delay(1000);
      erreur = 1;  //lorque lejection du bloc foire au lieu de reeassayer et de tout briser larduino imcremente la varible erreur qui renvoi au mode manuel ce qui necessite un reset
      delay(1000);
      Serial.println(erreur);
      modeManuel();
    }

    if (presencebloc == true) {
      digitalWrite(verinbloc, HIGH);
      digitalWrite(enablestepper, LOW);
      substartangle();  // met le bloc en angle pour recevoir la toupie
      digitalWrite(verintoupie, HIGH);
      delay(500);  //Pour que les toupies soient bien sur le bloc


      tourRectification();
      digitalWrite(spindle, LOW);
      digitalWrite(verintoupie, LOW);
      remetblocdroit();
      delay(350);
      digitalWrite(verinbloc, LOW);
      delay(350);  //laisse le temps au machoire de remonter pour eviter que le bloc soit laché croche
      resetverinbloc();
      presencebloc = false;  //pour etre sur que la variable presence bloc soit = a 0
      delay(50);
      digitalWrite(enablestepper, HIGH);  // désactive la drive du stepper
      Serial.println(presencebloc);
    } else {
      proxsensorstate = digitalRead(proxsensor);
    }
  }
}

void ejection() {
  digitalWrite(enablestepper, LOW);

  digitalWrite(spindle, HIGH);
  digitalWrite(ejecteur, HIGH);
  Serial.println("ejecteur sort");
  delay(1600);
  digitalWrite(verinbloc, HIGH);
  delay(200);  // pour etre sur que le la blocest bien aggriper par les machoires
  digitalWrite(ejecteur, LOW);
}[quote="doingmybest, post:1, topic:1241695, full:true"]
Hi everybody I have an if  statement with 3 conditions 
 if (erreur == 0 && presencebloc == false && proxsensorstate == LOW)

Here is most of the code I left out some functions that are not problematic.

The problem is that even if I put my variable erreur = 1 the if statement get executed which is against the first condition. I hope one of you guys have seen this before. If you need the full code just ask me. Thx in advance!
[/quote]


No, the compiler does not find problems and the sketch can be transfered in an arduino board.

I just posted a big chunk of the program

If the error were in the piece that you posted, you probably would have found it.

One missing piece is certainly modeManual( ) which is called after error=1. Perhaps it is changing the value back to 0.

Your best approach is to develop a Minimal, Reproducible, Example.

Very often you will find the issue yourself when trying to create the example.

Damn I am pretty sure that you are right in modeManual() I wrote erreur=0 because I wanted to be able to reset the error by flipping the switch to manuel mode. I feel so stupid I am gonna modify the modeManual funtion and keep you informed if it works(pretty sure it will)

Thanks for the help I am sorry I bothered you with such a stupid question. Thanks again!

Yeah no one here ever made such a stupid mistake and just didn't see it for hours. :wink:

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.