Hi everyone. I am currently learning how to use the Arduino Uno in school. I am working on a clock project that I will be using at home. My clock is a variation of the clock found here. http://tobiscorner.floery.net/rgb-pixel-clock-part-1/ As of right now the clock is fully functional. I am using an UNO, DS3231 RTC, and a string of WS2811 controlled LEDS. I want to have a small Light show at the top of every hour. The code for the LightCrawl I want works fine on its own. The clock code works fine on its own. I am having a very difficult time trying to place it with my clock code. I am hoping against hope that there is an easy way to get this to work. The class I am taking only taught us enough to explain what a microcontroller is and performed a few of the onboard Examples. If anyone can help me with my project, I would be most grateful. Thanks is advance.
And the answer as always, in the "read the instructions" at the top of the forum page is -
Use the "code" tags (the little scroll with brackets above the submission window).
Thanks for the info. Always butcher the first post on a forum. I was wondering where that was. The first code is the Pixel clock code.
/*
This is my sketch for a NEO PIXEL ring LED clock.
*/
// Includes below:
#include <Wire.h>
#include <stdio.h>
#include <Adafruit_NeoPixel.h>
#include <RTClib.h>
RTC_DS1307 RTC; // Create RTC object
// Define things here and set things up.
#define LED_Loop 60
#define PIN 6 // This is defining which Arduino pin is driving the Pixel ring
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_Loop, PIN, NEO_GRB + NEO_KHZ800);
int LED[LED_Loop];
int Q = (LED_Loop / 4);
int THREE = (Q-1);
int SIX = ((Q * 2)-1);
int NINE = ((Q * 3)-1);
int TWELVE = ((Q * 4)-1);
//int TWELVE = (LED_Loop-1);
int ONE = (LED_Loop/12); // Should be 5 if loop = 60
int HR_Fade = 7;
int MN_Fade;
long HR_Colour;
long SE_Colour = 0x0000FF;
long THIS_LED;
int Led_Flag;
int argh;
// trying this way to get colours working.
int HR_R;
int HR_G;
int HR_B;
int HR1_R = 0;
int HR1_G = 0x64;
int HR1_B = 0;
int HR2_R = 0;
int HR2_G = 0x64;
int HR2_B = 0;
int MN_R = 0;
int MN_G = 0x25;
int MN_B = 0;
//int SE_R = 0;
//int SE_G = 0x55;
//int SE_B = 0;
int hour_led;
int minute_led;
int second_led;
int new_minute;
//----------------------------- Set up here -----------------------------//
void setup()
{
// put your setup code here, to run once:
delay(2000); // This is just to give you time to open the window and watch.
Serial.begin(9600);
Serial.println("-------------------------------");
Serial.println("Setting up");
Wire.begin();
strip.begin();
strip.show(); // Initialize all pixels to 'off'
/*
Serial.print(THREE);
Serial.print(" ");
Serial.print(SIX);
Serial.print(" ");
Serial.print(NINE);
Serial.print(" ");
Serial.println(TWELVE);
*/
if (! RTC.isrunning())
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
Serial.println("Done setting up");
Serial.println("-------------------------------");
}
//----------------------------- MAIN LOOP here -----------------------------//
void loop()
{
// Get time
DateTime now = RTC.now();
// 24 hour time change colour of hour hand.
int hr = now.hour();
if (hr < 13)
{
//HR_Colour = HR_Colour1;
HR_R = HR1_R;
HR_G = HR1_G;
HR_B = HR1_B;
}
else
{
//HR_Colour = HR_Colour2;
HR_R = HR2_R;
HR_G = HR2_G;
HR_B = HR2_B;
}
int mins = now.minute(); // This line is only incase any maths had to be done.
second_led = now.second();
//
// calculate leds
hour_led = (((LED_Loop/12) * hr) + (mins / (LED_Loop/5)))%LED_Loop;
/*
if (hour_led == 60)
{
hour_led = 59;
}
*/
minute_led = mins;
// Debug code below
//
//------------------------------------------//
/*
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(' ');
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
Serial.println();
*/
//------------------------------------------//
/*
Serial.println("=========================");
Serial.println(hr);
Serial.println(mins);
Serial.println("-------------------------");
Serial.println(hour_led);
Serial.println(minute_led);
Serial.println(second_led);
*/
//------------------------------------------//
//
// Show LEDs ------------------ Main loop here -----------------
// Keep this at the top so it doesn't mess up any other settings when LEDs are
// turned on. Set the last part 0xXX to a number between 55 and 0. Zero returns the LED to off.
strip.setPixelColor(second_led-1,0x40);
strip.setPixelColor(second_led-2,0x35);
strip.setPixelColor(second_led-3,0x20);
strip.setPixelColor(second_led-4,0x05);
strip.setPixelColor(second_led-5,0);
//
// show THREE, SIX, NINE and TWELVE
//
strip.setPixelColor (THREE, 0x050505);
strip.setPixelColor (SIX, 0x050505);
strip.setPixelColor (NINE, 0x050505);
strip.setPixelColor (TWELVE, 0x050505);
//
// show the other numbers. (Optional)
//
strip.setPixelColor(4, 0x050505);
strip.setPixelColor(9, 0x050505);
strip.setPixelColor(19, 0x050505);
strip.setPixelColor(24, 0x050505);
strip.setPixelColor(34, 0x050505);
strip.setPixelColor(39, 0x050505);
strip.setPixelColor(49, 0x050505);
strip.setPixelColor(54, 0x050505);
//
// Now start setting LEDs below here.
//
if (second_led == 0)
{
strip.setPixelColor(LED_Loop-1, SE_Colour/2);
strip.setPixelColor(LED_Loop-2,SE_Colour/4);
strip.setPixelColor(LED_Loop-3,SE_Colour/8);
strip.setPixelColor(LED_Loop-4,SE_Colour/16);
strip.setPixelColor(LED_Loop-5,0);
new_minute = 1;
}
if (second_led == 1)
{
strip.setPixelColor(second_led-1, SE_Colour/2);
strip.setPixelColor(LED_Loop-1, SE_Colour/4);
strip.setPixelColor(LED_Loop-2,SE_Colour/8);
strip.setPixelColor(LED_Loop-3,SE_Colour/16);
strip.setPixelColor(LED_Loop-4,0);
}
if (second_led == 2)
{
strip.setPixelColor(second_led-1, SE_Colour/2);
strip.setPixelColor(second_led-2, SE_Colour/4);
strip.setPixelColor(LED_Loop-1, SE_Colour/8);
strip.setPixelColor(LED_Loop-2,SE_Colour/16);
strip.setPixelColor(LED_Loop-3,0);
}
if (second_led == 3)
{
strip.setPixelColor(second_led-1, SE_Colour/2);
strip.setPixelColor(second_led-2, SE_Colour/4);
strip.setPixelColor(second_led-3, SE_Colour/8);
strip.setPixelColor(LED_Loop-1,SE_Colour/16);
strip.setPixelColor(LED_Loop-2,0);
}
if (second_led == 4)
{
strip.setPixelColor(second_led-1, SE_Colour/2);
strip.setPixelColor(second_led-2, SE_Colour/4);
strip.setPixelColor(second_led-3, SE_Colour/8);
strip.setPixelColor(second_led-4,SE_Colour/16);
strip.setPixelColor(LED_Loop-1,0);
}
/*
if (Led_Flag == 0)
{
//
Led_Flag = 1;
THIS_LED = strip.getPixelColor(second_led);
// This is where I am at.
Serial.print(second_led);
Serial.print(" ");
Serial.println(THIS_LED);
}
*/
/*---------------- Draw SECOND HAND on clock ----------------*/
strip.setPixelColor(second_led,SE_Colour);
// strip.setPixelColor(second_led,SE_Colour+THIS_LED);
// strip.setPixelColor(second_led-1,THIS_LED);
if (new_minute == 1)
{
//new_minute = 0;
// strip.setPixelColor(minute_led-1,MN_Colour/50);
}
/*---------------- Draw MINUTE HAND on clock ----------------*/
//strip.setPixelColor(minute_led,MN_Colour);
// MN_Fade for fading.
strip.setPixelColor(minute_led,MN_R,MN_G,MN_B);
strip.setPixelColor(minute_led+1, MN_R, (MN_G * (second_led*10/6)/100) , MN_B);
strip.setPixelColor(minute_led-1, MN_R, (MN_G * (100-(second_led*10/6))/100) , MN_B);
/*---------------- Draw HOUR HAND on clock ----------------*/
strip.setPixelColor(hour_led,HR_R,HR_G,HR_B);
//strip.setPixelColor((hour_led-1)%LED_Loop,HR_R/HR_Fade,HR_G,HR_B/HR_Fade);
//strip.setPixelColor((hour_led+1)%LED_Loop,HR_R/HR_Fade,HR_G,HR_B/HR_Fade);
/*
strip.setPixelColor(hour_led,HR_Colour);
strip.setPixelColor((hour_led-1)%LED_Loop,HR_Colour);
strip.setPixelColor((hour_led+1)%LED_Loop,HR_Colour);
*/
if (second_led > minute_led)
{
new_minute = 0;
}
/*
if (second_led != argh)
{
Led_Flag = 0;
argh = second_led;
}
*/
//
// show alarms
//
strip.show();
delay(400);
// ------------------ End of Main loop here -----------------
}
//==================================================================
//==================================================================
//
// end of code
//
//==================================================================
LightCrawl code.
//This is the code for the light show that I want to happen at the
//top of the hour to mark the time. I want it to have a duration
//of 10 seconds before returning to the the normal LED clock
//program.
#include <Adafruit_NeoPixel.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Send a theater pixel chase in...
// theaterChase(strip.Color(127, 127, 127), 50); // White
theaterChase(strip.Color(127, 127, 0), 50); // Green
theaterChase(strip.Color( 0, 0, 127), 50); // Blue
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}}}}
// Input a value 0 to 255 to get a color value.
// The colours are a transition g - r - b - back to g.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
Well, it would seem that all you need to do is to insert the second code as an "if" option in the first loop code and merge the relevant parts of the "setup()".
If your concern is controlling the duration of the "LightCrawl" then there are two approaches available, one is that you determine the duration of a single iteration (loop time in current code) and run it in a "for" loop for a suitable number of repetitions to total the desired time.
The other is to re-work your code to a "state machine" in which certain actions function according to "switch" variables in a (rapidly) repeating chain of such actions and time-dependent actions occur according to determined "schedule" clock times. This would mean that to run something for ten seconds when a corresponding "switch" variable is turned on, you start by determining a clock time ten seconds in advance and execute the code repeatedly until the reading of the current time equals or exceeds that specified time, then turn the "switch" variable off so that this module is bypassed in the loop.
Thanks Paul__B. I went with the 'if' statement. The clock is working like a charm now.
.