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.
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
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.
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?
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();
}
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, 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.