Capacitor sensor

Hello,

I am working on a program allowing to display an error message in case of non detection of a metallic object after 2s.

I can't find the condition that allows me to activate my message as soon as I exceed the
2s (in the program, I cannot display the message if the sensor does not change state)

thank you in advance

Program :

void setup () {
Serial.begin (9600);
pinMode (7, INPUT);
pinMode (13, OUTPUT);

}
void loop () {
int start = millis (); // start of time counting in ms

while ((digitalRead (7) == HIGH)); // NC detector

int end = millis (); // end of time counting in ms
int duration = end-start; // calculate the time of no object in ms

if (duration> 2000) {

Serial.println (duration);
counter ++;
Serial.println (counter);
}
if (counter! = 0) {

Serial.println ("error");
// tone (13,10);
// delay (100);
// noTone (13);
counter = 0; // reset the counter to 0 after exceeding the condition
}
}

}

millis() returns an unsigned long (uint32_t)

Hi @zakmz07 ,

Read the topic " How to get the best out of this forum ";

When posting sketch in topic format it, and use </> tags;

Try this sketch ande read comments

int counter = 0;     // counter definition  <<  --------------------

void setup () {
  Serial.begin (9600);
  pinMode (7, INPUT);
  pinMode (13, OUTPUT);

}
void loop () {
  unsigned long start = millis (); // start of time counting in ms           //  uint32_t  format  <<  --------------------

  while ((digitalRead (7) == HIGH)); // NC detector

  unsigned long end = millis (); // end of time counting in ms           //  uint32_t  format  <<  --------------------
  unsigned long duration = end - start; // calculate the time of no object in ms               //  uint32_t  format  <<  --------------------

  if (duration > 2000) {

    Serial.println (duration);
    counter ++;                         //  counter not defined  < <  ---------------
    Serial.println (counter);
  }
  //if (counter! = 0) {                   // Wrong format, space between! e = not allowed
  if (counter!= 0) {  
    Serial.println ("error");
    // tone (13,10);
    // delay (100);
    // noTone (13);
    counter = 0; // reset the counter to 0 after exceeding the condition
  }
}

//}                 //  Deleted.. Extra {

This post looks the same as this one.

thank you for helping me