Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example/interrupt #57

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions examples/AlarmInterrupt/AlarmInterrupt.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
AlarmInterrupt.ino
Jacob Nuernberg
08/22

Example on using interrupts with DS3231 alarms.

Connect DS3231 SQW pin to Arduino interrupt pin 2

Tested on:
- Arduino UNO
- Arduino nano

*/

#include <DS3231.h>
#include <Wire.h>

// myRTC interrupt pin
#define CLINT 2

// Setup clock
DS3231 myRTC;

// Interrupt signaling byte
volatile byte tick = 1;

void setup() {
// Begin I2C communication
Wire.begin();

// Setup alarm one to fire every second
myRTC.turnOffAlarm(1);
myRTC.setA1Time(0, 0, 0, 0, 0b01111111, false, false, false);
myRTC.turnOnAlarm(1);
myRTC.checkIfAlarm(1);

// attach clock interrupt
pinMode(CLINT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLINT), isr_TickTock, FALLING);

// Use builtin LED to blink
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
// static variable to keep track of LED on/off state
static byte state = false;

// if alarm went of, do alarm stuff
if (tick) {
tick = 0;
state = ~state;
digitalWrite(LED_BUILTIN, state);
// Clear alarm state
myRTC.checkIfAlarm(1);
}

// Loop delay to emulate other running code
delay(10);
}


void isr_TickTock() {
// interrupt signals to loop
tick = 1;
return;
}