I am doing an experiment. In this experiment I want to start the RTC by presing the external Push Button which is connected with Aquino's pin 2 and take varites of action according to Time. For interrupt I have made a ISR and in this interrupt I wrote my main logic. But this things is not working. The schematics isSchematic Diagram
and The code is
`include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 9);
RTC_DS1307 rtc;
int k = 0;
int led =13;
int pin =2;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
lcd.begin(16, 2);
rtc.begin();
Serial.begin(11520);
pinMode(led,OUTPUT);
pinMode(pin,INPUT);
attachInterrupt(0, Time , LOW);
// Print a message to the LCD.
if (! rtc.begin()) {
lcd.print("hello, world!");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
}
void loop()
{}
void Time()
{
{DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.day(), DEC);
lcd.print(" ");
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
int c = now.second();
lcd.setCursor(0, 1);
lcd.print(c);
if(c==58)
{
k=k+1;
lcd.setCursor(5, 1);
lcd.print(k);
if(k==4)
{
digitalWrite(led,HIGH);
k=0;
}
}
}}`
please help me.
1 Answer 1
Well, for example Arduino's delay
needs interrupts to be working and as you may not know it's disabled inside of ISR
handler.
Never ever use anything like while (1)
, delay(1000)
and so on inside of ISR
! And also do not reenable interrupts either! Set a simple volatile
flag and use it inside of loop
function instead.
Basically something like this:
constexpr int8_t button = 2;
volatile bool flag = false;
void setFlag() {
flag = true;
}
void setup() {
pinMode(button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button), &setFlag, FALLING);
}
void loop() {
if (flag) {
// it's running, you can do something here
// and you can reset the flag here either
}
//delay(100); // if needed
}
-
sorry I am not getting your point. I have used
while(1)
anddelay()
for special reason forget about that. I have checked this code withoutwhile(1)
delay()
insideISR
still its not working.Prayuktibid– Prayuktibid2017年03月29日 09:05:13 +00:00Commented Mar 29, 2017 at 9:05 -
@Prayuktibid: You wrote "I have checked this code without [...] still its not working." There is no point in talking about code we cannot see. Please update the question with the relevant code and tell us exactly how it is not working.Edgar Bonet– Edgar Bonet2017年03月29日 09:22:07 +00:00Commented Mar 29, 2017 at 9:22
-
@EdgarBonet I have updated the code plaese check itPrayuktibid– Prayuktibid2017年03月29日 10:19:17 +00:00Commented Mar 29, 2017 at 10:19
-
@KIIV Sir, Why you using address of
setFlag()
Function Instead ofsetFlag()
function in this program please explain me.Prayuktibid– Prayuktibid2017年03月31日 07:05:04 +00:00Commented Mar 31, 2017 at 7:05 -
@Prayuktibid Well, without ampersand it's converted to the function pointer implicitly, so it's not required. But it's more obvious for reviewers or coworkers. For the class methods (member functions) it's required.KIIV– KIIV2017年03月31日 07:14:56 +00:00Commented Mar 31, 2017 at 7:14