62 lines
1.3 KiB
C++
62 lines
1.3 KiB
C++
//code for a linear clock
|
|
|
|
//todo electrical scheme
|
|
|
|
#include <FastLED.h>
|
|
|
|
#define NUM_LEDS 12
|
|
#define DATA_PIN 4 // Change this to match your LED strip's data pin
|
|
#define BUTTON_PIN 2 // Change this to match your LED strip's data pin
|
|
#define BRIGHTNESS 25
|
|
|
|
CRGB leds[NUM_LEDS];
|
|
uint8_t pos = 0;
|
|
bool toggle = false;
|
|
int hour=17;
|
|
int minute=54;
|
|
int second=0;
|
|
void setup() {
|
|
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
|
|
FastLED.setBrightness(BRIGHTNESS);
|
|
|
|
Serial.begin(115200);
|
|
pinMode(BUTTON_PIN, INPUT);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
int buttonState = digitalRead(BUTTON_PIN);
|
|
uint8_t ledhour = (hour * NUM_LEDS) / 24;
|
|
uint8_t ledmin =(minute * NUM_LEDS) / 60;
|
|
uint8_t ledsec =(second * NUM_LEDS) / 60;
|
|
for(uint8_t dot=0 ; dot< NUM_LEDS ; dot++) {
|
|
if(buttonState == LOW ) {
|
|
|
|
leds[dot].setRGB( 255*(ledhour==dot) ,
|
|
255*(ledsec==dot)+ 64 * ( (ledsec-1) ==dot ) + 16 *((ledsec-2)==dot),
|
|
255*(ledmin==dot));
|
|
|
|
} else {
|
|
leds[dot].setRGB(0,0,0);
|
|
}
|
|
}
|
|
|
|
|
|
FastLED.show();
|
|
|
|
//compute next time
|
|
second++;
|
|
if( second == 60 ) {
|
|
second=0;
|
|
minute++;
|
|
}
|
|
if( minute ==60 ) {
|
|
minute=0;
|
|
hour = (hour++ ) % 24;
|
|
}
|
|
delay(1000);
|
|
|
|
|
|
}
|