[SOLVED]Need advice on creating IF statement

I have to create a IF statement that does the following:
IF both btnNow and btnPreve are equale AND the timer has elapsed five seconds execute PING otherwise execute PONG.
But my code wont wait the five seconds.
Any suggestions?

int btnNow = 5;
int btnPrev = 5;
int count = 0;
unsigned long tmrPrev = 0;

void setup() {
  Serial.begin(9600);
  tmrPrev = millis();
}

void loop() {
  if (btnNow == btnPrev && (millis() - tmrPrev <= 5000UL)) {
    Serial.print(count++);
    Serial.print("=>");
    Serial.println("PING");
    delay(600);
    tmrPrev = millis();
  } else {
    Serial.print(count++);
    Serial.print("=>");
    Serial.println("PONG");
    delay(600);
  }
}
if (btnNow == btnPrev && (millis() - tmrPrev >= 5000UL)) {

Or even

A step further, even ppl who know what they are doing might use parentheses to make it clear to all what the intended expression is.

a7

This makes that time will be never reached. Always new millis ().

The time will be reached, but with tmrPrev = millis(); after a 600ms delay, the code will execute every 5.6 seconds rather than every 5 seconds. Better would be to use tmrPrev += 5000 to avoid this "drift" (I'd also move delay(600); out of each branch of the if to the bottom of loop).

Ah. Yes. Of course, when buttons are not equal .

Maybe I am not clear enough, :slight_smile: btnNow and btnPrev MUST be equal and the instructions will then be executed after five seconds delay

What does your code do that is different from what you want it to do? Also, have you already tried the change suggested in post #2?

Your requirements are not at all clear. What are you trying to do?

You have declared your variables btnNow and btnPrev to be the same so your if statement will always be true after every 600 millisecond delay and will never execute the else if statement. Not sure what you're looking for, but I made a few changes to your code to change btnNow and btnPrev to not being equal after 5 seconds.

int btnNow = 5;
int btnPrev = 5;
int count = 0;
unsigned long tmrPrev = 0;

void setup() {
  Serial.begin(9600);
  tmrPrev = millis();
}

void loop() {
  if (btnNow == btnPrev && (millis() - tmrPrev >= 5000UL)) {
    Serial.print(count++);
    Serial.print("=>");
    Serial.println("PING");
    btnPrev = 0;
    tmrPrev = millis();
  }

  else if (btnNow != btnPrev && (millis() - tmrPrev >= 5000UL)){
    Serial.print(count++);
    Serial.print("=>");
    Serial.println("PONG");
    btnPrev = 5;
    tmrPrev = millis();
}
}

5 seconds from when?

UPD. @christop btnNow and btnPrev are 5, so equal always, millis() - tmrPrev <= 5000UL is true, so will tmrPrev = millis(); executed and again and again.
I think the answer to OP is:

const byte btnNow = 5;
const byte btnPrev = 5;
unsigned int count = 0;
unsigned long tmrPrev = 0;

void setup() {
  Serial.begin(9600);
  tmrPrev = millis();
}

void loop() {
  if (btnNow == btnPrev && (millis() - tmrPrev <= 5000UL)) {
    Serial.print(count++);
    Serial.print("=>");
    Serial.println("PING");
    delay(600);
  } else {
    Serial.print(count++);
    Serial.print("=>");
    Serial.println("PONG");
    delay(600);
  }
}

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