Using both interrupts to increase and decrese timed delays

Currently I am working on project where I have used both of the interrupts of UNO (2,3) to get programmable delay for a LED. What I have done is, I have connected reset type switches on both of the interrupts of UNO, interrupt activates on rising.

When Pin 2 Interrupt is made to occur it increments the variable by 2 (like if the variable 0 on initialization then on occurrence of interrupt on pin it will become 2) later this is used as delay for the LED connected on pin 9 to keep it ON.

It is expected that if I press the interrupt (pin 2) during the execution of program the new value should add up in the previous value. I hae got this to work in case of increment values, But I am facing problems do it in same way while decrementing the value. Following is the code I am using right now.

volatile int chiran = 2;
volatile int new_chiran;
volatile int prev_chiran;
boolean anchi = true;

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

void setup()
{
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(9, OUTPUT);
 attachInterrupt(digitalPinToInterrupt(2),ANG,RISING);
 attachInterrupt(digitalPinToInterrupt(3),CHI,RISING);
 Serial.begin(9600);
 Serial.println("welcome");
}

void loop() 
{
  digitalWrite(6, HIGH);//MOTOR HIGH
 digitalWrite(9, HIGH);//INDICATION LED
 digitalWrite(7, HIGH);//BUZZ HIGH
 delay(2000);
 digitalWrite(7, LOW);//BUZZ LOW
  
 dilay(chiran);

 if(prev_chiran < chiran)
 {
  new_chiran = chiran - prev_chiran;
  delay(new_chiran);
  Serial.print("New set time:");
  Serial.println(new_chiran);
 }

 else
 {
  //nothing;
 }

 digitalWrite(6, LOW);
 digitalWrite(9, LOW);
}


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 inc()
{
 chiran = chiran + 2;
 Serial.println(chiran);
 if(chiran >= 16)
 {
   chiran = 2;
  }
  
}

void dec()
{
  chiran = chiran - 2;
  Serial.println(chiran);
 if(chiran < 2)
 {
   chiran = 2;
  }
 chiran = prev_chiran - chiran;
 dilay(chiran); 
  
}

void dilay(int chiran)
{
 Serial.print("Chiran =");
   Serial.println(chiran);


  prev_chiran = chiran;
 for(int i = 0; i < chiran; i++)
   {
    for(int x = 1; x < 61; x++)
    {
     for(int y = 0; y < 1001; y++)//gives delay of 1 Sec
      {
        delayMicroseconds(1000);
      }
    }
   }
 }

What should I do ? Thanks in advance

In your ISRs you are calling a function that uses Serial.print()
Interrupts are disabled when in an ISR and functions called from an ISR
Serial.print() uses interrupts

Can you see a possible problem ?

Thanks UKHeliBob, I deleted those serial prints still I am not getting it to wrok

What is triggering the switches?

If it is a human hand then I can't see any need to use interrupts. Humans are very slooooowwwwwww.

...R

Dear Robin, thanks for taking time to reply. I am using interrupts to enable me to vary the time for which the LED remains ON during the execution time.

am using interrupts to enable me to vary the time for which the LED remains ON during the execution time.

But you don't need to use a interrupts to do that. If you used millis() for timing you could poll the inputs thousands of times per second in loop()

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Count:
Dear Robin, thanks for taking time to reply. I am using interrupts to enable me to vary the time for which the LED remains ON during the execution time.

I understand that. But you have not answered my question - what is triggering the switches?

...R

Dear UKHeliBob and Robin2, Thanks for your great support altho I used completely different approach to solve my problem but you guys helped me to keep my confidence boosted up, thanks for helping.

If in case anybody intrested to know what I did, hers the code that worked for me.

#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();
    }

Good news that you got it working.

Here are some comments on your code

int chiran = 0;chiran is an int (2 bytes)

  EEPROM.write(address, chiran);EEPROM.write() writes a single byte.
It may work, but it is wrong.

If chiran is never going to be more than 255 then declare it as byte rather than int. The same goes for any other int variables that will never be more than 255.

In the inc() and dec() functions, which are called form ISRs, you change the value of chiran and use it later outside of the ISR. Because of that chiran should really be declared as a volatile variable. In the case of your program it probably won't matter because you don't change the value outside of the ISR but it is better to get into good habits.

  pinMode(9, OUTPUT);What is connected to the mysterious pin 9 ? Give it a name to make it more obvious what it is controlling. Further, what is connected to pins 2 and 3 ? Don't be shy, give the pins meaningful names.

Dear UKHeliBob thanks or your suggestions I will definitely gonna make those changes. Thans for your valuable support

Dear UKHeliBob, I got a strange behavior in the same program I posted few days ago as a working code. My problem is that once the output pin 9 goes HIGH both of my interrupts become inactive i dont know whats the reason behind it please help me to figure it out. when variables ' nil ' and 'yogesh' are updated by taking time from RTC the interrupts become inactive. If I set the values of these values predecided then the interrupts ar not ignored.