Issue with my Arduino code - delay function

I have an issue with my attached code which is that after I turn on a buzzer when the level is above 4 , then after the level become less than 4 it do not turn off directly. It takes the whole delay time before turned off ! I want to turn it off directly if the level less than 4

'''''''
const int buzzer = 8;
int sensor_value = 0;
int abs_value = 0;
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(sensor_value, INPUT);

Serial.begin(9600);

}

void loop() {
// Read Sound sensor
sensor_value = analogRead(A0);
abs_value = abs(sensor_value);
int level = map(abs_value, 0, 1023, 0, 10);//Map to 0 to 10 level
Serial.println(level);
if (level > 4) {
delay(20000); // waits for 20 second
if (level > 4){
digitalWrite(buzzer,HIGH);
return;
}
else {
digitalWrite(buzzer, LOW);
}

}

    else if (level < 4){
digitalWrite(buzzer, LOW);

}

}
''''''''

Please post your code properly, in code tags. These things ... </>

What is is supposed to do? Only sound the buzzer if at > Level4 for more than 20 seconds?

If yes, then you need to use millis() to keep track of how long it has been > Level 4.

Hi, @myt_13
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

When you use the delay function, you basically stop the controller for that length of time, it does nothing, not even read inputs or change outputs.
It is termed a blocking function.

You need to look at the Example in the IDE called "Blinkwithoutdelay", it will show how to delay without stopping/blocking you code.

Also look at how you use if statements, you do not need the return statement.
A suggestion, before posting your code, press [ CTRL ] [ T ], this will auto format your code anmd make it easier to use.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

1 Like

little more involved than i expectde

#undef MyHW
#ifdef MyHW
#define Delay   3000
const int buzzer = LED_BUILTIN;

#else
#define Delay   20000
const int buzzer = 8;
#endif

enum  { Off = LOW, On = HIGH };

bool          flag;
unsigned long msecLst;

void loop () {
    unsigned long msec = millis ();

    int level = map (analogRead (A0), 0, 1023, 0, 10);//Map to 0 to 10 level
    Serial.println (level);

    if (level > 4) {
        if (! flag)  {
            flag = true;
            msecLst = msec;
        }
        else if (Off == digitalRead (buzzer))  {
            if ((msec - msecLst) > Delay)
                digitalWrite (buzzer, On);
        }
    }
    else  {
        flag = false;
        digitalWrite (buzzer, Off);
    }
}

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

may be easier to see as a state machine

#undef MyHW
#ifdef MyHW
#define Delay   3000
const int buzzer = LED_BUILTIN;

#else
#define Delay   20000
const int buzzer = 8;
#endif

enum  { Off = LOW, On = HIGH };

enum { ST_OFF, ST_TRIG, ST_ON };
int           state = ST_OFF;
unsigned long msecLst;

void loop () {
    unsigned long msec = millis ();

    Serial.print (state);
    Serial.print (" ");

    int level = map (analogRead (A0), 0, 1023, 0, 10);//Map to 0 to 10 level
    Serial.println (level);

    switch (state) {
    case ST_OFF :
        if (level > 4)  {
            msecLst = msec;
            state   = ST_TRIG;
        }
        break;

    case ST_TRIG :
        if (level <= 4)  {
            state   = ST_OFF;
        }
        else if ((msec - msecLst) > Delay)  {
            digitalWrite (buzzer, On);
            state   = ST_ON;
        }
        break;

    case ST_ON :
        if (level <= 4)  {
            digitalWrite (buzzer, Off);
            state   = ST_OFF;
        }
        break;
    }
}

void setup () {
    Serial.begin (9600);
    pinMode (buzzer, OUTPUT);
    digitalWrite (buzzer, Off);
}

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

write down an example with example-numbers that show a typical case

10:00:00 level is 2 (which is below threshold value 4)
10:00:07 level is 6 (which is above threshold value 4) ==> buzzer ON
10:??? ???
10:??? ????
10:??? ????

This list must have an entry for each change of any detail that is important.

By this description your potential helpers get a clear picture of the functionality you want to have.

And only if this is really clear suitable suggestions can be made.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.