Sketch slowing down... General noob proggie questions
hey all,
i'm noob, , on 3rd version of project. keeps getting better, guess getting hang of it ;-) building "analog" clock, pendulum...
i'm working dream pixels (self-contained rgb led & lpd6803 controller, on string), 72 pixels long.
thanks moderator's coaxing, i've rebuilt of code speed things pendulum.
as develop sketch further, it's starting bog down under long 'if' statements.
i have ton of these 'if' statements, , add more gets slower - - more expected.
what doing wrong?
should stuff gets done under true "if" conditions turned separate functions?
i did consider trying construct array, , doing calcs array, realized doing that, strip.setpixel(x,x,x,x), , i'd have not same calculations, have take more time transfer array array set in library.
i know code bit messy, , possibly over-commented (is there such thing?) i'm still learning. trimmed out stuff code, keep simper here. alarm-type colorful-swirly function calls during time isn't displayed or calculated, , functions working anyway. if see call function "rainbowcycle()" or "colorunwipe((r,g,b), x)" exist, didn't post them.
(due message limits had chop code in half)
first, there libraries , inits.
then main loop.
and that, in advance , feedback. great community, indeed!
i'm noob, , on 3rd version of project. keeps getting better, guess getting hang of it ;-) building "analog" clock, pendulum...
i'm working dream pixels (self-contained rgb led & lpd6803 controller, on string), 72 pixels long.
thanks moderator's coaxing, i've rebuilt of code speed things pendulum.
as develop sketch further, it's starting bog down under long 'if' statements.
i have ton of these 'if' statements, , add more gets slower - - more expected.
what doing wrong?
should stuff gets done under true "if" conditions turned separate functions?
i did consider trying construct array, , doing calcs array, realized doing that, strip.setpixel(x,x,x,x), , i'd have not same calculations, have take more time transfer array array set in library.
i know code bit messy, , possibly over-commented (is there such thing?) i'm still learning. trimmed out stuff code, keep simper here. alarm-type colorful-swirly function calls during time isn't displayed or calculated, , functions working anyway. if see call function "rainbowcycle()" or "colorunwipe((r,g,b), x)" exist, didn't post them.
(due message limits had chop code in half)
first, there libraries , inits.
then main loop.
and that, in advance , feedback. great community, indeed!
code: [select]
/*
this uses 71 lpd6803 pixels
arduino mega
spi in hardware mode - data = 51 clock = 52
added 4/12 -- rainbow qaurter routine identical or rotating rainbow
cleaned pendulum routine , added colorwipe clear face after rainbowcycle
4/18 -- built hourmarks routine, toggle, color rgb settings, rainbow, , rainbow cycle
built colorfadein , colorfadeout routines
added quartermarks routine: toggle, color rgb settings, rainbow, , rainbow cycle
added options select hour,min,sec colors via rgb setting
added pendulum lag (zero'd 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 pwm 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' lpd6803 strip x leds long
// init pins hour , 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 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 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 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 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 track of pendulum led light (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 delay between pendulum lights (l-->r-->l)
// array contains time led 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 , clears leds
strip.setcpumax(90);
strip.begin();
strip.show();
// start wire
wire.begin();
// clear /eosc bit
// necessary ensure clock
// keeps running on battery power. once set,
// shouldn't need reset it's good
// idea make sure.
wire.begintransmission(0x68); // address ds3231
wire.write(0x0e); // select register
wire.write(0b00011100); // write register bitmap, bit 7 /eosc
wire.endtransmission();
// sets hr , min button pins inputs
pinmode(hourbuttonpin,input);
hourbuttonstate = 0;
pinmode(minbuttonpin,input);
minbuttonstate = 0;
//
// startup display -- testing various effects 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);
}
code: [select]
the main loop
[code]
void loop()
{
// high speed testing routine
// delay(0);
if (millis() >= lastsecond)
{
lastsecond = lastsecond + 1000;
strip.setpixelcolor(seconds, 0, 0, 0);
strip.setpixelcolor(minutes, 0, 0, 0);
seconds++;
// strip.show();
}
if (seconds > 59) {minutes++; seconds = 0;}
if (minutes > 59) {hours++; minutes = 0; seconds = 0;}
/*
// request time
// send request receive data starting @ register 0
wire.begintransmission(0x68); // 0x68 ds3231 device address
wire.write(byte(0)); // start @ register 0
wire.endtransmission();
wire.requestfrom(0x68, 3); // request 3 bytes (seconds, minutes, hours)
while(wire.available())
{
seconds = wire.read(); // seconds
minutes = wire.read(); // minutes
hours = wire.read(); // hours
// convert bcd decimal
seconds = (((seconds & 0b11110000)>>4)*10 + (seconds & 0b00001111)); // convert bcd decimal
minutes = (((minutes & 0b11110000)>>4)*10 + (minutes & 0b00001111)); // convert bcd decimal
hours = (((hours & 0b00110000)>>4)*10 + (hours & 0b00001111)); // convert bcd decimal (assume 24 hour mode)
*/
// show 12,3,6,9 marks
if (quartermarks == 2)
{
quartercounter = quartercounter +1;
if ( quartercounter > quarterstepper )
{ quartercounter = 0;
quartercount++; }
if ( quartercount > 96)
{quartercount = 0;}
strip.setpixelcolor( 0, wheel( (( ( 0 * quarterrainbow) * 96 / strip.numpixels()) + quartercount) % 96) );
strip.setpixelcolor(15, wheel( (( (15 * quarterrainbow) * 96 / strip.numpixels()) + quartercount) % 96) );
strip.setpixelcolor(30, wheel( (( (30 * quarterrainbow) * 96 / strip.numpixels()) + quartercount) % 96) );
strip.setpixelcolor(45, wheel( (( (45 * quarterrainbow) * 96 / strip.numpixels()) + quartercount) % 96) );
}
if (quartermarks == 1)
{
strip.setpixelcolor( 0, (quarterblue && seconds!= 0), (quarterred && seconds!= 0), (quartergreen && seconds!= 0) );
strip.setpixelcolor(15, (quarterblue && seconds!=15), (quarterred && seconds!=15), (quartergreen && seconds!=15) );
strip.setpixelcolor(30, (quarterblue && seconds!=30), (quarterred && seconds!=30), (quartergreen && seconds!=30) );
strip.setpixelcolor(45, (quarterblue && seconds!=45), (quarterred && seconds!=45), (quartergreen && seconds!=45) );
}
// show hourmarks
if (hourmarks == 2 )
{
hourcounter = hourcounter +1;
if ( hourcounter > hourstepper )
{
hourcounter = 0;
hourcount++;
}
if ( hourcount > 96)
{
hourcount = 0;
}
strip.setpixelcolor( 5, wheel( (( (( 5 * hourrainbow) * 96 / strip.numpixels()) + hourcount) % 96)) );
strip.setpixelcolor(10, wheel( (( ((10 * hourrainbow) * 96 / strip.numpixels()) + hourcount) % 96)) );
strip.setpixelcolor(20, wheel( (( ((20 * hourrainbow) * 96 / strip.numpixels()) + hourcount) % 96)) );
strip.setpixelcolor(25, wheel( (( ((25 * hourrainbow) * 96 / strip.numpixels()) + hourcount) % 96)) );
strip.setpixelcolor(35, wheel( (( ((35 * hourrainbow) * 96 / strip.numpixels()) + hourcount) % 96)) );
strip.setpixelcolor(40, wheel( (( ((40 * hourrainbow) * 96 / strip.numpixels()) + hourcount) % 96)) );
strip.setpixelcolor(50, wheel( (( ((50 * hourrainbow) * 96 / strip.numpixels()) + hourcount) % 96)) );
strip.setpixelcolor(55, wheel( (( ((55 * hourrainbow) * 96 / strip.numpixels()) + hourcount) % 96)) );
}
if (hourmarks == 1)
{
strip.setpixelcolor( 5, hourblue, hourred, hourgreen );
strip.setpixelcolor(10, hourblue, hourred, hourgreen );
strip.setpixelcolor(20, hourblue, hourred, hourgreen );
strip.setpixelcolor(25, hourblue, hourred, hourgreen );
strip.setpixelcolor(35, hourblue, hourred, hourgreen );
strip.setpixelcolor(40, hourblue, hourred, hourgreen );
strip.setpixelcolor(50, hourblue, hourred, hourgreen );
strip.setpixelcolor(55, hourblue, hourred, hourgreen );
}
// calc , rainbowcycle :15 alarms includes resetting toggle 1 minute later
// runs alarm animation 4x on hour
if ( ( minutes == 0 ) && ( fifteen == 0 ) )
{
rainbowcycle(30); // rainbowcycle(30); rainbowcycle(30); rainbowcycle(30);
fifteen++;
}
// runs alarm animation 2x on half hour
if ( ( minutes == 30 ) && ( fifteen == 0 ) )
{
rainbowcycle(30); rainbowcycle(30);
fifteen++;
}
// runs alarm animation once on :15 & :45
if ( ( minutes == 15 || minutes == 45 ) && ( fifteen == 0 ) )
{
rainbowcycle(30);
fifteen++;
}
//resets fifteen on :01, :16, :31, :46
if ( ( minutes == 1 ) || ( minutes == 16 ) || ( minutes == 31 ) || ( minutes == 46 ) )
{
fifteen = 0;
}
// hours, mins, seconds calc + pixel-set
// converts 12hr time 12 hr time
if ( hours > 11 )
{ hours = hours - 12 ; }
// calc + pixel-set center hour mark if not on 12,3,6,9
if( (hours != 0) && ( hours != 3 ) && ( hours != 6 ) && ( hours != 9 ) )
{ strip.setpixelcolor( hours*5, 28, 0, 0); }
// calc & pixel-set hour mark -1, except @ 12:00.
if (hours > 0)
{ strip.setpixelcolor((hours*5)-1, 8, 0, 0); }
// sets hour-1 pixel when @ 12:00 led59
else
{strip.setpixelcolor( 59, 8, 0, 0); }
// sets hour+1 pixel always
strip.setpixelcolor( (hours*5)+1, 8, 0, 0);
// sets minutes
strip.setpixelcolor( minutes, 0, 0,20);
// sets seconds
strip.setpixelcolor( seconds, 0,20, 0);
//
//pendulum routine
if (millis() >= pendulumduration)
{
if(pendulumled > 1) // turn off prior led
{
strip.setpixelcolor(pendulumlight[pendulumled-2], 0, 0, 0);
}
if(pendulumled >0)
{
strip.setpixelcolor(pendulumlight[pendulumled-1], 3, 2, 1);
}
strip.setpixelcolor(pendulumlight[pendulumled] , 6, 5, 4);
// set next led
pendulumled = pendulumled+1;
// if @ start, reset counter
if (pendulumled == 22){ pendulumled = 0; } // leds, & forth: 0-1-2-3-4-5-6-7-8-9-10-9-8-7-6-5-4-3-2-1
// turn on next led
strip.setpixelcolor(pendulumlight[pendulumled], 31, 30, 25);
//strip.setpixelcolor(pendulumlight[pendulumled], wheel( ( pendulumled * 96 / strip.numpixels()) + pendulumled) % 96);
// set time watch next change
pendulumduration = pendulumduration + pendulumtime[pendulumled];
// show leds
strip.show();
} // end of main loop
}
// create 15 bit color value r,g,b
unsigned int color(byte g, byte b, byte r)
{
//take lowest 5 bits of each value , append them end end
return( ((unsigned int)g & 0x1f )<<10 | ((unsigned int)b & 0x1f)<<5 | (unsigned int)r & 0x1f);
}
//input value 0 127 color value.
//the colours transition r - g -b - r
unsigned int wheel(byte wheelpos)
{
byte r,g,b;
switch(wheelpos >> 5)
{
case 0:
r=31- wheelpos % 32; //red down
g=wheelpos % 32; // green up
b=0; //blue off
break;
case 1:
g=31- wheelpos % 32; //green down
b=wheelpos % 32; //blue up
r=0; //red off
break;
case 2:
b=31- wheelpos % 32; //blue down
r=wheelpos % 32; //red up
g=0; //green off
break;
}
return(color(r,g,b));
}
[/code]
Arduino Forum > Using Arduino > Programming Questions > Sketch slowing down... General noob proggie questions
arduino
Comments
Post a Comment