Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!





RGB LED Ring Clock ...
 
Share:
Notifications
Clear all

RGB LED Ring Clock sketch: solving one problem produces another  

  RSS

(@ppgflyer68)
Active Member
Joined: 7 months ago
Posts: 5
May 3, 2020 1:17 AM  

first i would like to thank Hans for his contribution and help with the problem i was having with my sketch.   i originally started this conversation in the  "Arduino – LEDStrip effects for NeoPixel and FastLED"  thread and then in the "arduino/in-need-of-assistance-with-a-sketch-addressable-led" thread which is where i have posted the code.

the sketch i am working with is a super slimmed down version of the "Rise and shine infinity clock" by Barkengmad.  i have managed to eliminate 85% of the original code so that what remains is just the clock portion.  i have been able to get it to work and have made several clocks using this code, so i know it works well for my needs.  i recently purchased the Lumini 3" 60 RGB LED light ring to use in my next rendition, however, i come to discover that the ring is manufactured in a anti-clockwise direction unlike all the other rings that i use (they are 6 3/4"). so now the clock reads backwards... ie, where the 3 o'clock position is, is where the 9 o'clock position is and vise versa.  midnight/noon and 6 o'clock are in thier proper positions.

Thankfully, Hans was able to help to reverse the direction that the LEDs illuminate so that the clock shows correctly, however, with the change that he suggested, this has produced a new problem.  when the second indicator reaches the ring's 0 LED, the mode resets to 0 (there are 4 modes, outline clock and minimal clock, both having a standard time and daylight savings time).

if anyone has any idea as to why this is happening and my have any suggestions, it would be much appreciated for your input.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1620
May 4, 2020 4:31 AM  

HiPpgFluer68.

Could you post the full code (the code you made/use)?
This way we can look and see what may be going on.  🙂 

For reference for others: some of this we discussed here
However it seemed better to start a new topic. 


ReplyQuote
(@ppgflyer68)
Active Member
Joined: 7 months ago
Posts: 5
May 12, 2020 12:38 AM  

after extensive serches on the internet, i ran across an article discussing the use of the MAP function and applied it to the code.  now the clock works like a charm.  he is what i did.

#include <FastLED.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>
#include <Encoder.h>

DS3231 RTC;

#define DATA_PIN 6 //for 2 wire LED strips
#define CLOCK_PIN 5
//#define LEDStripPin 8 //for single wire LED strips
#define numLEDs 60 //number of LEDs in the strip

//Setting up the LED strip
struct CRGB leds[numLEDs];
Encoder rotary1(10, 11); //setting up the Rotary Encoder
DateTime old; //Variable to compare new and old time, to see if it has moved on
int rotary1Pos = 0;
int subSeconds; //60th's of a second
long newSecTime; //Variable to record when a new second starts, allowing to create milli seconds
long oldSecTime;
#define clockState 0
int cyclesPerSec;
float cyclesPerSecFloat; // So can be used as a float in calcs
float fracOfSec;
int timeHour;
int timeMin;
int timeSec;
int modeAddress = 0; // Address of where mode is stored in the EEPROM
long currentMillis;
long previousMillis = 0;
int state = 0;
int mode; // Variable of the display mode of the clock
int modeMax = 3; // Change this when new modes are added. This is so selecting modes can go back beyond.
int advanceMove = 0;
long lastRotary;
int rotaryTime = 1000;
int LEDPosition;
int fiveMins;
int LEDOffset = 30;

void setup()
{
// Set up all pins
// pinMode(menuPin, INPUT_PULLUP); // Uses the internal 20k pull up resistor. Pre Arduino_v.1.0.1 need to be "digitalWrite(menuPin,HIGH);pinMode(menuPin,INPUT);"

// Start LEDs
// LEDS.addLeds<WS2811, LEDStripPin, GRB>(leds, numLEDs); // Structure of the LED data. I have changed to from rgb to grb, as using an alternative LED strip. Test & change these if you're getting different colours.
LEDS.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(leds, numLEDs);

// Start RTC
Wire.begin(); // Starts the Wire library allows I2C communication to the Real Time Clock
RTC.begin(); // Starts communications to the RTC

Serial.begin(9600); // Starts the serial communications

// Uncomment to reset all the EEPROM addresses. You will have to comment again and reload, otherwise it will not save anything each time power is cycled
// write a 0 to all 512 bytes of the EEPROM
// for (int i = 0; i < 512; i++)
// {EEPROM.write(i, 0);}

// Load any saved setting since power off, such as mode & alarm time
mode = EEPROM.read(modeAddress); // The mode will be stored in the address "0" of the EEPROM

}

void loop()

{
DateTime now = RTC.now(); // Fetches the time from RTC

rotary1Pos = rotary1.read(); // Checks the rotary position

if (rotary1Pos <= -4 && lastRotary - millis() >= rotaryTime)
{
advanceMove = -1;
rotary1.write(0);
lastRotary = millis();
}
if (rotary1Pos >= 4 && lastRotary - millis() >= rotaryTime)
{
advanceMove = 1;
rotary1.write(0);
lastRotary = millis();
}
{
switch (mode)

case clockState: // State 0

if (advanceMove == -1 && mode == 0)
{
mode = modeMax;
advanceMove = 0;
}
else (advanceMove != 0);
{
mode = mode + advanceMove;
EEPROM.write(modeAddress, mode);
advanceMove = 0;
}
}

{
timeDisplay(now);
}
// Update LEDs;
LEDS.show();
memset(leds, 0, numLEDs * 3);
}

void clearLEDS()
{
for (int i = 0; i < numLEDs; i++) // set all LEDs to off
{
leds[i].r = 0;
leds[i].g = 0;
leds[i].b = 0;
}
}

void timeDisplay(DateTime now)
{
switch (mode)
{
case 0:
outlineClock(now);
break;
case 1:
outlineDSTClock(now);
break;
case 2:
minimalClock(now);
break;
case 3:
minimalDSTClock(now);
break;

default: // Keep this here and add more timeDisplay modes as defined cases.
{
mode = 0;
}
}
}

void outlineClock(DateTime now)

{
// for (int i = 59; i >= 0 ; i--)
for (int i = 0; i < numLEDs; i++)
{
fiveMins = i % 5;
if (fiveMins == 0)
{
leds[i].r = 2;
leds[i].g = 2;
leds[i].b = 2;
}
}
if (now.second() != old.second())
{
old = now;

}

// set hour, min & sec LEDs
/*
//uncomment this section to use with rings that are constructed clockwise
unsigned char hourPos = ((now.hour() % 12) * 5 + (now.minute() + 6) / 12);
leds[(hourPos + LEDOffset + 59) % 60].r = 5;
leds[(hourPos + LEDOffset) % 60].r = 20;
leds[(hourPos + LEDOffset + 1) % 60].r = 5;
leds[(now.minute() + LEDOffset) % 60].g = 20;
leds[(now.second() + LEDOffset) % 60].b = 30;
*/
// uncomment this section to use rings that are constructed counter clockwise (Lumini 3" APA102 2020)
unsigned char hourPos = ((now.hour() % 12) * 5 + (now.minute() + 6) / 12);
unsigned char minPos = (now.minute());
unsigned char secPos = (now.second());

int valh = hourPos;
int valm = minPos;
int vals = secPos;
int hours = map(valh, 0, 59, 59, 0);
int mins = map(valm, 0, 59, 59, 0);
int secs = map(vals, 0, 59, 59, 0);
leds[(hours + LEDOffset + 7) % 60].r = 2;
leds[(hours + LEDOffset + 6) % 60].r = 20;
leds[(hours + LEDOffset + 5) % 60].r = 2;
leds[(mins + LEDOffset + 1) % 60].g = 20;
leds[(secs + LEDOffset + 1) % 60].b = 30;


}

void outlineDSTClock(DateTime now)
{
//for (int i = numLEDs - 1; i > -1; i--)
for (int i = 0; i < numLEDs; i++)
{
fiveMins = i % 5;
if (fiveMins == 0)
{
leds[i].r = 2;
leds[i].g = 2;
leds[i].b = 2;
}
}
if (now.second() != old.second())
{
old = now;

}

// set hour, min & sec LEDs
/*
//uncomment this section to use with rings that are constructed clockwise
unsigned char hourPos = ((now.hour() % 12) * 5 + (now.minute() + 6) / 12);
leds[(hourPos + LEDOffset + 54) % 60].r = 10;
leds[(hourPos + LEDOffset + 55) % 60].r = 20;
leds[(hourPos + LEDOffset + 56) % 60].r = 10;
leds[(now.minute() + LEDOffset) % 60].g = 20;
leds[(now.second() + LEDOffset) % 60].b = 20;
*/
// uncomment this section to use rings that are constructed counter clockwise (Lumini 3" APA102 2020)
unsigned char hourPos = ((now.hour() % 12) * 5 + (now.minute() + 6) / 12);
unsigned char minPos = (now.minute());
unsigned char secPos = (now.second());

int valh = hourPos;
int valm = minPos;
int vals = secPos;
int hours = map(valh, 0, 59, 59, 0);
int mins = map(valm, 0, 59, 59, 0);
int secs = map(vals, 0, 59, 59, 0);
leds[(hours + LEDOffset + 2) % 60].r = 2;
leds[(hours + LEDOffset + 1) % 60].r = 20;
leds[(hours + LEDOffset) % 60].r = 2;
leds[(mins + LEDOffset + 1) % 60].g = 20;
leds[(secs + LEDOffset + 1) % 60].b = 30;


}
void minimalClock(DateTime now)
{
/*uncomment this section to use with rings that are constructed clockwise
unsigned char hourPos = (now.hour() % 12) * 5;
leds[(hourPos + LEDOffset) % 60].r = 10;
leds[(now.minute() + LEDOffset) % 60].g = 10;
leds[(now.second() + LEDOffset) % 60].b = 10;
*/
// uncomment this section to use rings that are constructed counter clockwise (Lumini 3" APA102 2020)
unsigned char hourPos = ((now.hour() % 12) * 5 + (now.minute() + 6) / 12);
unsigned char minPos = (now.minute());
unsigned char secPos = (now.second());

int valh = hourPos;
int valm = minPos;
int vals = secPos;
int hours = map(valh, 0, 59, 59, 0);
int mins = map(valm, 0, 59, 59, 0);
int secs = map(vals, 0, 59, 59, 0);
leds[(hours + LEDOffset +6) % 60].r = 10;
leds[(mins + LEDOffset + 1) % 60].g = 10;
leds[(secs + LEDOffset + 1) % 60].b = 10;

}
//
void minimalDSTClock(DateTime now)
{
/*uncomment this section to use with rings that are constructed clockwise
unsigned char hourPos = (now.hour() % 12) * 5 + (now.minute() + 6) / 12);
leds[(hourPos + LEDOffset + 55) % 60].r = 10;
leds[(now.minute() + LEDOffset) % 60].g = 10;
leds[(now.second() + LEDOffset) % 60].b = 10;
*/
// uncomment this section to use rings that are constructed counter clockwise (Lumini 3" APA102 2020)
unsigned char hourPos = ((now.hour() % 12) * 5 + (now.minute() + 6) / 12);
unsigned char minPos = (now.minute());
unsigned char secPos = (now.second());

int valh = hourPos;
int valm = minPos;
int vals = secPos;
int hours = map(valh, 0, 59, 59, 0);
int mins = map(valm, 0, 59, 59, 0);
int secs = map(vals, 0, 59, 59, 0);
leds[(hours + LEDOffset + 1) % 60].r = 10;
leds[(mins + LEDOffset + 1) % 60].g = 10;
leds[(secs + LEDOffset + 1) % 60].b = 10;


}

ReplyQuote
(@ppgflyer68)
Active Member
Joined: 7 months ago
Posts: 5
May 12, 2020 1:32 AM  

i guess i should highlight the addition/fix to my code which is in the bottom sections of the code (the different MODES)

 

// set hour, min & sec LEDs
/*
//uncomment this section to use with rings that are constructed clockwise
unsigned char hourPos = ((now.hour() % 12) * 5 + (now.minute() + 6) / 12);
leds[(hourPos + LEDOffset + 59) % 60].r = 5;
leds[(hourPos + LEDOffset) % 60].r = 20;
leds[(hourPos + LEDOffset + 1) % 60].r = 5;
leds[(now.minute() + LEDOffset) % 60].g = 20;
leds[(now.second() + LEDOffset) % 60].b = 30;
*/
// uncomment this section to use rings that are constructed counter clockwise (Lumini 3" APA102 2020)
unsigned char hourPos = ((now.hour() % 12) * 5 + (now.minute() + 6) / 12);
unsigned char minPos = (now.minute());
unsigned char secPos = (now.second());

int valh = hourPos;
int valm = minPos;
int vals = secPos;
int hours = map(valh, 0, 59, 59, 0);
int mins = map(valm, 0, 59, 59, 0);
int secs = map(vals, 0, 59, 59, 0);
leds[(hours + LEDOffset + 7) % 60].r = 2;
leds[(hours + LEDOffset + 6) % 60].r = 20;
leds[(hours + LEDOffset + 5) % 60].r = 2;
leds[(mins + LEDOffset + 1) % 60].g = 20;
leds[(secs + LEDOffset + 1) % 60].b = 30;


}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1620
May 18, 2020 4:26 AM  

Awesome! Glad to hear it now works as intended and thanks for posting the working code!
I'm sure other users would love to give it a try! 👍 


ReplyQuote



Share: