/*
 * Smart Buoy - Part 3 - T3ch Flicks
 * This programme will turn on the Arduino's built in LED for 3 seconds and then 
 * set an alarm on the RTC module to occur 15 seconds later. If the
 * system is set up as per the schematic, then power to the Arduino will
 * be given when the alarm is triggered and stopped when the alarm is cleared.
 */

#include <DS3232RTC.h>
#include <Wire.h> 

int led = LED_BUILTIN; 

void setup() {
  Serial.begin(115200);
  Wire.begin();
  pinMode(led, OUTPUT);
}

void loop() {
  
  digitalWrite(led, HIGH);
  delay(3000);
  reset_alarm();
}

void reset_alarm(){
    Wire.beginTransmission(0x70);
  Wire.write(1 << 6);
  Wire.endTransmission();
  RTC.alarmInterrupt(ALARM_1, true); // alarm is an output trigger
  RTC.squareWave(SQWAVE_NONE);
  //      0h 0m 0s 
  setTime(0, 0, 0, 1, 1, 1970); 
  RTC.set(now());
  // set new alarm
  RTC.setAlarm(ALM1_MATCH_SECONDS, 15, 0, 0, 1);
  // clear old alarm flag - turning off system
  RTC.alarm(ALARM_1);
}



