Bim bam

Submitted by robert on Sat, 10/13/2018 - 13:02

Don't mind the dirty code please.

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

// baudrate 9600
// arduino nano

#define DEBUG /* extra serial data */

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB
#endif

RTC_DS1307 rtc;

//enum Ds1307SqwPinMode { OFF = 0x00, ON = 0x80, SquareWave1HZ = 0x10, SquareWave4kHz = 0x11, SquareWave8kHz = 0x12, SquareWave32kHz = 0x13 };

#define BAMPIN 3 // Pin D3
#define BAMDELAY 50 // duration of solenoid action
#define BAMDELAYDOUBLE 750
char bam = 0;
int ser = 0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void print_mode() {
  Ds1307SqwPinMode mode = rtc.readSqwPinMode();
 
  Serial.print("Sqw Pin Mode: ");
  switch(mode) {
  case OFF:             Serial.println("OFF");       break;
  case ON:              Serial.println("ON");        break;
  case SquareWave1HZ:   Serial.println("1Hz");       break;
  case SquareWave4kHz:  Serial.println("4.096kHz");  break;
  case SquareWave8kHz:  Serial.println("8.192kHz");  break;
  case SquareWave32kHz: Serial.println("32.768kHz"); break;
  default:              Serial.println("UNKNOWN");   break;
  }
}

void bimbam(){
 digitalWrite(BAMPIN, HIGH);
 delay(BAMDELAY);
 digitalWrite(BAMPIN, LOW);
 Serial.println("Bom");
}


void setup () {

pinMode(BAMPIN, OUTPUT); // set bim bam pin as output

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  Serial.begin(9600);
  Serial.println("Begin Bim Bam");
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
delay(100);
rtc.writeSqwPinMode(SquareWave1HZ);
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled

    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
 //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
    if (Serial.available() > 0) {
    // get incoming byte:
    ser = Serial.read();
    bimbam(); delay(BAMDELAYDOUBLE); bimbam();
    }
    
    DateTime now = rtc.now();

#if defined(DEBUG)
//  if ((now.minute()  == 0) && (now.second() % 10 ) == 0){
    if ((now.second() % 10 ) == 0){
    Serial.print(now.day(), DEC);
    Serial.print('-');
    Serial.print(now.month(), DEC);
    Serial.print('-');
    Serial.print(now.year(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    print_mode();
  }
#endif
if ((now.hour() >= 8) && (now.hour() <= 22)){
    if (((now.minute()  == 0) || (now.minute()  == 30)) && bam == 0){
      bam = 1;
      if (now.hour() == 10 && now.minute() == 30){ bimbam(); delay(BAMDELAYDOUBLE); }
      if (now.hour() == 12 && now.minute() == 0 ){ bimbam(); delay(BAMDELAYDOUBLE); }
      if (now.hour() == 14 && now.minute() == 0 ){ bimbam(); delay(BAMDELAYDOUBLE); }
      if (now.hour() == 17 && now.minute() == 30){ bimbam(); delay(BAMDELAYDOUBLE); }
      if (now.hour() == 19 && now.minute() == 30){ bimbam(); delay(BAMDELAYDOUBLE); }
      if (now.hour() == 20 && now.minute() == 0 ){ bimbam(); delay(BAMDELAYDOUBLE); }
      if (now.hour() == 22 && now.minute() == 0 ){ bimbam(); delay(BAMDELAYDOUBLE); bimbam(); delay(BAMDELAYDOUBLE); }
      if (now.hour() != 22) {bimbam(); }
      
      Serial.print("Bim Bam: ");
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.println(now.minute(), DEC);
    }
    else if (((now.minute()  == 1) || (now.minute()  == 31))){
      bam = 0;
    }
}    
/*
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
*/

    delay(1000);
}