Hey All,
I'm a noob, and I am now on the 3rd version of my project. It keeps getting better, so I guess I am getting the hang of it I am building an "analog" clock, with a pendulum...
I'm working with the Dream Pixels (self-contained RGB LED & LPD6803 controller, on a string), 72 pixels long.
thanks to a Moderator's coaxing, I've rebuilt some of my code to speed things up for the pendulum.
As I develop the sketch further, it's starting to bog down under some long 'if' statements.
I have a ton of these 'if' statements, and as I add more it gets slower - obviously - but much more than I expected.
What am I doing wrong?
Should all the stuff that gets done under true "if" conditions be turned into separate functions?
I did consider trying to construct an array, and then doing all the calcs into the array, but then I realized that I already am doing that, with the strip.SetPixel(x,x,x,x), AND i'd have not only the same calculations, but then I have to take more time just to transfer from my array into the array set up in the library.
I know the code is a bit messy, and possibly over-commented (is there such a thing?) but I'm still learning. I trimmed out some stuff from the code, just to keep it simper here. Mainly alarm-type colorful-swirly function calls during which time isn't displayed or calculated, and those functions are working properly anyway. So if you see a call to a function like "rainbowCycle()" or "colorUnwipe((r,g,b), x)" they exist, I just didn't post them.
(due to message limits I had to chop the code in half)
First, there are all the libraries and inits.
Then the main loop.
And with that, thanks in advance any and all feedback. This is a great community, indeed!
/*
This uses 71 LPD6803 pixels
arduino Mega
SPI in hardware mode - data = 51 clock = 52
added 4/12 -- rainbow qaurter routine with identical or rotating rainbow
cleaned up pendulum routine and added a colorWipe to clear face after rainbowcycle
4/18 -- built hourMarks routine, toggle, color by RGB settings, rainbow, and rainbow cycle
built colorFadein and colorFadeout routines
added to quarterMarks routine: toggle, color by RGB settings, rainbow, and rainbow cycle
added options to select hour,min,sec colors via RGB setting
added pendulum lag (zero'd for flashing on1st PedulumLED)
4/21 -- built updated rainbow routines:
rainbowCycface cycles face only, == around face
rainbowCycpend cycles pend only, == across pend
rainbowCycboth cycles face & pend == across both
*/ // end comments
//Pins 11 (hour), 12 (min) buttons
// Libraries
#include <TimerOne.h> // TimerOne for PWM for LPD6803 library
#include <Neophob_LPD6803.h> // LPD6803 library
#include <SPI.h> // SPI Library
#include <Wire.h> // Wire library
#include <Arduino.h> // Arduino library
#define LED_MODULES 71 // # of LED's
Neophob_LPD6803 strip = Neophob_LPD6803(LED_MODULES); // assign 'strip' to LPD6803 strip X LEDs long
// init pins for hour and minute buttons
const int hourButtonPin = 11; const int minButtonPin = 12;
// init variables
int hours = 4; // testing
int minutes = 8; // testing
int seconds = 25;
int hundreds;
// init buttons
int hourButtonState;
int minButtonState;
// init timing variables
long interval = 10;
long previousMillis = 0;
// init startup variable
int tested = 0;
// init variable for 15-min displays tracking
int fifteen = 0;
// init options
// ints timekeeping variables
int LastMinute;
int ThisMinute;
unsigned long lastSecond;
unsigned long thisSecond;
// ints sweep return for pendulum
int SweepReturn;
int pendulumLED;
unsigned long pendulumDuration;
// ints quarterCounter
int quarterCount;
int quarterCounter;
const int quarterStepper = 10;
const int quarterMarks = 1; //0 = off, 1 = hourR/G/B below, 2 = rainbow w/ qaurter
const int quarterRainbow = 0; // 0 = rainbows all quarters identical, 1 = wheels
const int quarterRed = 28;
const int quarterGreen = 0;
const int quarterBlue = 28;
// ints hourmarks
int hourCount;
int hourCounter;
const int hourStepper = 10;
const int hourMarks = 1; // 0 = off, 1 = hourR/G/B below, 2 = rainbow w/ qaurter
const int hourRainbow = 0; // 0 = rainbows all hours identical, 1 = wheels
const int hourBlinks = 1; // blinks hourMarks @ blink rate
const int hourRed = 1;
const int hourGreen = 0;
const int hourBlue = 1;
// ints hour, min, sec colors
int hrColor [ ] = {31, 0, 0};
int minColor [ ] = { 0, 0,28};
int secColor [ ] = { 0,28, 0};
// int hrRed, hrBlue, hrGreen, minRed, minBlue, minGreen, secBlue,secRed, secGreen;
// ints array to track of the pendulum LED to light up (L-->R-->L)
int pendulumLight [ ] = {60,61,62,63,64,65,66,67,68,69,70,69,68,67,66,65,64,63,62,61,60};
// ints array for delay between pendulum lights (L-->R-->L)
// array contains time from LED to next LED, swing in both directions, defined in milliseconds:
// int pendulumTime [ ] = {120, 95, 81, 68, 55, 47 , 55, 68, 81, 95,120, 95, 81, 68, 55, 47, 55, 68, 81, 95, 120};
int pendulumTime [ ] = {140,110, 80, 60, 45, 35, 45, 60, 80,110,180,110, 80, 60, 45, 35, 45, 60, 80,110,120};
void setup()
{
// configs strip and clears all LEDS
strip.setCPUmax(90);
strip.begin();
strip.show();
// start Wire
Wire.begin();
// clear /EOSC bit
// Sometimes necessary to ensure that the clock
// keeps running on just battery power. Once set,
// it shouldn't need to be reset but it's a good
// idea to make sure.
Wire.beginTransmission(0x68); // address DS3231
Wire.write(0x0E); // select register
Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
Wire.endTransmission();
// sets hr and min button pins to inputs
pinMode(hourButtonPin,INPUT);
hourButtonState = 0;
pinMode(minButtonPin,INPUT);
minButtonState = 0;
//
// startup display -- for testing various effects as built --
colorWipe(Color( 0, 0, 0),0);
//strip.show();
/* rainbowCycle(5);
rainbowCycle(5);
colorWipe(Color( 26, 0, 0),20);
delay(100);
colorUnwipe(Color( 6, 6, 0),0);
delay(100);
colorWipe(Color( 0, 26, 0),20);
delay(100);
colorUnwipe(Color( 0, 6, 6),0);
delay(100);
colorWipe(Color( 0, 0, 26), 20);
delay(100);
colorUnwipe(Color( 0, 0, 0), 10);
colorFadein(Color( 0, 0, 0), 60);
colorFadeout(Color(31,31,31),60);
colorFadein(Color( 0, 0, 0), 60);
colorFadeout(Color(31,31,31),60);
colorFadein(Color( 0, 0, 0), 60);
colorFadeout(Color(31,31,31),60);
// colorSpin(Color(31, 0, 0),10);
// colorSpin(Color(31,31, 0),10);
// colorSpin(Color( 0,31, 0),10);
// colorSpin(Color( 0,31,31),10);
// colorSpin(Color( 0, 0,31),10);
// colorWipe(Color( 31,31,31),20);
// colorWipe(Color(0,0,0),20);
// delay(1000);
//rainbowCycle(10);
*/
colorWipe((0,0,0),0);
delay(250);
rainbowCycface(25);
delay(250);
rainbowCycpend(25);
delay(250);
rainbowCycboth(25);
}