Switch Case Example clarification

PaulS:

    unsigned long duration = millis() - highAt;

if(duration >= 500 && duration < 2000)
   {
       //Serial.println("between 500 and 1990");
   }
   else if(duration >= 2000 && duration < 3000)
   {
       //Serial.println("above 2999");
   }



Of course, that last commented out Serial.print() statement is nonsense...

Cheers, I have tried to use your code, it works to some extent, but after the operation is executed and "timings" it should reset the timing to ZERO, so that it can run in a loop, but it does not :confused:

// if the sensor input goes above the Redline start measuring the time...
if(Sensor.getProximity() > Redline){ 
    
    duration = millis() - highAt;


    Serial.print(duration/1000);
    
    // check for timings
    if(duration >= 500 && duration < 2000)
    {
        Serial.println("between 500 and 1990");

    }
    else if(duration >= 2000 && duration < 3000)
    {
        Serial.println("above 2999");
    }
    
    else if(Sensor.getProximity() < Redline){
      Serial.print("reset clock");
      duration = 0;
      highAt = 0;
           
    }

    }

You're last else if statement appears to be connected to the wrong if statement.

Thanks, but I am not sure, what do you mean? Can I get a hint more?

Retype your code from scratch. Every time you type if(, put the close parentheses AND the open and close curly brace, on two separate lines, BEFORE you type anything else.

Then, go back and fill in the () and the {}.

That will make typing something like this:

    // check for timings
    if(duration >= 500 && duration < 2000)
    {
        Serial.println("between 500 and 1990");

    }
    else if(duration >= 2000 && duration < 3000)
    {
        Serial.println("above 2999");
    }
   
    else if(Sensor.getProximity() < Redline){
      Serial.print("reset clock");
      duration = 0;
      highAt = 0;
           
    }

a lot less likely. What does the value of Sensor.getProximity() relative to Redline have to do with duration?

That (last) else if statement goes with

if(Sensor.getProximity() > Redline){

it would appear.

It would also appear that you are deliberately ignoring the case where Sensor.getProximity() equals Redline. Why?