infinityledclock/vscode/clock/src/clock.cpp

113 lines
2.4 KiB
C++

#include <Adafruit_NeoPixel.h>
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
void update_clock();
void showseconds();
RtcDS3231<TwoWire> Rtc(Wire);
#define PIN 6 // Which pin on the Arduino is connected to the NeoPixels?
const unsigned int NUMPIXELS = 83; // How many NeoPixels are attached to the Arduino?
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
unsigned long color_on, color_off, color_sec, color_min, color_hour;
const float seconds2pixel = NUMPIXELS /60.0;
void setup() {
Serial.begin(9600);
Rtc.Begin();
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
color_on = pixels.Color(0, 0, 255);
color_off = pixels.Color(0, 0, 0);
color_sec = pixels.Color(0, 255, 0);
color_min = pixels.Color(0, 255, 255);
color_hour = pixels.Color(255, 255, 0);
pixels.fill(color_off);
pixels.show();
}
int last_second = 0;
int second = 0;
int minute = 0;
int hour = 0;
void update_clock()
{
if(last_second != second)
{
Serial.print("Now is -> ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
last_second = second;
// first delete all ...
pixels.fill(color_off);
// second show seconds (^_^);
showseconds();
// third show minutes
// fourth show hour
pixels.show(); // Send the updated pixel colors to the hardware.
}
}
void showseconds()
{
int secondpixelpos1 = 0;
int secondpixelpos2 = 0;
int secondpixelpos3 = 0;
float centerposf = seconds2pixel * second;
secondpixelpos2 = round(centerposf);
if(secondpixelpos2 == NUMPIXELS)
{
secondpixelpos1 = secondpixelpos2 - 1;
secondpixelpos3 = 0;
}
else if(secondpixelpos2 == 0)
{
secondpixelpos1 = NUMPIXELS;
secondpixelpos3 = secondpixelpos2 + 1;
}
else
{
secondpixelpos1 = secondpixelpos2 - 1;
secondpixelpos3 = secondpixelpos2 + 1;
}
Serial.println(secondpixelpos1);
Serial.println(secondpixelpos2);
Serial.println(secondpixelpos3);
pixels.setPixelColor(secondpixelpos1, color_sec);
pixels.setPixelColor(secondpixelpos2, color_sec);
pixels.setPixelColor(secondpixelpos3, color_sec);
}
RtcDateTime now;
void loop() {
now = Rtc.GetDateTime();
hour = now.Hour();
minute = now.Minute();
second = now.Second();
//RtcTemperature temp = Rtc.GetTemperature();
update_clock();
}