Orologio con WS2812


OROLOGIO CON WS2812

Per realizzare questo semplice progetto userò un Arduino Uno (va bene anche un Nano), un modulo RTC e 4 semicerchi da 90° di led WS2812.


// Scritto da Giuseppe Tamanini 17/04/2020 di pubblico dominio
#include <Wire.h>
#include "RTClib.h"
#include <WS2812.h>
 
RTC_DS1307 rtc;
#define outputPin 8  // Digital output pin command WS2812
#define LEDCount 60   // Number of LEDs WS2812
WS2812 LED(LEDCount);
cRGB value;
 
byte holdminute=99;
byte holdsecond=99;
 
int hh = 0;
int hho = 0;
int oldhh = - 99;
int mm=0;;
int ss=0;
boolean mh=true;
 
void setup() {
  Serial.begin(9600);
  LED.setOutput(outputPin);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
}
 
void loop() {
  // put your main code here, to run repeatedly:
 DateTime now = rtc.now();
  ss = now.second(); 
  if (holdsecond != ss) {
    mm = now.minute();
    hh = now.hour();
    hho = hh;
    if (hh > 11) {
      hho = hh - 12;
    }
    hho = hho * 5 + mm / 12;
    holdsecond = ss;
    AccendiLed();
  }
}
 
void AccendiLed() {
  for (int i = 0; i < 60; i++) {
    value.b = 5; value.g = 5; value.r =0; // RGB Value -> Blue
    LED.set_crgb_at(i, value); // Set value at LED found at index 0
  }
  value.b = 0; value.g = 127; value.r =255; // RGB Value -> Blue
  LED.set_crgb_at(mm, value); // Set value at LED found at index 0
  value.b = 0; value.g = 0; value.r =255; // RGB Value -> Blue
  LED.set_crgb_at(hho, value); // Set value at LED found at index 0
  if (mm == hho && mh == true) {
    value.b = 0; value.g = 255; value.r =0; // RGB Value -> Blue
    LED.set_crgb_at(mm, value); // Set value at LED found at index 0
    mh = false;
  }
  else if (mm == hho && mh == false) {
    mh = true;
  }
  value.b = 127; value.g = 127; value.r = 0; // RGB Value -> Blue
  LED.set_crgb_at(ss, value); // Set value at LED found at index 0
  if (ss == hho) {
    value.b = 127; value.g = 0; value.r =127; // RGB Value -> Blue
    LED.set_crgb_at(ss, value); // Set value at LED found at index 0
}
  if (ss == mm) {
    value.b = 0; value.g = 127; value.r =127; // RGB Value -> Blue
    LED.set_crgb_at(mm, value); // Set value at LED found at index 0
  }
  LED.sync(); // Sends the value to the LED
}


Articolo tratto da: Filodrammatica ViVa di Vigolo Vattaro - http://www.filoviva.it/
URL di riferimento: http://www.filoviva.it//index.php?mod=10_Progetti/34_orologio_con_ws