Hi, I want to use interrupt and used a very simple interrupt command to test it before proceeding for further coding. However whenever an interrupt occurs loop stops but interrupt routine does not work.
Normally during the loop I should see the date and time, changing every second.
When Interrupt happens, the screen freezes, nothing changes. If interrupt happens when servo is rotating then servo stops and again nothing happens on the screen.
Note that I'm using external power for all peripherals, so it is not a power issue.
Here is my code. I'm connecting interrupt pin to GND to start interrupt sequence.
#include <Wire.h> // Include IIC communication library
#include <LiquidCrystal_I2C.h> // Include LCD library
#include <RTClib.h> // Include RTC library
#include <Servo.h> // Include Servo library
Servo myservo; // Declare Servo name as myservo
#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif
uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
RTC_DS1307 RTC;
LiquidCrystal_I2C lcd(0x27,16,2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
Serial.begin(9600);
Wire.begin();
RTC.begin();
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), setbuttonpressed, FALLING);
myservo.attach(10); // Servo is attached to pin 10
lcd.init(); // Initialize the lcd
lcd.backlight(); // Initialize LCD backlight
lcd.print("Curtain System"); // Print "Curtain System"
lcd.setCursor(0, 1); // Go to second line 1(second line) pixel zero(actually first)
lcd.print("Is Starting"); // Print "Is Starting"
delay(5000); // 2000ms delay
lcd.createChar(2, clock);
//RTC.adjust(DateTime(2019,5,9,0,30,0)); // Following line sets the RTC to the date & time this sketch was compiled,
}
void loop()
{
lcd.clear();
DateTime now = RTC.now(); // Describe now item
lcd.print(now.day(), DEC); // Print Day
lcd.print('.'); // Print .
lcd.print(now.month(), DEC); // Print Month
lcd.print('.'); // Print .
lcd.print(now.year(), DEC); // Print Year
lcd.print(' '); // Print Space
lcd.setCursor(0, 1); // Go to second line 1(second line) pixel zero(actually first)
lcd.printByte(2); // Print clock icon
lcd.print(" "); // Print Space
lcd.print(now.hour(), DEC); // Print Hour
lcd.print(':'); // Print :
lcd.print(now.minute(), DEC); // Print Minute
lcd.print(':'); // Print :
lcd.print(now.second(), DEC); // Print Second
delay(1000); // 1000ms delay and go back to beginning of loop, meaning renew screen every second
if((now.hour()>= 7) && (now.hour()<=19)) // If the hour is bigger than 7 and smaller than 19(During day)
// if((now.hour()>= 23 & now.minute() == 0 ) && (now.hour() <= 7 & now.minute() == 0 ))
{
lcd.clear();
lcd.print("Servo turns"); // Print "Servo turns"
lcd.setCursor(0,1); // Go to second line first pixel
lcd.print("counterclockwise"); // Print "counterclockwise"
myservo.attach(10); // Servo is attached to pin 10
myservo.write(45); // Turn servo counter clockwise
delay(5000); // For 2 seconds
myservo.write(90); // Stop servo
delay(1000);
myservo.detach(); // Servo is detached to stop jittering
while((now.hour()>=7) && (now.hour()<=19)) // Run an empty loop while the time is between 07:00-19:00. Exit when condition is invalid
{lcd.clear();
DateTime now = RTC.now(); // Describe now item
lcd.print(now.day(), DEC); // Print Day
lcd.print('.'); // Print .
lcd.print(now.month(), DEC); // Print Month
lcd.print('.'); // Print .
lcd.print(now.year(), DEC); // Print Year
lcd.print(' '); // Print Space
lcd.print("Curtn"); // Print "Curtn"
lcd.setCursor(0, 1); // Go to second line 1(second line) pixel zero(actually first)
lcd.printByte(2); // Print clock icon
lcd.print(" "); // Print Space
lcd.print(now.hour(), DEC); // Print Hour
lcd.print(':'); // Print :
lcd.print(now.minute(), DEC); // Print Minute
lcd.print(':'); // Print :
lcd.print(now.second(), DEC); // Print Second
lcd.print(' '); // Print Space
lcd.print("Opend"); // Print "Opend"
delay(1000); // 1000ms delay and go back to beginning of loop, meaning renew screen every second
}
}
else
{
lcd.clear();
lcd.print("Servo turns"); // Print "Servo turns"
lcd.setCursor(0, 1); // Go to second line first pixel
lcd.print("clockwise"); // Print "clockwise"
myservo.attach(10); // Servo is attached to pin 10
myservo.write(135); // Turn servo clockwise
delay(5000); // For 2 seconds
myservo.write(90); // Stop servo
delay(1000);
myservo.detach(); // Servo is detached to stop jittering
while((now.hour()>19) || (now.hour()<7)) // Run an empty loop while time is between 19:00-07:00. Exit when condition is invalid.
{
lcd.clear();
DateTime now = RTC.now(); // Describe now item
lcd.print(now.day(), DEC); // Print Day
lcd.print('.'); // Print .
lcd.print(now.month(), DEC); // Print Month
lcd.print('.'); // Print .
lcd.print(now.year(), DEC); // Print Year
lcd.print(' '); // Print Space
lcd.print("Curtn"); // Print "Curtn"
lcd.setCursor(0, 1); // Go to second line 1(second line) pixel zero(actually first)
lcd.printByte(2); // Print clock icon
lcd.print(" "); // Print Space
lcd.print(now.hour(), DEC); // Print Hour
lcd.print(':'); // Print :
lcd.print(now.minute(), DEC); // Print Minute
lcd.print(':'); // Print :
lcd.print(now.second(), DEC); // Print Second
lcd.print(' '); // Print Space
lcd.print("Closd"); // Print "Closd"
delay(1000); // 1000ms delay and go back to beginning of loop, meaning renew screen every second
}
}
}
void setbuttonpressed()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("interrupt start");
delay(4000);
//Put date and time change menu here, include plus and minus buttons
}