parent
0f98d2d6d6
commit
81ec2204d8
@ -0,0 +1,311 @@ |
||||
// CONNECTIONS: |
||||
// DS1307 SDA --> SDA |
||||
// DS1307 SCL --> SCL |
||||
// DS1307 VCC --> 5v |
||||
// DS1307 GND --> GND |
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0])) |
||||
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
#include <EepromAT24C32.h> |
||||
|
||||
void printDateTime(const RtcDateTime &dt); |
||||
|
||||
RtcDS3231<TwoWire> Rtc(Wire); |
||||
EepromAt24c32<TwoWire> RtcEeprom(Wire); |
||||
|
||||
const int pwrPin = 4; |
||||
|
||||
// if you have any of the address pins on the RTC soldered together |
||||
// then you need to provide the state of those pins, normally they |
||||
// are connected to vcc with a reading of 1, if soldered they are |
||||
// grounded with a reading of 0. The bits are in the order A2 A1 A0 |
||||
// thus the following would have the A2 soldered together |
||||
// EepromAt24c32<TwoWire> RtcEeprom(Wire, 0b011); |
||||
|
||||
/* for normal hardware wire use above */ |
||||
|
||||
// nothing longer than 32 bytes |
||||
// rtc eeprom memory is 32 byte pages |
||||
// writing is limited to each page, so it will wrap at page |
||||
// boundaries. |
||||
// But reading is only limited by the buffer in Wire class which |
||||
// by default is 32 |
||||
|
||||
const char data[] = "a..............."; |
||||
const uint16_t stringAddr = 64; // stored on page boundary |
||||
|
||||
const int memTotal = 4096; |
||||
const int pageSize = 16; |
||||
const int pageCount = (memTotal / pageSize) - 1; |
||||
|
||||
void setup() |
||||
{ |
||||
pinMode(pwrPin, OUTPUT); |
||||
digitalWrite(pwrPin, HIGH); |
||||
|
||||
Serial.begin(9600); |
||||
delay(3000); |
||||
|
||||
RtcEeprom.Begin(); |
||||
|
||||
// // store starting address of string |
||||
// RtcEeprom.SetMemory(0, stringAddr); |
||||
// // store the string, nothing longer than 32 bytes due to paging |
||||
// uint8_t written = RtcEeprom.SetMemory(stringAddr, (const uint8_t*)data, sizeof(data) - 1); // remove the null terminator strings add |
||||
// // store the length of the string |
||||
// RtcEeprom.SetMemory(1, written); // store the |
||||
|
||||
// Serial.println("Writing started."); |
||||
// for (int i = 0; i <= pageCount; i++) |
||||
// { |
||||
// uint8_t written = RtcEeprom.SetMemory(i * pageSize, (const uint8_t *)data, sizeof(data) - 1); |
||||
// Serial.print("."); |
||||
// } |
||||
// Serial.println("\nWriting finished."); |
||||
// delay(250); |
||||
|
||||
// Serial.print("Reading "); |
||||
// Serial.print(pageSize); |
||||
// Serial.println(" bytes of data:"); |
||||
// for (int i = 0; i <= pageCount; i++) |
||||
// { |
||||
// uint8_t buff[pageSize + 1]; |
||||
// uint8_t gotten = RtcEeprom.GetMemory(i * pageSize, buff, pageSize); |
||||
// Serial.print(i); |
||||
// Serial.print(":\t"); |
||||
// Serial.print("data read ("); |
||||
// Serial.print(gotten); |
||||
// Serial.print(") = \""); |
||||
// for (uint8_t ch = 0; ch < gotten; ch++) |
||||
// { |
||||
// Serial.print((char)buff[ch]); |
||||
// } |
||||
// Serial.println("\""); |
||||
// delay(10); |
||||
// } |
||||
// delay(25000); |
||||
|
||||
delay(1); |
||||
} |
||||
|
||||
void loop() |
||||
{ |
||||
int lastA = -1; |
||||
int lastB = -1; |
||||
|
||||
for (int i = 0; i <= pageCount; i++) |
||||
{ |
||||
uint8_t x = RtcEeprom.GetMemory(i * pageSize); |
||||
|
||||
if ((char)x == 'a') |
||||
{ |
||||
lastA = i; |
||||
} |
||||
|
||||
if ((char)x == 'b') |
||||
{ |
||||
lastB = i; |
||||
} |
||||
} |
||||
|
||||
int head = -1; |
||||
uint8_t next = 'c'; |
||||
|
||||
if (lastA == pageCount) |
||||
{ |
||||
head = pageCount; |
||||
next = 'b'; |
||||
} |
||||
|
||||
if (lastB == pageCount) |
||||
{ |
||||
head = pageCount; |
||||
next = 'a'; |
||||
} |
||||
|
||||
if (lastA > lastB) |
||||
{ |
||||
head = lastB; |
||||
next = 'b'; |
||||
} |
||||
|
||||
if (lastB > lastA) |
||||
{ |
||||
head = lastA; |
||||
next = 'a'; |
||||
} |
||||
|
||||
head = (head + 1) % pageCount; |
||||
|
||||
Serial.print("Last a: "); |
||||
Serial.print(lastA); |
||||
Serial.print(", Last b: "); |
||||
Serial.print(lastB); |
||||
Serial.print(", HEAD: "); |
||||
Serial.print(head); |
||||
Serial.print(", next: "); |
||||
Serial.println((char)next); |
||||
|
||||
RtcEeprom.SetMemory(head * pageSize, next); |
||||
|
||||
for (int i = 0; i <= pageCount; i++) |
||||
{ |
||||
uint8_t buff[pageSize + 1]; |
||||
uint8_t gotten = RtcEeprom.GetMemory(i * pageSize, buff, pageSize); |
||||
Serial.print(i); |
||||
Serial.print(":\t"); |
||||
Serial.print("data read ("); |
||||
Serial.print(gotten); |
||||
Serial.print(") = \""); |
||||
for (uint8_t ch = 0; ch < gotten; ch++) |
||||
{ |
||||
Serial.print((char)buff[ch]); |
||||
} |
||||
Serial.println("\""); |
||||
} |
||||
|
||||
delay(3000); |
||||
} |
||||
|
||||
/* |
||||
#include <Arduino.h> |
||||
#include <avr/sleep.h> |
||||
|
||||
#include <Wire.h> |
||||
#include <RtcDS3231.h> |
||||
#include <HX711.h> |
||||
|
||||
RtcDS3231<TwoWire> Rtc(Wire); |
||||
HX711 scale; |
||||
|
||||
const int rtcPowerPin = 4; |
||||
const int wakeUpPin = 7; |
||||
const int ledPin = 17; |
||||
|
||||
const int scaleDataPin = 21; |
||||
const int scaleSckPin = 20; |
||||
const int scalePowerPin = 19; |
||||
|
||||
const int secondsTillNextWakeup = 3; |
||||
const long interval = 50; |
||||
|
||||
void wake() |
||||
{ |
||||
sleep_disable(); |
||||
detachInterrupt(digitalPinToInterrupt(wakeUpPin)); |
||||
} |
||||
|
||||
void sleepNow() |
||||
{ |
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); |
||||
noInterrupts(); |
||||
sleep_enable(); |
||||
attachInterrupt(digitalPinToInterrupt(wakeUpPin), wake, LOW); |
||||
interrupts(); |
||||
sleep_cpu(); |
||||
} |
||||
|
||||
void setup() |
||||
{ |
||||
delay(3000); |
||||
|
||||
Serial.begin(9660); |
||||
Serial.println("Initializing."); |
||||
|
||||
pinMode(wakeUpPin, INPUT_PULLUP); |
||||
pinMode(ledPin, OUTPUT); |
||||
pinMode(scalePowerPin, OUTPUT); |
||||
|
||||
pinMode(rtcPowerPin, OUTPUT); |
||||
digitalWrite(rtcPowerPin, HIGH); |
||||
digitalWrite(scalePowerPin, HIGH); |
||||
|
||||
Rtc.Begin(); |
||||
|
||||
scale.begin(scaleDataPin, scaleSckPin); |
||||
scale.set_scale(1000.f); // this value is obtained by calibrating the scale with known weights; see the README for details |
||||
scale.tare(); // reset the scale to 0 |
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
||||
|
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
} |
||||
|
||||
if (!Rtc.GetIsRunning()) |
||||
{ |
||||
Serial.println("RTC was not actively running, starting now"); |
||||
Rtc.SetIsRunning(true); |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
if (now < compiled) |
||||
{ |
||||
Serial.println("RTC is older than compile time! (Updating DateTime)"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
|
||||
Rtc.Enable32kHzPin(false); |
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne); |
||||
} |
||||
|
||||
void loop() |
||||
{ |
||||
pinMode(rtcPowerPin, OUTPUT); |
||||
pinMode(scalePowerPin, OUTPUT); |
||||
digitalWrite(scalePowerPin, HIGH); |
||||
digitalWrite(rtcPowerPin, HIGH); |
||||
digitalWrite(ledPin, LOW); |
||||
|
||||
delay(interval); |
||||
|
||||
scale.power_up(); |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
RtcDateTime alarmTime = now + secondsTillNextWakeup; |
||||
DS3231AlarmOne alarm1( |
||||
alarmTime.Day(), |
||||
alarmTime.Hour(), |
||||
alarmTime.Minute(), |
||||
alarmTime.Second(), |
||||
DS3231AlarmOneControl_HoursMinutesSecondsMatch); |
||||
|
||||
Rtc.SetAlarmOne(alarm1); |
||||
Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
RtcTemperature temp = Rtc.GetTemperature(); |
||||
|
||||
String buffer; |
||||
buffer += String(scale.get_units(1), 2); |
||||
buffer += F(" kg, "); |
||||
buffer += String(temp.AsFloatDegC(), 2); |
||||
buffer += F(" °C"); |
||||
Serial.println(buffer); |
||||
|
||||
Serial.flush(); |
||||
scale.power_down(); |
||||
|
||||
digitalWrite(scalePowerPin, LOW); |
||||
digitalWrite(ledPin, HIGH); |
||||
digitalWrite(rtcPowerPin, LOW); |
||||
pinMode(rtcPowerPin, INPUT); |
||||
pinMode(scalePowerPin, INPUT); |
||||
|
||||
sleepNow(); |
||||
} |
||||
*/ |
@ -0,0 +1,270 @@ |
||||
#include <Arduino.h> |
||||
#include <avr/sleep.h> |
||||
|
||||
// const byte wakeUpPin = 7; |
||||
const byte ledPin = 17; |
||||
const byte RtcSquareWavePin = 7; |
||||
// const byte RtcSquareWaveInterrupt = digitalPinToInterrupt(RtcSquareWavePin); |
||||
const byte RtcSquareWaveInterrupt = digitalPinToInterrupt(RtcSquareWavePin); |
||||
|
||||
// marked volatile so interrupt can safely modify them and |
||||
// other code can safely read and modify them |
||||
volatile uint16_t interuptCount = 0; |
||||
volatile bool interuptFlag = false; |
||||
|
||||
// void wake() |
||||
// { |
||||
// sleep_disable(); |
||||
// detachInterrupt(RtcSquareWaveInterrupt); |
||||
// // since this interupted any other running code, |
||||
// // don't do anything that takes long and especially avoid |
||||
// // any communications calls within this routine |
||||
// interuptCount++; |
||||
// interuptFlag = true; |
||||
// } |
||||
|
||||
// CONNECTIONS: |
||||
// DS3231 SDA --> SDA |
||||
// DS3231 SCL --> SCL |
||||
// DS3231 VCC --> 3.3v or 5v |
||||
// DS3231 GND --> GND |
||||
// SQW ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC) |
||||
|
||||
/* for software wire use below |
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
|
||||
SoftwareWire myWire(SDA, SCL); |
||||
RtcDS3231<SoftwareWire> Rtc(myWire); |
||||
for software wire use above */ |
||||
|
||||
/* for normal hardware wire use below */ |
||||
#include <Wire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
RtcDS3231<TwoWire> Rtc(Wire); |
||||
/* for normal hardware wire use above */ |
||||
|
||||
bool Alarmed(); |
||||
void printDateTime(const RtcDateTime &dt); |
||||
|
||||
// Interrupt Pin Lookup Table |
||||
// (copied from Arduino Docs) |
||||
// |
||||
// CAUTION: The interrupts are Arduino numbers NOT Atmel numbers |
||||
// and may not match (example, Mega2560 int.4 is actually Atmel Int2) |
||||
// this is only an issue if you plan to use the lower level interupt features |
||||
// |
||||
// Board int.0 int.1 int.2 int.3 int.4 int.5 |
||||
// --------------------------------------------------------------- |
||||
// Uno, Ethernet 2 3 |
||||
// Mega2560 2 3 21 20 [19] 18 |
||||
// Leonardo 3 2 0 1 7 |
||||
|
||||
// #define RtcSquareWavePin 7 // Mega2560 |
||||
// #define RtcSquareWaveInterrupt 4 // Mega2560 |
||||
|
||||
|
||||
|
||||
void ISR_ATTR InteruptServiceRoutine() |
||||
{ |
||||
sleep_disable(); |
||||
// detachInterrupt(RtcSquareWaveInterrupt); |
||||
// since this interupted any other running code, |
||||
// don't do anything that takes long and especially avoid |
||||
// any communications calls within this routine |
||||
interuptCount++; |
||||
interuptFlag = true; |
||||
} |
||||
|
||||
void sleepNow() |
||||
{ |
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); |
||||
noInterrupts(); |
||||
// sleep_enable(); |
||||
attachInterrupt(RtcSquareWaveInterrupt, InteruptServiceRoutine, FALLING); |
||||
interrupts(); |
||||
// sleep_cpu(); |
||||
} |
||||
|
||||
|
||||
void setup() |
||||
{ |
||||
Serial.begin(9600); |
||||
|
||||
// set the interupt pin to input mode |
||||
pinMode(RtcSquareWavePin, INPUT_PULLUP); |
||||
|
||||
//--------RTC SETUP ------------ |
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to |
||||
// the available pins for SDA, SCL |
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL |
||||
|
||||
Rtc.Begin(); |
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
||||
|
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
} |
||||
|
||||
if (!Rtc.GetIsRunning()) |
||||
{ |
||||
Serial.println("RTC was not actively running, starting now"); |
||||
Rtc.SetIsRunning(true); |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
if (now < compiled) |
||||
{ |
||||
Serial.println("RTC is older than compile time! (Updating DateTime)"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
|
||||
Rtc.Enable32kHzPin(false); |
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne); |
||||
|
||||
// Alarm 1 set to trigger every day when |
||||
// the hours, minutes, and seconds match |
||||
RtcDateTime alarmTime = now + 18; // into the future |
||||
DS3231AlarmOne alarm1( |
||||
alarmTime.Day(), |
||||
alarmTime.Hour(), |
||||
alarmTime.Minute(), |
||||
alarmTime.Second(), |
||||
DS3231AlarmOneControl_OncePerSecond); |
||||
// DS3231AlarmOneControl_HoursMinutesSecondsMatch); |
||||
Rtc.SetAlarmOne(alarm1); |
||||
|
||||
// Alarm 2 set to trigger at the top of the minute |
||||
DS3231AlarmTwo alarm2( |
||||
0, |
||||
0, |
||||
0, |
||||
DS3231AlarmTwoControl_OncePerMinute); |
||||
Rtc.SetAlarmTwo(alarm2); |
||||
|
||||
// throw away any old alarm state before we ran |
||||
Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
// setup external interupt |
||||
// attachInterrupt(RtcSquareWaveInterrupt, InteruptServiceRoutine, FALLING); |
||||
} |
||||
|
||||
void loop() |
||||
{ |
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
} |
||||
} |
||||
|
||||
// for (byte i = 0; i <= 3; i++){ |
||||
|
||||
if (Alarmed()) |
||||
{ |
||||
|
||||
for (byte i = 0; i <= 5; i++){ |
||||
digitalWrite(ledPin, !digitalRead(ledPin)); |
||||
delay(50); |
||||
} |
||||
// delay(3000); |
||||
|
||||
Serial.print(">>Interupt Count: "); |
||||
Serial.print(interuptCount); |
||||
Serial.println("<<"); |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
|
||||
printDateTime(now); |
||||
Serial.println(); |
||||
} |
||||
|
||||
sleepNow(); |
||||
|
||||
// pinMode(ledPin, OUTPUT); |
||||
// delay(200); |
||||
// digitalWrite(ledPin, HIGH); |
||||
// delay(500); |
||||
// digitalWrite(ledPin, LOW); |
||||
// delay(200); |
||||
// pinMode(ledPin, INPUT); |
||||
|
||||
// we only want to show time every 10 seconds |
||||
// but we want to show responce to the interupt firing |
||||
// for (int timeCount = 0; timeCount < 20; timeCount++) |
||||
// { |
||||
// if (Alarmed()) |
||||
// { |
||||
// Serial.print(">>Interupt Count: "); |
||||
// Serial.print(interuptCount); |
||||
// Serial.println("<<"); |
||||
// digitalWrite(LED, !digitalRead(LED)); |
||||
// } |
||||
// delay(500); |
||||
// } |
||||
} |
||||
|
||||
bool Alarmed() |
||||
{ |
||||
bool wasAlarmed = false; |
||||
if (interuptFlag) // check our flag that gets sets in the interupt |
||||
{ |
||||
wasAlarmed = true; |
||||
interuptFlag = false; // reset the flag |
||||
|
||||
// this gives us which alarms triggered and |
||||
// then allows for others to trigger again |
||||
DS3231AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
if (flag & DS3231AlarmFlag_Alarm1) |
||||
{ |
||||
Serial.println("alarm one triggered"); |
||||
} |
||||
if (flag & DS3231AlarmFlag_Alarm2) |
||||
{ |
||||
Serial.println("alarm two triggered"); |
||||
} |
||||
} |
||||
return wasAlarmed; |
||||
} |
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0])) |
||||
|
||||
void printDateTime(const RtcDateTime &dt) |
||||
{ |
||||
char datestring[20]; |
||||
|
||||
snprintf_P(datestring, |
||||
countof(datestring), |
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"), |
||||
dt.Month(), |
||||
dt.Day(), |
||||
dt.Year(), |
||||
dt.Hour(), |
||||
dt.Minute(), |
||||
dt.Second()); |
||||
Serial.print(datestring); |
||||
} |
@ -0,0 +1,47 @@ |
||||
#include <Arduino.h> |
||||
#include <avr/sleep.h> |
||||
|
||||
const int wakeUpPin = 7; |
||||
const int ledPin = 17; |
||||
|
||||
void wake() |
||||
{ |
||||
sleep_disable(); |
||||
detachInterrupt(digitalPinToInterrupt(wakeUpPin)); |
||||
} |
||||
|
||||
void sleepNow() |
||||
{ |
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); |
||||
noInterrupts(); |
||||
sleep_enable(); |
||||
attachInterrupt(digitalPinToInterrupt(wakeUpPin), wake, LOW); |
||||
interrupts(); |
||||
sleep_cpu(); |
||||
} |
||||
|
||||
void setup() |
||||
{ |
||||
pinMode(wakeUpPin, INPUT_PULLUP); |
||||
pinMode(ledPin, OUTPUT); |
||||
} |
||||
|
||||
void loop() |
||||
{ |
||||
// Do something here |
||||
// Example: Read sensor, data logging, data transmission. |
||||
for (byte i = 0; i <= 6; i++) |
||||
{ |
||||
digitalWrite(ledPin, !digitalRead(ledPin)); |
||||
delay(50); |
||||
} |
||||
|
||||
for (byte i = 0; i <= 2; i++) |
||||
{ |
||||
digitalWrite(ledPin, !digitalRead(ledPin)); |
||||
delay(300); |
||||
} |
||||
|
||||
// Now go to sleep |
||||
sleepNow(); |
||||
} |
@ -0,0 +1,38 @@ |
||||
#include <Arduino.h> |
||||
#include <avr/sleep.h> |
||||
|
||||
const int wakeUpPin = 7; |
||||
const int ledPin = 17; |
||||
|
||||
void wake() |
||||
{ |
||||
sleep_disable(); |
||||
detachInterrupt(digitalPinToInterrupt(wakeUpPin)); |
||||
} |
||||
|
||||
void sleepNow() |
||||
{ |
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); |
||||
noInterrupts(); |
||||
sleep_enable(); |
||||
attachInterrupt(digitalPinToInterrupt(wakeUpPin), wake, LOW); |
||||
interrupts(); |
||||
sleep_cpu(); |
||||
|
||||
} |
||||
|
||||
void setup() |
||||
{ |
||||
pinMode(wakeUpPin, INPUT_PULLUP); |
||||
pinMode(ledPin, OUTPUT); |
||||
} |
||||
|
||||
void loop() |
||||
{ |
||||
// Do something here |
||||
// Example: Read sensor, data logging, data transmission. |
||||
digitalWrite(ledPin, !digitalRead(ledPin)); |
||||
|
||||
// Now go to sleep |
||||
sleepNow(); |
||||
} |
@ -0,0 +1,431 @@ |
||||
#include <Arduino.h> |
||||
#include <avr/sleep.h> |
||||
|
||||
#include <Wire.h> |
||||
#include <RtcDS3231.h> |
||||
|
||||
RtcDS3231<TwoWire> Rtc(Wire); |
||||
|
||||
const int ledPin = 17; |
||||
const int wakeUpPin = 7; |
||||
const int wakeUpInterrupt = digitalPinToInterrupt(wakeUpPin); |
||||
|
||||
volatile uint16_t interuptCount = 0; |
||||
volatile bool interuptFlag = false; |
||||
|
||||
bool Alarmed(); |
||||
void printDateTime(const RtcDateTime &dt); |
||||
|
||||
void ISR_ATTR InteruptServiceRoutine() |
||||
{ |
||||
// sleep_disable(); |
||||
// detachInterrupt(wakeUpInterrupt); |
||||
|
||||
interuptCount++; |
||||
interuptFlag = true; |
||||
} |
||||
|
||||
void sleepNow() |
||||
{ |
||||
// set_sleep_mode(SLEEP_MODE_PWR_DOWN); |
||||
noInterrupts(); |
||||
// sleep_enable(); |
||||
attachInterrupt(wakeUpInterrupt, InteruptServiceRoutine, FALLING); |
||||
interrupts(); |
||||
// sleep_cpu(); |
||||
} |
||||
|
||||
void setup() |
||||
{ |
||||
pinMode(wakeUpPin, INPUT_PULLUP); |
||||
pinMode(ledPin, OUTPUT); |
||||
|
||||
Serial.begin(9660); |
||||
Rtc.Begin(); |
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
||||
|
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
} |
||||
|
||||
if (!Rtc.GetIsRunning()) |
||||
{ |
||||
Serial.println("RTC was not actively running, starting now"); |
||||
Rtc.SetIsRunning(true); |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
if (now < compiled) |
||||
{ |
||||
Serial.println("RTC is older than compile time! (Updating DateTime)"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
|
||||
Rtc.Enable32kHzPin(false); |
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne); |
||||
|
||||
DS3231AlarmOne alarm1(0, 0, 0, 0, DS3231AlarmOneControl_OncePerSecond); |
||||
Rtc.SetAlarmOne(alarm1); |
||||
|
||||
Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
attachInterrupt(wakeUpPin, InteruptServiceRoutine, FALLING); |
||||
} |
||||
|
||||
// void loop() |
||||
// { |
||||
// digitalWrite(ledPin, !digitalRead(ledPin)); |
||||
// Serial.println("interrupred"); |
||||
|
||||
// sleepNow(); |
||||
// } |
||||
|
||||
void loop () |
||||
{ |
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
} |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
|
||||
printDateTime(now); |
||||
Serial.println(); |
||||
|
||||
// we only want to show time every 10 seconds |
||||
// but we want to show responce to the interupt firing |
||||
for (int timeCount = 0; timeCount < 20; timeCount++) |
||||
{ |
||||
if (Alarmed()) |
||||
{ |
||||
Serial.print(">>Interupt Count: "); |
||||
Serial.print(interuptCount); |
||||
Serial.println("<<"); |
||||
digitalWrite(ledPin, !digitalRead(ledPin)); |
||||
} |
||||
delay(500); |
||||
} |
||||
|
||||
// sleepNow(); |
||||
} |
||||
|
||||
bool Alarmed() |
||||
{ |
||||
bool wasAlarmed = false; |
||||
if (interuptFlag) // check our flag that gets sets in the interupt |
||||
{ |
||||
wasAlarmed = true; |
||||
interuptFlag = false; // reset the flag |
||||
|
||||
// this gives us which alarms triggered and |
||||
// then allows for others to trigger again |
||||
DS3231AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
if (flag & DS3231AlarmFlag_Alarm1) |
||||
{ |
||||
Serial.println("alarm one triggered"); |
||||
} |
||||
if (flag & DS3231AlarmFlag_Alarm2) |
||||
{ |
||||
Serial.println("alarm two triggered"); |
||||
} |
||||
} |
||||
return wasAlarmed; |
||||
} |
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0])) |
||||
|
||||
void printDateTime(const RtcDateTime& dt) |
||||
{ |
||||
char datestring[20]; |
||||
|
||||
snprintf_P(datestring, |
||||
countof(datestring), |
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"), |
||||
dt.Month(), |
||||
dt.Day(), |
||||
dt.Year(), |
||||
dt.Hour(), |
||||
dt.Minute(), |
||||
dt.Second() ); |
||||
Serial.print(datestring); |
||||
} |
||||
|
||||
// // CONNECTIONS: |
||||
// // DS3231 SDA --> SDA |
||||
// // DS3231 SCL --> SCL |
||||
// // DS3231 VCC --> 3.3v or 5v |
||||
// // DS3231 GND --> GND |
||||
// // SQW ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC) |
||||
|
||||
// /* for software wire use below |
||||
// #include <SoftwareWire.h> // must be included here so that Arduino library object file references work |
||||
// #include <RtcDS3231.h> |
||||
|
||||
// SoftwareWire myWire(SDA, SCL); |
||||
// RtcDS3231<SoftwareWire> Rtc(myWire); |
||||
// for software wire use above */ |
||||
|
||||
// /* for normal hardware wire use below */ |
||||
// #include <Wire.h> // must be included here so that Arduino library object file references work |
||||
// #include <RtcDS3231.h> |
||||
// RtcDS3231<TwoWire> Rtc(Wire); |
||||
// /* for normal hardware wire use above */ |
||||
|
||||
// bool Alarmed(); |
||||
// void printDateTime(const RtcDateTime& dt); |
||||
// const byte LED = 17; |
||||
|
||||
// // Interrupt Pin Lookup Table |
||||
// // (copied from Arduino Docs) |
||||
// // |
||||
// // CAUTION: The interrupts are Arduino numbers NOT Atmel numbers |
||||
// // and may not match (example, Mega2560 int.4 is actually Atmel Int2) |
||||
// // this is only an issue if you plan to use the lower level interupt features |
||||
// // |
||||
// // Board int.0 int.1 int.2 int.3 int.4 int.5 |
||||
// // --------------------------------------------------------------- |
||||
// // Uno, Ethernet 2 3 |
||||
// // Mega2560 2 3 21 20 [19] 18 |
||||
// // Leonardo 3 2 0 1 7 |
||||
|
||||
// #define RtcSquareWavePin 7 // Mega2560 |
||||
// #define RtcSquareWaveInterrupt 4 // Mega2560 |
||||
|
||||
// // marked volatile so interrupt can safely modify them and |
||||
// // other code can safely read and modify them |
||||
// volatile uint16_t interuptCount = 0; |
||||
// volatile bool interuptFlag = false; |
||||
|
||||
// void ISR_ATTR InteruptServiceRoutine() |
||||
// { |
||||
// // since this interupted any other running code, |
||||
// // don't do anything that takes long and especially avoid |
||||
// // any communications calls within this routine |
||||
// interuptCount++; |
||||
// interuptFlag = true; |
||||
// } |
||||
|
||||
// void setup () |
||||
// { |
||||
// Serial.begin(9600); |
||||
|
||||
// // set the interupt pin to input mode |
||||
// pinMode(RtcSquareWavePin, INPUT); |
||||
|
||||
// //--------RTC SETUP ------------ |
||||
// // if you are using ESP-01 then uncomment the line below to reset the pins to |
||||
// // the available pins for SDA, SCL |
||||
// // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL |
||||
|
||||
// Rtc.Begin(); |
||||
|
||||
// RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
||||
|
||||
// if (!Rtc.IsDateTimeValid()) |
||||
// { |
||||
// if (Rtc.LastError() != 0) |
||||
// { |
||||
// // we have a communications error |
||||
// // see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// // what the number means |
||||
// Serial.print("RTC communications error = "); |
||||
// Serial.println(Rtc.LastError()); |
||||
// } |
||||
// else |
||||
// { |
||||
// Serial.println("RTC lost confidence in the DateTime!"); |
||||
// Rtc.SetDateTime(compiled); |
||||
// } |
||||
// } |
||||
|
||||
// if (!Rtc.GetIsRunning()) |
||||
// { |
||||
// Serial.println("RTC was not actively running, starting now"); |
||||
// Rtc.SetIsRunning(true); |
||||
// } |
||||
|
||||
// RtcDateTime now = Rtc.GetDateTime(); |
||||
// if (now < compiled) |
||||
// { |
||||
// Serial.println("RTC is older than compile time! (Updating DateTime)"); |
||||
// Rtc.SetDateTime(compiled); |
||||
// } |
||||
|
||||
// Rtc.Enable32kHzPin(false); |
||||
// Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmBoth); |
||||
|
||||
// // Alarm 1 set to trigger every day when |
||||
// // the hours, minutes, and seconds match |
||||
// RtcDateTime alarmTime = now + 88; // into the future |
||||
// DS3231AlarmOne alarm1( |
||||
// alarmTime.Day(), |
||||
// alarmTime.Hour(), |
||||
// alarmTime.Minute(), |
||||
// alarmTime.Second(), |
||||
// DS3231AlarmOneControl_HoursMinutesSecondsMatch); |
||||
// Rtc.SetAlarmOne(alarm1); |
||||
|
||||
// // Alarm 2 set to trigger at the top of the minute |
||||
// DS3231AlarmTwo alarm2( |
||||
// 0, |
||||
// 0, |
||||
// 0, |
||||
// DS3231AlarmTwoControl_OncePerMinute); |
||||
// Rtc.SetAlarmTwo(alarm2); |
||||
|
||||
// // throw away any old alarm state before we ran |
||||
// Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
// // setup external interupt |
||||
// attachInterrupt(RtcSquareWaveInterrupt, InteruptServiceRoutine, FALLING); |
||||
// } |
||||
|
||||
// void loop () |
||||
// { |
||||
// if (!Rtc.IsDateTimeValid()) |
||||
// { |
||||
// if (Rtc.LastError() != 0) |
||||
// { |
||||
// // we have a communications error |
||||
// // see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// // what the number means |
||||
// Serial.print("RTC communications error = "); |
||||
// Serial.println(Rtc.LastError()); |
||||
// } |
||||
// else |
||||
// { |
||||
// Serial.println("RTC lost confidence in the DateTime!"); |
||||
// } |
||||
// } |
||||
|
||||
// RtcDateTime now = Rtc.GetDateTime(); |
||||
|
||||
// printDateTime(now); |
||||
// Serial.println(); |
||||
|
||||
// // we only want to show time every 10 seconds |
||||
// // but we want to show responce to the interupt firing |
||||
// for (int timeCount = 0; timeCount < 20; timeCount++) |
||||
// { |
||||
// if (Alarmed()) |
||||
// { |
||||
// Serial.print(">>Interupt Count: "); |
||||
// Serial.print(interuptCount); |
||||
// Serial.println("<<"); |
||||
// digitalWrite(LED, !digitalRead(LED)); |
||||
// } |
||||
// delay(500); |
||||
// } |
||||
// } |
||||
|
||||
// bool Alarmed() |
||||
// { |
||||
// bool wasAlarmed = false; |
||||
// if (interuptFlag) // check our flag that gets sets in the interupt |
||||
// { |
||||
// wasAlarmed = true; |
||||
// interuptFlag = false; // reset the flag |
||||
|
||||
// // this gives us which alarms triggered and |
||||
// // then allows for others to trigger again |
||||
// DS3231AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
// if (flag & DS3231AlarmFlag_Alarm1) |
||||
// { |
||||
// Serial.println("alarm one triggered"); |
||||
// } |
||||
// if (flag & DS3231AlarmFlag_Alarm2) |
||||
// { |
||||
// Serial.println("alarm two triggered"); |
||||
// } |
||||
// } |
||||
// return wasAlarmed; |
||||
// } |
||||
|
||||
// #define countof(a) (sizeof(a) / sizeof(a[0])) |
||||
|
||||
// void printDateTime(const RtcDateTime& dt) |
||||
// { |
||||
// char datestring[20]; |
||||
|
||||
// snprintf_P(datestring, |
||||
// countof(datestring), |
||||
// PSTR("%02u/%02u/%04u %02u:%02u:%02u"), |
||||
// dt.Month(), |
||||
// dt.Day(), |
||||
// dt.Year(), |
||||
// dt.Hour(), |
||||
// dt.Minute(), |
||||
// dt.Second() ); |
||||
// Serial.print(datestring); |
||||
// } |
||||
|
||||
// // #include <Arduino.h> |
||||
// // // **** INCLUDES ***** |
||||
// // #include "LowPower.h" |
||||
|
||||
// // // Use pin 2 as wake up pin |
||||
// // const int wakeUpPin = 7; |
||||
// // const int ledPin = 17; |
||||
|
||||
// // void wakeUp() |
||||
// // { |
||||
// // // Just a handler for the pin interrupt. |
||||
// // } |
||||
|
||||
// // void setup() |
||||
// // { |
||||
// // // Configure wake up pin as input. |
||||
// // // This will consumes few uA of current. |
||||
// // pinMode(wakeUpPin, INPUT); |
||||
// // } |
||||
|
||||
// // void loop() |
||||
// // { |
||||
// // // Allow wake up pin to trigger interrupt on low. |
||||
// // attachInterrupt(digitalPinToInterrupt(wakeUpPin), wakeUp, LOW); |
||||
|
||||
// // // Enter power down state with ADC and BOD module disabled. |
||||
// // // Wake up when wake up pin is low. |
||||
// // LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); |
||||
|
||||
// // // Disable external pin interrupt on wake up pin. |
||||
// // detachInterrupt(digitalPinToInterrupt(wakeUpPin)); |
||||
|
||||
// // // Do something here |
||||
// // // Example: Read sensor, data logging, data transmission. |
||||
// // pinMode(ledPin, OUTPUT); |
||||
// // delay(100); |
||||
// // digitalWrite(ledPin, HIGH); |
||||
// // delay(100); |
||||
// // digitalWrite(ledPin, LOW); |
||||
// // delay(100); |
||||
// // pinMode(ledPin, INPUT); |
||||
// // } |
||||
// |
@ -0,0 +1,138 @@ |
||||
/** |
||||
* |
||||
* HX711 library for Arduino - example file |
||||
* https://github.com/bogde/HX711 |
||||
* |
||||
* MIT License |
||||
* (c) 2018 Bogdan Necula |
||||
* |
||||
**/ |
||||
|
||||
#include "HX711.h" |
||||
|
||||
|
||||
// HX711 circuit wiring |
||||
const int LOADCELL_DOUT_PIN = 21; |
||||
const int LOADCELL_SCK_PIN = 20; |
||||
|
||||
|
||||
HX711 scale; |
||||
|
||||
void setup() { |
||||
Serial.begin(38400); |
||||
Serial.println("HX711 Demo"); |
||||
|
||||
Serial.println("Initializing the scale"); |
||||
|
||||
// Initialize library with data output pin, clock input pin and gain factor. |
||||
// Channel selection is made by passing the appropriate gain: |
||||
// - With a gain factor of 64 or 128, channel A is selected |
||||
// - With a gain factor of 32, channel B is selected |
||||
// By omitting the gain factor parameter, the library |
||||
// default "128" (Channel A) is used here. |
||||
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); |
||||
|
||||
Serial.println("Before setting up the scale:"); |
||||
Serial.print("read: \t\t"); |
||||
Serial.println(scale.read()); // print a raw reading from the ADC |
||||
|
||||
Serial.print("read average: \t\t"); |
||||
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC |
||||
|
||||
Serial.print("get value: \t\t"); |
||||
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet) |
||||
|
||||
Serial.print("get units: \t\t"); |
||||
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided |
||||
// by the SCALE parameter (not set yet) |
||||
|
||||
scale.set_scale(1000.f); // this value is obtained by calibrating the scale with known weights; see the README for details |
||||
scale.tare(); // reset the scale to 0 |
||||
|
||||
Serial.println("After setting up the scale:"); |
||||
|
||||
Serial.print("read: \t\t"); |
||||
Serial.println(scale.read()); // print a raw reading from the ADC |
||||
|
||||
Serial.print("read average: \t\t"); |
||||
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC |
||||
|
||||
Serial.print("get value: \t\t"); |
||||
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare() |
||||
|
||||
Serial.print("get units: \t\t"); |
||||
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided |
||||
// by the SCALE parameter set with set_scale |
||||
|
||||
Serial.println("Readings:"); |
||||
} |
||||
|
||||
void loop() { |
||||
Serial.print("one reading:\t"); |
||||
Serial.print(scale.get_units(), 1); |
||||
Serial.print("\t| average:\t"); |
||||
Serial.println(scale.get_units(10), 1); |
||||
|
||||
scale.power_down(); // put the ADC in sleep mode |
||||
delay(1000); |
||||
scale.power_up(); |
||||
} |
||||
|
||||
|
||||
|
||||
/* #include <Arduino.h> |
||||
#include <HX711.h> |
||||
#include <RtcDS3231.h> |
||||
|
||||
|
||||
|
||||
int RXLED = 17; // The RX LED has a defined Arduino pin |
||||
// Note: The TX LED was not so lucky, we'll need to use pre-defined |
||||
// macros (TXLED1, TXLED0) to control that. |
||||
// (We could use the same macros for the RX LED too -- RXLED1, |
||||
// and RXLED0.) |
||||
|
||||
// 1. HX711 circuit wiring |
||||
const int LOADCELL_DOUT_PIN = 21; |
||||
const int LOADCELL_SCK_PIN = 20; |
||||
|
||||
// 2. Adjustment settings |
||||
const long LOADCELL_OFFSET = 50682624; |
||||
const long LOADCELL_DIVIDER = 5895655; |
||||
|
||||
HX711 loadcell; |
||||
|
||||
void setup() |
||||
{ |
||||
pinMode(RXLED, OUTPUT); // Set RX LED as an output |
||||
// TX LED is set as an output behind the scenes |
||||
|
||||
Serial.begin(9600); //This pipes to the serial monitor |
||||
Serial.println("Initialize Serial Monitor"); |
||||
|
||||
Serial1.begin(9600); //This is the UART, pipes to sensors attached to board |
||||
Serial1.println("Initialize Serial Hardware UART Pins"); |
||||
|
||||
// 3. Initialize library |
||||
loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); |
||||
loadcell.set_scale(LOADCELL_DIVIDER); |
||||
loadcell.set_offset(LOADCELL_OFFSET); |
||||
} |
||||
|
||||
void loop() |
||||
{ |
||||
// Serial.println("Hello world!!!"); // Print "Hello World" to the Serial Monitor |
||||
// Serial1.println("Hello! Can anybody hear me?"); // Print "Hello!" over hardware UART |
||||
|
||||
// 4. Acquire reading |
||||
Serial.print("Weight: "); |
||||
Serial.println(loadcell.get_units(10), 2); |
||||
|
||||
digitalWrite(RXLED, LOW); // set the RX LED ON |
||||
TXLED0; //TX LED is not tied to a normally controlled pin so a macro is needed, turn LED OFF |
||||
delay(200); // wait for a second |
||||
|
||||
digitalWrite(RXLED, HIGH); // set the RX LED OFF |
||||
TXLED1; //TX LED macro to turn LED ON |
||||
delay(200); // wait for a second |
||||
} */ |
@ -0,0 +1,197 @@ |
||||
// CONNECTIONS: |
||||
// DS1307 SDA --> SDA |
||||
// DS1307 SCL --> SCL |
||||
// DS1307 VCC --> 5v |
||||
// DS1307 GND --> GND |
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0])) |
||||
|
||||
/* for software wire use below |
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
#include <EepromAT24C32.h> |
||||
|
||||
SoftwareWire myWire(SDA, SCL); |
||||
RtcDS1307<SoftwareWire> Rtc(myWire); |
||||
for software wire use above |
||||
|
||||
for normal hardware wire use below */ |
||||
#include <Wire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
#include <EepromAT24C32.h> |
||||
|
||||
RtcDS3231<TwoWire> Rtc(Wire); |
||||
EepromAt24c32<TwoWire> RtcEeprom(Wire); |
||||
|
||||
// if you have any of the address pins on the RTC soldered together |
||||
// then you need to provide the state of those pins, normally they |
||||
// are connected to vcc with a reading of 1, if soldered they are |
||||
// grounded with a reading of 0. The bits are in the order A2 A1 A0 |
||||
// thus the following would have the A2 soldered together |
||||
// EepromAt24c32<TwoWire> RtcEeprom(Wire, 0b011); |
||||
|
||||
/* for normal hardware wire use above */ |
||||
|
||||
// nothing longer than 32 bytes |
||||
// rtc eeprom memory is 32 byte pages |
||||
// writing is limited to each page, so it will wrap at page |
||||
// boundaries. |
||||
// But reading is only limited by the buffer in Wire class which |
||||
// by default is 32 |
||||
const char data[] = "What time is it in Greenwich?"; |
||||
const uint16_t stringAddr = 64; // stored on page boundary |
||||
|
||||
void printDateTime(const RtcDateTime& dt); |
||||
|
||||
void setup () |
||||
{ |
||||
Serial.begin(9600); |
||||
|
||||
Serial.print("compiled: "); |
||||
Serial.print(__DATE__); |
||||
Serial.println(__TIME__); |
||||
|
||||
//--------RTC SETUP ------------ |
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to |
||||
// the available pins for SDA, SCL |
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL |
||||
|
||||
Rtc.Begin(); |
||||
RtcEeprom.Begin(); |
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
||||
printDateTime(compiled); |
||||
Serial.println(); |
||||
|
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
} |
||||
|
||||
if (!Rtc.GetIsRunning()) |
||||
{ |
||||
Serial.println("RTC was not actively running, starting now"); |
||||
Rtc.SetIsRunning(true); |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
if (now < compiled) |
||||
{ |
||||
Serial.println("RTC is older than compile time! (Updating DateTime)"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
|
||||
// never assume the Rtc was last configured by you, so |
||||
// just clear them to your needed state |
||||
Rtc.Enable32kHzPin(false); |
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); |
||||
|
||||
/* comment out on a second run to see that the info is stored long term */ |
||||
// // Store something in memory on the Eeprom |
||||
|
||||
// // store starting address of string |
||||
// RtcEeprom.SetMemory(0, stringAddr); |
||||
// // store the string, nothing longer than 32 bytes due to paging |
||||
// uint8_t written = RtcEeprom.SetMemory(stringAddr, (const uint8_t*)data, sizeof(data) - 1); // remove the null terminator strings add |
||||
// // store the length of the string |
||||
// RtcEeprom.SetMemory(1, written); // store the |
||||
/* end of comment out section */ |
||||
} |
||||
|
||||
void loop () |
||||
{ |
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
// Common Causes: |
||||
// 1) the battery on the device is low or even missing and the power line was disconnected |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
} |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
|
||||
printDateTime(now); |
||||
Serial.println(); |
||||
|
||||
delay(5000); |
||||
|
||||
// read data |
||||
|
||||
// get the offset we stored our data from address zero |
||||
uint8_t address = RtcEeprom.GetMemory(0); |
||||
if (address != stringAddr) |
||||
{ |
||||
Serial.print("address didn't match "); |
||||
Serial.println(address); |
||||
} |
||||
|
||||
{ |
||||
// get the size of the data from address 1 |
||||
uint8_t count = RtcEeprom.GetMemory(1); |
||||
uint8_t buff[64]; |
||||
|
||||
// get our data from the address with the given size |
||||
uint8_t gotten = RtcEeprom.GetMemory(address, buff, count); |
||||
|
||||
if (gotten != count || |
||||
count != sizeof(data) - 1) // remove the extra null terminator strings add |
||||
{ |
||||
Serial.print("something didn't match, count = "); |
||||
Serial.print(count, DEC); |
||||
Serial.print(", gotten = "); |
||||
Serial.print(gotten, DEC); |
||||
Serial.println(); |
||||
} |
||||
Serial.print("data read ("); |
||||
Serial.print(gotten); |
||||
Serial.print(") = \""); |
||||
for (uint8_t ch = 0; ch < gotten; ch++) |
||||
{ |
||||
Serial.print((char)buff[ch]); |
||||
} |
||||
Serial.println("\""); |
||||
} |
||||
|
||||
|
||||
delay(5000); |
||||
} |
||||
|
||||
|
||||
|
||||
void printDateTime(const RtcDateTime& dt) |
||||
{ |
||||
char datestring[20]; |
||||
|
||||
snprintf_P(datestring, |
||||
countof(datestring), |
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"), |
||||
dt.Month(), |
||||
dt.Day(), |
||||
dt.Year(), |
||||
dt.Hour(), |
||||
dt.Minute(), |
||||
dt.Second() ); |
||||
Serial.print(datestring); |
||||
} |
@ -0,0 +1,213 @@ |
||||
|
||||
// CONNECTIONS: |
||||
// DS3231 SDA --> SDA |
||||
// DS3231 SCL --> SCL |
||||
// DS3231 VCC --> 3.3v or 5v |
||||
// DS3231 GND --> GND |
||||
// SQW ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC) |
||||
|
||||
/* for software wire use below |
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
|
||||
SoftwareWire myWire(SDA, SCL); |
||||
RtcDS3231<SoftwareWire> Rtc(myWire); |
||||
for software wire use above */ |
||||
|
||||
/* for normal hardware wire use below */ |
||||
#include <Wire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
RtcDS3231<TwoWire> Rtc(Wire); |
||||
/* for normal hardware wire use above */ |
||||
|
||||
|
||||
bool Alarmed(); |
||||
void printDateTime(const RtcDateTime& dt); |
||||
const byte LED = 17; |
||||
|
||||
// Interrupt Pin Lookup Table |
||||
// (copied from Arduino Docs) |
||||
// |
||||
// CAUTION: The interrupts are Arduino numbers NOT Atmel numbers |
||||
// and may not match (example, Mega2560 int.4 is actually Atmel Int2) |
||||
// this is only an issue if you plan to use the lower level interupt features |
||||
// |
||||
// Board int.0 int.1 int.2 int.3 int.4 int.5 |
||||
// --------------------------------------------------------------- |
||||
// Uno, Ethernet 2 3 |
||||
// Mega2560 2 3 21 20 [19] 18 |
||||
// Leonardo 3 2 0 1 7 |
||||
|
||||
#define RtcSquareWavePin 7 // Mega2560 |
||||
#define RtcSquareWaveInterrupt 4 // Mega2560 |
||||
|
||||
// marked volatile so interrupt can safely modify them and |
||||
// other code can safely read and modify them |
||||
volatile uint16_t interuptCount = 0; |
||||
volatile bool interuptFlag = false; |
||||
|
||||
void ISR_ATTR InteruptServiceRoutine() |
||||
{ |
||||
// since this interupted any other running code, |
||||
// don't do anything that takes long and especially avoid |
||||
// any communications calls within this routine |
||||
interuptCount++; |
||||
interuptFlag = true; |
||||
} |
||||
|
||||
void setup () |
||||
{ |
||||
Serial.begin(9600); |
||||
|
||||
// set the interupt pin to input mode |
||||
pinMode(RtcSquareWavePin, INPUT); |
||||
|
||||
//--------RTC SETUP ------------ |
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to |
||||
// the available pins for SDA, SCL |
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL |
||||
|
||||
Rtc.Begin(); |
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
||||
|
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
} |
||||
|
||||
if (!Rtc.GetIsRunning()) |
||||
{ |
||||
Serial.println("RTC was not actively running, starting now"); |
||||
Rtc.SetIsRunning(true); |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
if (now < compiled) |
||||
{ |
||||
Serial.println("RTC is older than compile time! (Updating DateTime)"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
|
||||
Rtc.Enable32kHzPin(false); |
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmBoth); |
||||
|
||||
// Alarm 1 set to trigger every day when |
||||
// the hours, minutes, and seconds match |
||||
RtcDateTime alarmTime = now + 18; // into the future |
||||
DS3231AlarmOne alarm1( |
||||
alarmTime.Day(), |
||||
alarmTime.Hour(), |
||||
alarmTime.Minute(), |
||||
alarmTime.Second(), |
||||
DS3231AlarmOneControl_HoursMinutesSecondsMatch); |
||||
Rtc.SetAlarmOne(alarm1); |
||||
|
||||
// Alarm 2 set to trigger at the top of the minute |
||||
DS3231AlarmTwo alarm2( |
||||
0, |
||||
0, |
||||
0, |
||||
DS3231AlarmTwoControl_OncePerMinute); |
||||
Rtc.SetAlarmTwo(alarm2); |
||||
|
||||
// throw away any old alarm state before we ran |
||||
Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
// setup external interupt |
||||
attachInterrupt(RtcSquareWaveInterrupt, InteruptServiceRoutine, FALLING); |
||||
} |
||||
|
||||
void loop () |
||||
{ |
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
} |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
|
||||
printDateTime(now); |
||||
Serial.println(); |
||||
|
||||
// we only want to show time every 10 seconds |
||||
// but we want to show responce to the interupt firing |
||||
for (int timeCount = 0; timeCount < 20; timeCount++) |
||||
{ |
||||
if (Alarmed()) |
||||
{ |
||||
Serial.print(">>Interupt Count: "); |
||||
Serial.print(interuptCount); |
||||
Serial.println("<<"); |
||||
digitalWrite(LED, !digitalRead(LED)); |
||||
} |
||||
delay(500); |
||||
} |
||||
} |
||||
|
||||
bool Alarmed() |
||||
{ |
||||
bool wasAlarmed = false; |
||||
if (interuptFlag) // check our flag that gets sets in the interupt |
||||
{ |
||||
wasAlarmed = true; |
||||
interuptFlag = false; // reset the flag |
||||
|
||||
// this gives us which alarms triggered and |
||||
// then allows for others to trigger again |
||||
DS3231AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
if (flag & DS3231AlarmFlag_Alarm1) |
||||
{ |
||||
Serial.println("alarm one triggered"); |
||||
} |
||||
if (flag & DS3231AlarmFlag_Alarm2) |
||||
{ |
||||
Serial.println("alarm two triggered"); |
||||
} |
||||
} |
||||
return wasAlarmed; |
||||
} |
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0])) |
||||
|
||||
void printDateTime(const RtcDateTime& dt) |
||||
{ |
||||
char datestring[20]; |
||||
|
||||
snprintf_P(datestring, |
||||
countof(datestring), |
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"), |
||||
dt.Month(), |
||||
dt.Day(), |
||||
dt.Year(), |
||||
dt.Hour(), |
||||
dt.Minute(), |
||||
dt.Second() ); |
||||
Serial.print(datestring); |
||||
} |
||||
|
||||
|
@ -0,0 +1,259 @@ |
||||
#include <Arduino.h> |
||||
#include <avr/sleep.h> |
||||
|
||||
// const byte wakeUpPin = 7; |
||||
const byte ledPin = 17; |
||||
const byte RtcSquareWavePin = 7; |
||||
// const byte RtcSquareWaveInterrupt = digitalPinToInterrupt(RtcSquareWavePin); |
||||
|
||||
void wake() |
||||
{ |
||||
sleep_disable(); |
||||
detachInterrupt(digitalPinToInterrupt(RtcSquareWavePin)); |
||||
} |
||||
|
||||
void sleepNow() |
||||
{ |
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); |
||||
noInterrupts(); |
||||
sleep_enable(); |
||||
attachInterrupt(digitalPinToInterrupt(RtcSquareWavePin), wake, LOW); |
||||
interrupts(); |
||||
sleep_cpu(); |
||||
} |
||||
|
||||
// void setup() |
||||
// { |
||||
// pinMode(wakeUpPin, INPUT_PULLUP); |
||||
// } |
||||
|
||||
// void loop() |
||||
// { |
||||
// pinMode(ledPin, OUTPUT); |
||||
// delay(200); |
||||
// digitalWrite(ledPin, HIGH); |
||||
// delay(500); |
||||
// digitalWrite(ledPin, LOW); |
||||
// delay(200); |
||||
// pinMode(ledPin, INPUT); |
||||
// sleepNow(); |
||||
// } |
||||
|
||||
// CONNECTIONS: |
||||
// DS3231 SDA --> SDA |
||||
// DS3231 SCL --> SCL |
||||
// DS3231 VCC --> 3.3v or 5v |
||||
// DS3231 GND --> GND |
||||
// SQW ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC) |
||||
|
||||
/* for software wire use below |
||||
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
|
||||
SoftwareWire myWire(SDA, SCL); |
||||
RtcDS3231<SoftwareWire> Rtc(myWire); |
||||
for software wire use above */ |
||||
|
||||
/* for normal hardware wire use below */ |
||||
#include <Wire.h> // must be included here so that Arduino library object file references work |
||||
#include <RtcDS3231.h> |
||||
RtcDS3231<TwoWire> Rtc(Wire); |
||||
/* for normal hardware wire use above */ |
||||
|
||||
bool Alarmed(); |
||||
void printDateTime(const RtcDateTime &dt); |
||||
|
||||
// Interrupt Pin Lookup Table |
||||
// (copied from Arduino Docs) |
||||
// |
||||
// CAUTION: The interrupts are Arduino numbers NOT Atmel numbers |
||||
// and may not match (example, Mega2560 int.4 is actually Atmel Int2) |
||||
// this is only an issue if you plan to use the lower level interupt features |
||||
// |
||||
// Board int.0 int.1 int.2 int.3 int.4 int.5 |
||||
// --------------------------------------------------------------- |
||||
// Uno, Ethernet 2 3 |
||||
// Mega2560 2 3 21 20 [19] 18 |
||||
// Leonardo 3 2 0 1 7 |
||||
|
||||
; |
||||
|
||||
// marked volatile so interrupt can safely modify them and |
||||
// other code can safely read and modify them |
||||
volatile uint16_t interuptCount = 0; |
||||
volatile bool interuptFlag = false; |
||||
|
||||
void ISR_ATTR InteruptServiceRoutine() |
||||
{ |
||||
// since this interupted any other running code, |
||||
// don't do anything that takes long and especially avoid |
||||
// any communications calls within this routine |
||||
interuptCount++; |
||||
interuptFlag = true; |
||||
} |
||||
|
||||
void setup() |
||||
{ |
||||
Serial.begin(9600); |
||||
|
||||
// set the interupt pin to input mode |
||||
pinMode(RtcSquareWavePin, INPUT); |
||||
|
||||
//--------RTC SETUP ------------ |
||||
// if you are using ESP-01 then uncomment the line below to reset the pins to |
||||
// the available pins for SDA, SCL |
||||
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL |
||||
|
||||
Rtc.Begin(); |
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); |
||||
|
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
} |
||||
|
||||
if (!Rtc.GetIsRunning()) |
||||
{ |
||||
Serial.println("RTC was not actively running, starting now"); |
||||
Rtc.SetIsRunning(true); |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
if (now < compiled) |
||||
{ |
||||
Serial.println("RTC is older than compile time! (Updating DateTime)"); |
||||
Rtc.SetDateTime(compiled); |
||||
} |
||||
|
||||
Rtc.Enable32kHzPin(false); |
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmBoth); |
||||
|
||||
// Alarm 1 set to trigger every day when |
||||
// the hours, minutes, and seconds match |
||||
RtcDateTime alarmTime = now + 88; // into the future |
||||
DS3231AlarmOne alarm1( |
||||
alarmTime.Day(), |
||||
alarmTime.Hour(), |
||||
alarmTime.Minute(), |
||||
alarmTime.Second(), |
||||
DS3231AlarmOneControl_HoursMinutesSecondsMatch); |
||||
Rtc.SetAlarmOne(alarm1); |
||||
|
||||
// Alarm 2 set to trigger at the top of the minute |
||||
DS3231AlarmTwo alarm2( |
||||
0, |
||||
0, |
||||
0, |
||||
DS3231AlarmTwoControl_OncePerMinute); |
||||
Rtc.SetAlarmTwo(alarm2); |
||||
|
||||
// throw away any old alarm state before we ran |
||||
Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
// setup external interupt |
||||
pinMode(RtcSquareWavePin, INPUT_PULLUP); |
||||
attachInterrupt(digitalPinToInterrupt(RtcSquareWavePin), InteruptServiceRoutine, LOW); |
||||
} |
||||
|
||||
void loop() |
||||
{ |
||||
if (!Rtc.IsDateTimeValid()) |
||||
{ |
||||
if (Rtc.LastError() != 0) |
||||
{ |
||||
// we have a communications error |
||||
// see https://www.arduino.cc/en/Reference/WireEndTransmission for |
||||
// what the number means |
||||
Serial.print("RTC communications error = "); |
||||
Serial.println(Rtc.LastError()); |
||||
} |
||||
else |
||||
{ |
||||
Serial.println("RTC lost confidence in the DateTime!"); |
||||
} |
||||
} |
||||
|
||||
RtcDateTime now = Rtc.GetDateTime(); |
||||
|
||||
printDateTime(now); |
||||
Serial.println(); |
||||
|
||||
pinMode(ledPin, OUTPUT); |
||||
delay(200); |
||||
digitalWrite(ledPin, HIGH); |
||||
delay(500); |
||||
digitalWrite(ledPin, LOW); |
||||
delay(200); |
||||
pinMode(ledPin, INPUT); |
||||
|
||||
// sleepNow(); |
||||
|
||||
// we only want to show time every 10 seconds |
||||
// but we want to show responce to the interupt firing |
||||
for (int timeCount = 0; timeCount < 20; timeCount++) |
||||
{ |
||||
if (Alarmed()) |
||||
{ |
||||
Serial.print(">>Interupt Count: "); |
||||
Serial.print(interuptCount); |
||||
Serial.println("<<"); |
||||
digitalWrite(ledPin, !digitalRead(ledPin)); |
||||
} |
||||
delay(500); |
||||
} |
||||
} |
||||
|
||||
bool Alarmed() |
||||
{ |
||||
bool wasAlarmed = false; |
||||
if (interuptFlag) // check our flag that gets sets in the interupt |
||||
{ |
||||
wasAlarmed = true; |
||||
interuptFlag = false; // reset the flag |
||||
|
||||
// this gives us which alarms triggered and |
||||
// then allows for others to trigger again |
||||
DS3231AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags(); |
||||
|
||||
if (flag & DS3231AlarmFlag_Alarm1) |
||||
{ |
||||
Serial.println("alarm one triggered"); |
||||
} |
||||
if (flag & DS3231AlarmFlag_Alarm2) |
||||
{ |
||||
Serial.println("alarm two triggered"); |
||||
} |
||||
} |
||||
return wasAlarmed; |
||||
} |
||||
|
||||
#define countof(a) (sizeof(a) / sizeof(a[0])) |
||||
|
||||
void printDateTime(const RtcDateTime &dt) |
||||
{ |
||||
char datestring[20]; |
||||
|
||||
snprintf_P(datestring, |
||||
countof(datestring), |
||||
PSTR("%02u/%02u/%04u %02u:%02u:%02u"), |
||||
dt.Month(), |
||||
dt.Day(), |
||||
dt.Year(), |
||||
dt.Hour(), |
||||
dt.Minute(), |
||||
dt.Second()); |
||||
Serial.print(datestring); |
||||
} |
Loading…
Reference in new issue