Go Down

Topic: Interrupts getting ignored (Read 1 time) previous topic - next topic

Count

Hello friends, recently I wrote few days back

Code: [Select]

#include <EEPROM.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

int chiran = 0;
int anchi = 0;
boolean ang = true;
boolean star = true;
int address = 0;
int yogesh = 0;
int yog = 0;
int nil;

//Debouncing Time in Milliseconds
long debouncing_time = 400;
volatile unsigned long last_micros;

void inc()
{
  chiran ++;
  EEPROM.write(address,chiran);
  ang = false;
  }

void dec()
{
  chiran --;
  EEPROM.write(address,chiran);
  ang = false;
  }

void ANG()
{
  if((long)(micros() - last_micros) >= debouncing_time * 1000)
  {
    inc();
    last_micros = micros();
  }
}

void CHI()
{
  if((long)(micros() - last_micros) >= debouncing_time * 1000)
  {
    dec();
    last_micros = micros();
  }
}

void setup ()
{
 Serial.begin(9600);
 pinMode(9, OUTPUT);
 
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  attachInterrupt(digitalPinToInterrupt(2), ANG, RISING);
  attachInterrupt(digitalPinToInterrupt(3), CHI, RISING);
}

void loop ()
{
 
 DateTime now = rtc.now();
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    Serial.print("EEPROM:");
    Serial.println(EEPROM.read(address));
    Serial.print("chiran:");
    Serial.println(chiran);
    Serial.print("ang:");
    Serial.println(ang);
   
     if(star == true)
   {
    Serial.println("Motor ON time update ENABLED");
     nil = now.hour();
     nil = nil + 1;
     yogesh = now.minute();
     Serial.print("star: False & Motor Will be ON at:");
     Serial.print(nil);
     Serial.print(":");
     Serial.println(yogesh);
     Serial.println();
     star = false;
    }

     if(nil == now.hour()&& yogesh == now.minute())
     {
      Serial.println("Motor ON");
      digitalWrite(9, HIGH);
      ang = false;
      nil = 25;//enter the value which will never occur
      yogesh = 61;//value for minnute which will never occur
       }

     
       if (ang == false)
    {
      Serial.println("Motor ON time Updtae EnABLED");
      Serial.println("Updating ANCHI");
      yog = now.hour();
      anchi = now.minute();
      anchi = anchi + EEPROM.read(address);
      Serial.print("EEPROM:");
      Serial.println(EEPROM.read(address));
      Serial.print("MOTOr will be OFF at:");
      Serial.println(anchi);
      ang = true;
    }

    if (anchi == now.minute())
      {
        Serial.println("Mtr OFF");
        digitalWrite(9, LOW);
        anchi = 61;
        star = true;
       }

       if (star == true)
       {
        Serial.println("Motor ON time update ENABLED");
        }
        else
        {
          Serial.println("Motor ON time update DISABLED");
          }

          if (ang == false)
          {
            Serial.println("Motor OFF time Update ENABLED");
          }
          else
          {
            Serial.println("Motor OFF time update DISABLED");
            }
    Serial.print("chiran:");
    Serial.println(chiran);
   
    Serial.print("EEPROM:");
    Serial.println(EEPROM.read(address));
   
    delay(3000);
    Serial.println();
    }



My problem is that once the pin 9 goes HIGH the interrupts are just ignored and nothing happens even when the interrupt occurs.

I have connected to reset switches on each of the interrupts triggered as rising. Any help!!!

Thanks in advance

6v6gt

Variables which are updated in a call chain from an interrupt service routine should be declared as volatile e.g. chiran .

Count

 I did that too but it still doesn't work

6v6gt

Have you a pull down resistor (say 10K)  on pins 2 & 3 ?
You changed both ang and chiran to volatile ?
What board / processor have you and what is connected to pins 2 & 3 ?

Count

I have those pull down resistors exactly 10K. I am using Arduino UNO. Strange thing is that Interrupts are detected when the PIN 9 is LOW, But once it goes HIGH interrupts are just ignored. Sometimes the if loop turning pin 9 HIGH not even execute even if its condition becomes true.

Whandall

The Arduino is failing when you switch the motor on?

I guess that points to a power problem.

Do you have a proper power supply for the motor?

Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Count

Right now I have just connected a LED on pin 9. I haven't connected any ext power the arduino is powered with USB only

6v6gt

#7
Apr 08, 2018, 10:46 am Last Edit: Apr 08, 2018, 10:53 am by 6v6gt
Then this question:
Quote
and what is connected to pins 2 & 3 ?
If the device generating the interrupt is not giving a sharp enough rise, you could use a pin change interrupt instead.

And maybe comment out ang here :

Code: [Select]
  if (nil == now.hour() && yogesh == now.minute())
  {
    Serial.println("Motor ON");
    digitalWrite(9, HIGH);
    // ang = false;
    nil = 25;//enter the value which will never occur
    yogesh = 61;//value for minnute which will never occur
  }


because I guess you are assuming that an interrupt has not worked based indirectly on the value of ang.

Robin2

Why are you doing this multiplication in your ISR
Code: [Select]
if((long)(micros() - last_micros) >= debouncing_time * 1000)
just change debouncing_time from 400 to 400000 or do the calculation once in setup()

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Count

Quote
If the device generating the interrupt is not giving a sharp enough rise, you could use a pin change interrupt instead.
But then wouldn't the interrupt occurring twice in a single key press. Major problem is that I have to wait for complete one hour to see if the program works or not. and now you would say that I should test it for minutes. So another strange thing is the program works fine when I test it for minutes like comparing minutes from the RTC. It even works great when I manually assign the values to variable rather than getting it from the RTC. but when I get the values from RTC like hours and minutes the problem starts

6v6gt

OK. You are using an RTC to generate an interrupt.

Have a look at this to see if it matches your problem:

http://forum.arduino.cc/index.php?topic=342672.0

Count

Then this question:
If the device generating the interrupt is not giving a sharp enough rise, you could use a pin change interrupt instead.

And maybe comment out ang here :

Code: [Select]
 if (nil == now.hour() && yogesh == now.minute())
  {
    Serial.println("Motor ON");
    digitalWrite(9, HIGH);
    // ang = false;
    nil = 25;//enter the value which will never occur
    yogesh = 61;//value for minnute which will never occur
  }


because I guess you are assuming that an interrupt has not worked based indirectly on the value of ang.
Then this question:
If the device generating the interrupt is not giving a sharp enough rise, you could use a pin change interrupt instead.

And maybe comment out ang here :

Code: [Select]
 if (nil == now.hour() && yogesh == now.minute())
  {
    Serial.println("Motor ON");
    digitalWrite(9, HIGH);
    // ang = false;
    nil = 25;//enter the value which will never occur
    yogesh = 61;//value for minnute which will never occur
  }


because I guess you are assuming that an interrupt has not worked based indirectly on the value of ang.
I have kept this command bcoz I want the if loop that sets motor OFF time to be enabled only after the pin 9 goes high. If I am wrong implementing that please tell me how do I correct it

Count

OK. You are using an RTC to generate an interrupt.

Have a look at this to see if it matches your problem:

http://forum.arduino.cc/index.php?topic=342672.0
No I am not using RTC to generate Interrupt I just fetch time from it add decimal 2 to it and then compare it with current time and when the current time and that is equal it turns pin 9 high. I do this bcoz I want to make pin 9 HIGH after every two hrs.

Count

#13
Apr 08, 2018, 11:23 am Last Edit: Apr 08, 2018, 11:25 am by Count

[/quote]
Why are you doing this multiplication in your ISR
Code: [Select]
if((long)(micros() - last_micros) >= debouncing_time * 1000)
just change debouncing_time from 400 to 400000 or do the calculation once in setup()

...R
How do I do that can you guide me please

6v6gt

OK. For the third time:

Then this question:
Quote
and what is connected to pins 2 & 3 ?
Also: how are you determining that an interrupt has not been detected ?

Ignore the last test code change I gave you and temporarily replace your loop with this which will determine if interrupts are working or not or maybe point to a logic error in your code.

Code: [Select]

void loop ()
{

  if (ang == false )
  {
    Serial.println("Interrupt detected");
    ang = true ;
  }

}


Go Up