# Simple question about a boolean

hello,

Does someone know how to program this:
if (boolean == true during three seconds)
{
...
}

Thanks a lot

Did you mean:

"at any time during a specific three second interval"

or

"continuously over a specific three second interval"

or

"continuously for any three consecutive seconds"

You have to know exactly what you mean to write code.

Excuse me that it was not clear.

I want to say:
If the boolean is three seconds or more true, than the action must be taken.

So:

If (boolean == true (for more or for three seconds) )
{
counter ++;
}

you can do something like this,

if(boolean == true && ((millis() - trigger_time) % 1000) >= 3) counter++; // if boolean is true and the difference of when it was triggered(changed state) to current time is greater or equal to 3 seconds.

Edit: first one would not work correctly, Sorry.

thanks a lot

I will try it now and let you know of it works.

Joris_DP:
If the boolean is three seconds or more true.

``````static unsigned long timerStartTime = 0;

if (boolean && (millis() - timerStartTime > 3000))
counter++;
else
timerStartTime = millis();  // Re-start the timer if boolean is false
``````

Joris_DP:
Excuse me that it was not clear.

I want to say:
If the boolean is three seconds or more true, than the action must be taken.

So:

``````If (boolean == true (for more or for three seconds) )
``````

{
counter ++;
}

That's still not clear. Do you only want to increment counter once or all the time that boolean== true for more than 3 seconds. What if it's true for 4 seconds...5 seconds...6 seconds? How many times do you want to increment counter?

``````static unsigned long timerStartTime = 0;
``````

if (boolean && (millis() - timerStartTime > 3000))
counter++;
else
timerStartTime = millis();  // Re-start the timer if boolean is false

I don't think that's going to work. Even if boolean is true, it will fail the second half of the if and reset the start time every iteration.

What are you trying to do?

If you are trying to test for a logic condition (button held down / some kind of analog input over a threshold for 3 seconds) , you need to record the time the condition went true, then check it as often as possible until

1. it goes false OR
2. 3 seconds lapses

I don't think that's going to work. Even if boolean is true, it will fail the second half of the if and reset the start time every iteration.

Oops. You're right. Change it to:

``````static unsigned long timerStartTime = 0;

if (boolean expression) {
if (millis() - timerStartTime > 3000)
counter++;
}
else
timerStartTime = millis();  // Re-start the timer if boolean is false
``````

..what if the "booloean expresion" goes false and then back true outside the above "if-else" ?

pito:
..what if the "booloean expresion" goes false and then back true outside the above "if-else" ?

If the signal changes that fast the OP should be using hardware to detect pulses.

johnwasser:

I don't think that's going to work. Even if boolean is true, it will fail the second half of the if and reset the start time every iteration.

Oops. You're right. Change it to:

``````static unsigned long timerStartTime = 0;
``````

if (boolean expression) {
if (millis() - timerStartTime > 3000)
counter++;
}
else
timerStartTime = millis();  // Re-start the timer if boolean is false

In each iteration, where both if statements are true, counter will be incremented. Its value will be almost meaningless, other than a count of the iterations since the 3 second mark was reached. The OP wasn't clear about this, but I suspect that's not what he wants.

Henry_Best:

Then the OP should have said what they want.

Thanks a lot, but it doesn't work.
I'm trying to make it clear by giving the whole code.
Excuse me, but the words who are used in the code are written in dutch.

My project is to navigate a robot between rows.
The robot can ride automatically with the help of a laserscanner.
The laserscanner gives the distance and the angel to an object.
Everythings works, but there is only one problem.
When the first row is ended, the robot have to turn right.
When the second row is ended, the robot have to turn left.
You can see it on the picture.

meetWaarde = the distance from the object to the laserscanner
i = the angle
geenRijRechts means: The row is ended

Here is the problem... If the first row is ended, the robot has to turn right !
First of all I thaugt to work with a counter.
If (geenRijRechts == true)
{
counter = counter ++;

if (counter % 2 == 0)
{ turnLeft;}

else
{turnRight;}

But it does'nt work because the values of the scanner are reading continuously.
And the counter goes continue ++, and the robot wil continuously turn left and turn right.
With the counter who are recommended have I the same problem.
Can someone help?

``````void loop()
{
//overslaan schaal offset, is altijd nul

//lees start hoek
//Serial.print("start hoek ");
//Serial.println(startHoek/10000.0);

//lees stap hoek
//Serial.print("stap hoek ");
//Serial.println(stapHoek/10000.0);

//lees data hoeveelheid
//Serial.print("data hoeveelheid is gelijk aan ");
//Serial.println(dataCount);

//lees data
for ( unsigned long i = 0; i < dataCount; ++i )
{

if (meetWaarde > 1200)
{
if(i > 270)
{
geenRijRechts = true;
}
}

if (meetWaarde <= 1200 && meetWaarde > 600)
{
if (i > 270)
{
teVerVanRijRechts = true;
}
}

if (meetWaarde <= 600 && meetWaarde > 350)
{
if (i > 270)
{
rijRechts = true;
}
}

if (meetWaarde <= 350 && meetWaarde > 10)
{
if (i > 270)
{
botsingRijRechts = true;
}
}
//}

}

}
}
}
}
}
}

client.flush();
// Serial.println(micros()-startTime);

if(botsingRijRechts == true) //&& botsingRijLinks == false)
{
snelheid = maneuverSnelheid;
botsingRijRechts = false;
//Serial.println("botsing rij rechts");
}

if(rijRechts == true)
{
snelheid = maneuverSnelheid;
rechtdoor();
rijRechts = false;
//Serial.println("rechtdoor");
}

static unsigned long timerStartTime = 0;

if (geenRijRechts == true) {
if (millis() - timerStartTime > 3000)
{
eindeRij = eindeRij + 1;
snelheid = maneuverSnelheid;
Serial.println(eindeRij);
if (eindeRij % 2 == 0)
{
naarRechts();
Serial.println("bochtNaarRechts");
geenRijRechts = false;
}
else
{
geenRijRechts = false;
}

}

}
else
{
timerStartTime = millis();  // Re-start the timer if boolean is false
}

if(teVerVanRijRechts == true)
{
snelheid = maneuverSnelheid;
naarRechts();
//Serial.println("te ver van rij rechts");
teVerVanRijRechts = false;
}

else
{

uitlezenXpixels = aantalAardbeien * 3 + 1;

clearSerialBuffer();
Serial.print("#S|LEESWITHE|[");
Serial.print(itoa((2), buffer, 10));
Serial.println("]#");

{
stilstaan();
}
}
}

}

void clearSerialBuffer(){
char dummy;
while (Serial.available())  {  //Also check for timeout here, also leave space for '\0' at the end of your C-String
delay(10); //slight delay to allow the buffer to fill up
}
}

void readSerialString (char *strArray,long timeOut) {
// Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
long startTime=millis();
int i = 0;

while (!Serial.available()) {
if (millis()-startTime >= timeOut) {
return;
}
}
while (Serial.available() && i < (serInLen-1) && (millis()-startTime) < timeOut) {  //Also check for timeout here, also leave space for '\0' at the end of your C-String
i++;
delay(10); //slight delay to allow the buffer to fill up
}
strArray[i]='\0';
}
``````

Can't auto-format. Too many right brackets.

Can't compile. "sketch_may20b:3: error: 'readLength' was not declared in this scope"

The code is too large.
But I have solve the problem.
Thanks a lot.