arduino once interrupt and reset after x second if no interrupt.

Hi friends. is it possible: make arduino once interrupt and reset after x second if no interrupt ? if yes please help me how can I write this program. thanks.

Please explain exactly what you want to do. Resetting the Arduino from a program is a very extreme thing to do. When you say "interrupt" do you actually mean that ?

UKHeliBob:
Please explain exactly what you want to do. Resetting the Arduino from a program is a very extreme thing to do. When you say "interrupt" do you actually mean that ?

thanks for reply. I have a high voltage fault indicator. I want to connect the fault indicator LED output to the arduino interrupt pin and if any fault appears send sms. my problem is when fault appears in fault indicator the fault indicator LEDs flashing every 5 sec and the arduino interrupts activating every 5 sec and sending the sms every 5 sec. I want to when fault indicator LED flashing, with the first LED flashing active the interrupt once, then check the interrupts after x second if there is no LED flashing resetting the program.
thanks and sorry for bad english.

I strongly suspect that you do not need to use an interrupt for that.

It sounds like you need to read up on using millis() for timing

Save the value of millis() as the start time when the first LED flash is detected, set a boolean to true to flag the fact that timing is taking place and do whatever you need to react to the detected LED flash. Ignore any more inputs from the LED flashing whilst the boolean is true, but each time through loop() check whether the current millis() value minus the start time is greater than the required period. When the period has elapsed set the boolean to false to enable detection again and do whatever else you need

What exactly do you mean by "resetting the program" ?

For more details on using millis() for timing see Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

UKHeliBob:
I strongly suspect that you do not need to use an interrupt for that.

It sounds like you need to read up on using millis() for timing

Save the value of millis() as the start time when the first LED flash is detected, set a boolean to true to flag the fact that timing is taking place and do whatever you need to react to the detected LED flash. Ignore any more inputs from the LED flashing whilst the boolean is true, but each time through loop() check whether the current millis() value minus the start time is greater than the required period. When the period has elapsed set the boolean to false to enable detection again and do whatever else you need

What exactly do you mean by "resetting the program" ?

For more details on using millis() for timing see Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

thanks for reply. OK i'm playing with milis(). about the "resetting the program" ?: I mean just back to normal program running.

ErfanDL:
I mean just back to normal program running.

The demo Several Things at a Time illustrates that. Note how each function runs very briefly and returns to loop() so the next one can be called.

...R

UKHeliBob:
I strongly suspect that you do not need to use an interrupt for that.

It sounds like you need to read up on using millis() for timing

Save the value of millis() as the start time when the first LED flash is detected, set a boolean to true to flag the fact that timing is taking place and do whatever you need to react to the detected LED flash. Ignore any more inputs from the LED flashing whilst the boolean is true, but each time through loop() check whether the current millis() value minus the start time is greater than the required period. When the period has elapsed set the boolean to false to enable detection again and do whatever else you need

What exactly do you mean by "resetting the program" ?

For more details on using millis() for timing see Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

ahh. I'm not success! can you write the code for me ? I'm a little noob in codding :frowning:
also I need interrupt because the arduino board must be in sleep mode
thanks

Your code may not involve millis() for timing.

What you need to do is recognize when a fault occurs. That is NOT the same as recognizing that the fault LED is on. You want to send a message when the fault occurs.

You also need to recognize when the fault is no longer occurring. That is NOT the same as recognizing that the fault LED is off.

ahh. I'm not success!

What did you try ?

UKHeliBob:
What did you try ?

I'm writing some code with milli but it's completely wrong because I'm really noob in coding. if you can please help me and writing the code.
thanks.

Have a look at this example

const byte inputPin = A1;
unsigned long inputWentLowTime;
unsigned long currentTime;
unsigned long ignorePeriod = 5000;
boolean ignoreInput = false;

void setup()
{
  Serial.begin(115200);
  pinMode(inputPin, INPUT_PULLUP);
  Serial.println("looking for the input to go LOW");
  Serial.println();
}

void loop()
{
  currentTime = millis();
  if (ignoreInput == false)
  {
    if (digitalRead(inputPin) == LOW)
    {
      inputWentLowTime = currentTime;
      ignoreInput = true;
      Serial.println("input went LOW");
      Serial.println("now ignoring further inputs for 5 seconds");
      Serial.println();
    }
  }
  else if (currentTime - inputWentLowTime >= ignorePeriod)
  {
    Serial.println("ignore period ended");
    Serial.println("looking for the input to go LOW");
    Serial.println();
    ignoreInput = false;
  }
}

You can adapt it to read your input and take the required actions instead of just printing a message

UKHeliBob:
Have a look at this example

const byte inputPin = A1;

unsigned long inputWentLowTime;
unsigned long currentTime;
unsigned long ignorePeriod = 5000;
boolean ignoreInput = false;

void setup()
{
  Serial.begin(115200);
  pinMode(inputPin, INPUT_PULLUP);
  Serial.println("looking for the input to go LOW");
  Serial.println();
}

void loop()
{
  currentTime = millis();
  if (ignoreInput == false)
  {
    if (digitalRead(inputPin) == LOW)
    {
      inputWentLowTime = currentTime;
      ignoreInput = true;
      Serial.println("input went LOW");
      Serial.println("now ignoring further inputs for 5 seconds");
      Serial.println();
    }
  }
  else if (currentTime - inputWentLowTime >= ignorePeriod)
  {
    Serial.println("ignore period ended");
    Serial.println("looking for the input to go LOW");
    Serial.println();
    ignoreInput = false;
  }
}



You can adapt it to read your input and take the required actions instead of just printing a message

Hi thanks. I writing this code and it's just turn ON the LED on pin 3 for 2 sec and turn OFF. can you help me to fix ?

int ledPin = 3;// choose the pin for the LED
int btPin = 2;               // choose the input pin
int val = 0;
unsigned long detectTime;

void setup() {

 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(btPin, INPUT);     // declare sensor as input

}

void loop() {

 val = digitalRead(btPin);
 // read input value

 if (val == HIGH) {            // check if the input is HIGH
   digitalWrite(ledPin, HIGH);
 }
 else if (val == LOW) {
   digitalWrite(ledPin, LOW);
 }
 detectTime = millis();
 // turn LED ON

 if ( (millis() - detectTime) <= 2000) {

   digitalWrite(ledPin, LOW);
 }
}

One obvious problem are these lines

 detectTime = millis();
  // turn LED ON
  if ( (millis() - detectTime) <= 2000)
  {
    digitalWrite(ledPin, LOW);
  }

Every time through loop() detectTime is updated to the current value of millis() then immediately afterwards you check to see whether 2 seconds has elapsed since the detect time. How likely is that to be anything but true ?

Another question. How is the input wired ? Do you have a resistor in place to keep it either HIGH or LOW when the button is not pressed or is it floating at an unknown voltage that could be either HIGH or LOW ?

UKHeliBob:
One obvious problem are these lines

 detectTime = millis();

// turn LED ON
  if ( (millis() - detectTime) <= 2000)
  {
    digitalWrite(ledPin, LOW);
  }



Every time through loop() detectTime is updated to the current value of millis() then immediately afterwards you check to see whether 2 seconds has elapsed since the detect time. How likely is that to be anything but true ?

Another question. How is the input wired ? Do you have a resistor in place to keep it either HIGH or LOW when the button is not pressed or is it floating at an unknown voltage that could be either HIGH or LOW ?

thanks. the fault indicator flashing every 5 sec for unknown time maybe for 1 hour flashing to fix the line fault.
yes I put the 4.7kohm resistor between the GND and pin 2. when LED flashing, active the arduino input pin

I put the 4.7kohm resistor between the GND and pin 2. when LED flashing, active the arduino input pin

So pin 2 is LOW until the LED turns on which should make the input go HIGH. What is the input voltage and do the two systems have a common GND connection so that there is a common point of reference against which the voltage can be measured ?

Did you try the example code in post #10 ?
Try it using the Arduino alone with no external connections. Change the Serial baud rate to suit your self. To trigger the timing period you can just connect the input pin to GND using a piece of wire. You do not even need a switch.

UKHeliBob:
So pin 2 is LOW until the LED turns on which should make the input go HIGH. What is the input voltage and do the two systems have a common GND connection so that there is a common point of reference against which the voltage can be measured ?

Did you try the example code in post #10 ?
Try it using the Arduino alone with no external connections. Change the Serial baud rate to suit your self. To trigger the timing period you can just connect the input pin to GND using a piece of wire. You do not even need a switch.

the output from fault indicator LED is 1.72 volt and only works when I make common GND between the arduino and fault indicator. no I dont test the code in post #10.

the output from fault indicator LED is 1.72 volt

Is that high enough to be reliably regarded as HIGH by the Arduino ? Which Arduino are you using ?

no I dont test the code in post #10.

Before going any further I suggest that you try it, understand it, perhaps comment the lines of code that deal with the logic and timing

yeah that's make the arduino input pin to high without any problem. that's enough. the arduino is UNO

OK I test the code from post #10 the output from SM is:

ignore period ended
looking for the input to go LOW

input went LOW
now ignoring further inputs for 5 seconds

ignore period ended
looking for the input to go LOW

input went LOW
now ignoring further inputs for 5 seconds

ignore period ended
looking for the input to go LOW

but it's need to connect the GND to A1 continuously.

but it's need to connect the GND to A1 continuously.

No it doesn't

Describe what happens when you connect A1 to GND then disconnect it
Is a message printed ?
If so then what message do you get ?
Does a different message appear after a period of time ?
How long was the period ?
If so what message do you get ?

Assuming that you connected A1 to GND then disconnected it, got a message, waited and got a different message some period of time later what happens if you connect A1 to GND again and disconnect it ?

What happens if you repeatedly connect and disconnect A1 to GND ?
Do you get a message each time you make the connection to GND ?

In the example, connecting A1 to GND is behaving like an LED going on and off. The first time it happens a message is printed then the program ignores further connections of A1 to GND for a period before reacting again.

UKHeliBob:
No it doesn't

Describe what happens when you connect A1 to GND then disconnect it
Is a message printed ?
If so then what message do you get ?
Does a different message appear after a period of time ?
How long was the period ?
If so what message do you get ?

Assuming that you connected A1 to GND then disconnected it, got a message, waited and got a different message some period of time later what happens if you connect A1 to GND again and disconnect it ?

What happens if you repeatedly connect and disconnect A1 to GND ?
Do you get a message each time you make the connection to GND ?

In the example, connecting A1 to GND is behaving like an LED going on and off. The first time it happens a message is printed then the program ignores further connections of A1 to GND for a period before reacting again.

you just ask my question, with questions. I just need someone to help me coding this project.