summaryrefslogtreecommitdiff
path: root/UhrLed.ino
blob: fdb12a634ffca7a810d23b881f116164f8b47692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <Wire.h>
#include <RTCx.h>

void printTm(Stream &str, struct RTCx::tm *tm)
{
  str.print(tm->tm_year + 1900);
  str.print('-');
  str.print(tm->tm_mon + 1);
  str.print('-');
  str.print(tm->tm_mday);
  str.print('T');
  str.print(tm->tm_hour);
  str.print(':');
  str.print(tm->tm_min);
  str.print(':');
  str.print(tm->tm_sec);
  str.print(" yday=");
  str.print(tm->tm_yday);
  str.print(" wday=");
  str.println(tm->tm_wday);
}

void setup(void)
{
  Serial.begin(9600);
  Wire.begin();
  // The address used by the DS1307 is also used by other devices (eg
  // MCP3424 ADC). Test for a MCP7941x device first.
  uint8_t addressList[] = {RTCx::MCP7941xAddress,
         RTCx::DS1307Address};

  // Autoprobe to find a real-time clock.
  if (rtc.autoprobe(addressList, sizeof(addressList))) {
    // Found something, hopefully a clock.
    Serial.print("Autoprobe found ");
    switch (rtc.getDevice()) {
      case RTCx::DS1307:
  Serial.print("DS1307");
  break;
    case RTCx::MCP7941x:
      Serial.print("MCP7941x");
      break;
    default:
      // Ooops. Must update this example!
      Serial.print("unknown device");
      break;
    }
    Serial.print(" at 0x");
    Serial.println(rtc.getAddress(), HEX);
  }
  else {
    // Nothing found at any of the addresses listed.
    Serial.println("No RTCx found");
    return;
  }

  // Enable the battery backup. This happens by default on the DS1307
  // but needs to be enabled on the MCP7941x.
  rtc.enableBatteryBackup();

  // rtc.clearVBAT();
  
  // Ensure the oscillator is running.
  rtc.startClock();

  if (rtc.getDevice() == RTCx::MCP7941x) {
    Serial.print("Calibration: ");
    Serial.println(rtc.getCalibration(), DEC);
    // rtc.setCalibration(-127);
  }

  rtc.setSQW(RTCx::freq4096Hz);
}

int ledBlue = 9;
int ledGreen = 3;
int ledRed = 11;
unsigned long last = 0;
void loop() {
  // put your main code here, to run repeatedly:
  struct RTCx::tm tm;
  struct RTCx::tm *ptr;
  rtc.readClock(tm);
  ptr= &tm;
  analogWrite(ledBlue,(ptr->tm_sec*4)+15);
  Serial.print("Blue LED ");
  Serial.println((ptr->tm_sec*4));
  analogWrite(ledGreen,(ptr->tm_sec*2));
  analogWrite(ledRed,(ptr->tm_sec*2));
  delay(1000);
    
    printTm(Serial, ptr);
    RTCx::time_t t = RTCx::mktime(&tm);
    printTm(Serial, &tm);
    Serial.print("unixtime = ");
    Serial.println(t);
    Serial.println("-----");
  
}