LED is HIGH if analogValue reached 500 3 times in 5 seconds

How can i turn on the LED if the condition is the analogValue reached greater than 500 3 times in 5 seconds. I kinda new using millis() function and i tried many times what can i do but i still didnt get it work. I hope you help me thanks

const int led = 7;
const int analogPIN = A0;

void setup(){
	Serial.begin(9600);
  	pinMode(led, OUTPUT);
  	pinMode(analogPIN, INPUT);
}

void loop(){
	int analogValue = analogRead(analogPIN);
  	if(analogValue >= 500){
    	
    }

}

school work?

what are your thoughts? how would you do this if you were ask to report if a dog is barking more than 3 times in 1 minute? (in plain English, not in code)

1 Like

You need to store millis for each time the value exceeds 500, if the first and last reading is less than 5 seconds apart, you have a winner.

1 Like

you just have to remember that loop is very fast and it will go round and read analog value again in no time so you need to decide what constitutes as event, a constant presence of value >= 500 for 3 reads or a change from <500 to >=500 3 times

This sketch is to show the State Change Detection for the analog value as killzone_kid explained in the previous post. I give that as a start, but I'm not providing a full answer, you have to add millis() yourself. A good start is the Blink Without Delay page.

Most good sketches need the "State Change Detection" and the "Blink Without Delay" and also "Hysteresis". Could you also read about Hysteresis and change the sketch accordingly ?

// The conundrum of the Barking Dog enigma.
// ----------------------------------------
// (English is not my native language, I had to look up "conundrum" and "enigma")
//
// A analog input does not need a pinMode.
// For millis see the Blink Without Delay
//   https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
// An analog version of the State Change Detection is used.
//   https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
//   

const int ledPin = 7;
const int analogPin = A0;
int barkCount = 0;            // count the barks
int lastBarkValue;

void setup() 
{
  Serial.begin(9600);
	pinMode( ledPin, OUTPUT);
}

void loop()
{
  int barkValue = analogRead( analogPin);

  if( lastBarkValue < 500 && barkValue >= 500)
  {
    // The previous level was below 500, and it just got above or equal to 500
    barkCount++;
    Serial.print( "Bark !  (");
    Serial.print( barkCount);
    Serial.println( ")");
  }
  lastBarkValue = barkValue;
    	
  delay( 100);        // slow down the sketch to reduce serial output messages
}

This sketch in Wokwi (start the simulation, turn the potentiometer-knob a few times):

If the others do not agree that I gave a start, you may bark at me.

Thank you very much to all who replied it make sense to me now.

Thank you also for the headstart because i really dont know if i really should use millis function at first. I will work on it now :slight_smile:

How else were you going to measure five seconds?

it also depends how you read the requirements

is it a sliding window for the last 5 seconds or are you listening for 5 Seconds and reporting, then measuring for the next 5s or listening for 5s once you got triggered ?

depending on how you read this, you'll need a rolling 5s buffer to memorize all the events - or not

if you need the buffer, sampling rate (or min duration of an event) is important, as is the available memory.

Or a rolling 3-sample buffer to timestamp the last three events.

Yes