Arduino with 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
}
}

}

Welcome to the forums. Please read the sticky post at the top to learn how to properly format and post your code. It will help people help you. After doing this, go back and edit your post to clean up your code.

A couple of items:
millis() returns 'unsigned long' not 'int' values. ALL variables associated should be that type.

How do you have your sensor wired up? Are you sure it is actively driving that pin HIGH and LOW? Some sensors only pull the line low and you have to declare the pin as INPUT_PULLUP to make it HIGH when nothing is detected

pls post whole program

millis returns unsigned long, not int.

I already told you this in your other topic.

How come "inductif" in French is translated as "capacitive"?
(Yes, this is also in the French section)

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)
it's a inductive sensor ( NC)
thank you in advance

//////////////////////////////////////////////////////////
int counter = 0; //counter

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

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

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

unsigned long fin = millis (); // end of time counting in ms
unsigned long duration = fin - 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");
counter = 0; // reset the counter to 0 after exceeding the condition
}
}
////////////////////////////////////////////////////////

pls edit your posts, select whole program text and mouse click the button which look like "</>" . it make so that i can copy it with one click. otherwise i must hold mouse button and scrolling down to marking text.

This line prevents you from doing anything until pin 7 goes LOW.

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

You should check pin 7 each time the loop() function is called. Something like this:

void loop ()
{
  static unsigned long start = millis (); // start of time counting in ms

  if (digitalRead (7) == HIGH)
  {
    // no object detected
    if (millis() - start > 2000)
    {
      Serial.println ("error");
      start = millis ();
    }
  }
  else
  {
    // Object detected
    start = millis ();
  }
}

I don't know exactly what you want to do but this code will just print out the error every 2 seconds until an object is detected. If an object is detected it really doesn't do anything.