Reset int variable

I am a using an ultrasonic sensor to detect how long ago an object was placed. I need to reset the "countUp" variable. My code is provided bellow if anyone has a solution?

#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
int countUp;

void setup() {
   Serial.begin(9600);
}

void loop() {

   delay(1000);
   Serial.print(countUp);
   Serial.println(" Seconds Since Object Placed");
   
   if (sr04.Distance() < 5)
{
   countUp++;
}

else
{
  int countUp = 0;
  Serial.print("Reset");
}

}

else
{
countUp = 0;
Serial.print("Reset");
}

Sorry, but what changed, if it is the:

else
{
  int countUp = 0;
  Serial.print("Reset");
}

}

to

else
{
  int countUp = 0;
  Serial.print("Reset");
}

I got this error: expected '}' at end of input

That creates a completely new countUp variable that is different from the countUp variable declared before setup(). It only lives (is in scope) in the else structure.

See here for more on scope.

Error code: 'countUp' was not declared in this scope

int countUp = 0; declares a new variable by the name countUp and assigns the value 0.
countUp = 0; assigns a value of 0 to a variable named countUp.

All that you needed to do remove the int from in front of countUp in the else structure.

#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
int countUp;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  delay(1000);
  Serial.print(countUp);
  Serial.println(" Seconds Since Object Placed");

  if (sr04.Distance() < 5)
  {
    countUp++;
  }

  else
  {
    countUp = 0;  // remove the int 
    Serial.print("Reset");
  }
}

You were missing the } to close the loop() function.

Thanks so much!

...which is exactly why I said:

When you compile and run it, it doesn't perform like you want...

econjack