How to Stop a audio funcion inside loop after 10 seconds

A help please.

I am making a temperature monitor, which is no more than a serial value reader and which should only show on the display if the temperature is above or below the point.

however decide to implement an audio function to alert me when it is off limits.
So far so good.

However I don’t want the audio to play for more than about 10 seconds, when the alarm occurs on any of the imputs.
use the millis or count function?
I can’t use either :frowning:

EMS_Silvio_Arduino_alterado_forum.ino (3.88 KB)

Hello X-Trem,

Please read 'how to use this forum - please read' (plenty of clues in the title) the post your code as per item #7.

Yes, use millis.

Thanks Perry

I will do that more closely

But I researched a lot, even before making the post and find nothing I can use :confused:

Find nothing I can use.

It's difficult to know what to tell you without just giving you the answer, which I won't do. First is to post your code as per the forum instructions, not as an attachment.

Second is to show us what you tried and tell us what it did that was different to what you wanted.

I think that is te correctaawy

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const int sensorPin = A0;  //the number of the sensor pin
const int sensorPin1 = A1;  //the number of the sensor1 pin
const int sensorPin2 = A2;  //the number of the sensor2 pin
const int sensorPin3 = A3;  //the number of the sensor3 pin
const int sensorPin6 = A6;  //the number of the sensor6 pin
const int sensorPin7 = A7;  //the number of the sensor7 pin




#define OLED_RESET 12
Adafruit_SSD1306 display(OLED_RESET);


void setup()
{

    Serial.begin(9600); //sets serial port for communication





    pinMode(sensorPin, INPUT);   //initialize the sensor pin as an input
    pinMode(sensorPin1, INPUT);   //initialize the sensor1 pin as an input
    pinMode(sensorPin2, INPUT);   //initialize the sensor2 pin as an input
    pinMode(sensorPin3, INPUT);   //initialize the sensor3 pin as an input
    pinMode(sensorPin6, INPUT);   //initialize the sensor6 pin as an input
    pinMode(sensorPin7, INPUT);   //initialize the sensor7 pin as an input
    pinMode(12, OUTPUT);   //initialize the pin 12 to all alarms
    pinMode(13, OUTPUT);   //initialize the pin 13 to CO alarm

    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
    // init done


    

}

void loop()

{


    {
        int sensorValue = analogRead(A7);   // alterar de acordo com a entrada analogica que se quer ler
        // print out the value you read:
        Serial.println(sensorValue);
        delay(1);
   

    // entrada A0 para EGT1
    }

    int sensorStatus = analogRead(sensorPin);   //read the status of the sensor value

    
    if (sensorStatus >= 305)
    {

        display.setTextColor(WHITE);
        display.setCursor(1, 30);
        display.setTextSize(2);
        display.println("EGT 1 HIGH");
        display.display();
        delay(300);
        digitalWrite(12, HIGH); // valor para acender o led

    }

    if (sensorStatus < 305) // valor para apagar o led

    {

        digitalWrite(12, LOW);

    }

    if (sensorStatus >= 305)
   

    {

        // turn off tone function for pin 8:
        noTone(6);
        // play a note on pin 6 for 200 ms:
        tone(6, 440, 200);
        delay(200);
        tone(6, 600, 200);

    }

    if (sensorStatus <= 301)
    {

        display.setTextColor(WHITE);
        display.setCursor(1, 30);
        display.setTextSize(2);
        display.println("EGT 1 LOW");
        display.display();
        delay(300);
        digitalWrite(12, LOW);

    }

    if (sensorStatus >= 10)

       delay(1);
    display.clearDisplay();
    display.display();


 // entrada A1 para EGT2

    int sensorStatus1 = analogRead(sensorPin1);   //read the status of the sensor1 value

    //check if the sensor status is >=305)


    if (sensorStatus1 >= 305)
    {

        display.setTextColor(WHITE);
        display.setCursor(1, 30);
        display.setTextSize(2);
        display.println("EGT 2 HIGH");
        display.display();
        delay(300);
        digitalWrite(12, HIGH); // valor para acender o led
    }


if (sensorStatus < 305) // valor para apagar o led

    {

        digitalWrite(12, LOW);

    }

 if (sensorStatus >= 305)
   

    {

        // turn off tone function for pin 8:
        noTone(6);
        // play a note on pin 6 for 200 ms:
        tone(6, 440, 200);
        delay(200);
        tone(6, 600, 200);

    }
    
    if (sensorStatus1 <= 301)
    {

        display.setTextColor(WHITE);
        display.setCursor(1, 30);
        display.setTextSize(2);
        display.println("EGT 2 LOW");
        display.display();
        delay(300);
    }



    if (sensorStatus1 <= 10)

        delay(1);
    display.clearDisplay();
    display.display();

    delay(1);
   
}

That's better, however, it would be easier to read if there were not so much white space between lines. Generally no space between lines and one line only between functions.

Also, some of your comments are in Portuguese, I guess this is your native language. No problem as such but if you are asking English speakers for help then better to ask in English (we are a lazy lot, we expect the world to speak English but are rubbish are learning other people's languages).

You need to take a step back. I would have difficulty making your code do what you want because of the way it's laid out. You need to learn to break your code into separate functions that do different tasks. Don't do everything in loop(); All that should be in loop(); is calls to other functions that do the work. Unfortunately too many of the examples in the IDE don't show this. Have you done the tutorial blink without delay? It is the first step to what you want to do, however it does not use separate functions.

Have a look at the code in Using Nextion displays with Arduino Some of what is in there is what you need; it shows how to break code into seperate functions and it has a timer using millis. In this case the timer is every second to implement a clock, but the idea is the same as what you want to do.

you are absolutely right :slight_smile:

i'm a beginner in arduino programming like you have allready understood.

and yes I saw these tutorials

I will try to explain what I intend to do

I'm a pilot and I have a small plane.
Minimum and maximum temperatures are very important for engine health.
so with the arduino aduda I will read by analogical inputs 6 different temperatures and wanted to trigger an alert on the display for each of them

this is bad or well... is done and working

The problem was when I decided to put sound to be injected into the headset

I want to be alerted by the audio, yes, but only for about 10 seconds, so as not to disturb communications with the Ground.

stumbled here and can't get out :frowning:

I understand what you want to do. I'm at my limit of ability to help you. Your next step is to learn to write code in separate functions, not all in loop();. Please go and try to do that. Start with something simple, a simple timer as a timer is what you are aiming for. Use millis instead of delay.

Maybe someone else can offer help I can't, but at the moment that's the best I can give you.

Good luck.

I am betting your airplane's radio has a jack to inject audio into the earphone audio circuitry and you can use that to inject a warning tone.

Paul

Hi Paul,

Yes, that is what I intend to do. But under an alarm condition, I just wanted an audio alert 10 seconds.

If an emergency occurs, permanent sound will not help, and may even be dangerous if it causes anxiety.