MajicDesigns / Chroniker

A LED Neopixel Analog Clock with multiple control interfaces (Bluetooth, IR Remote, tact switch)
2 stars 3 forks source link

Replacing a demo pattern with another #1

Closed urkel27 closed 4 years ago

urkel27 commented 5 years ago

I am trying to learn off this project how to work with interrupts to switch patterns during a pattern loop using "millis" statements. I am assuming these are "state machines"?

I am trying to replace one of the case statements (demo pattern 2) to use the "BouncingColoredBalls" pattern in place of i.e "demoRainbowWithGlitter". This pattern usually runs for infinity and wanted to see if I could get it to switch using the interrupts you have in place.

I am having trouble understanding the "(bool bInit)" portion of "void demoRainbowWithGlitter(bool bInit)". I am assuming it is calling something in FastLED library and may need to implement it somehow?

The "BouncingColoredBalls" pattern is found here: https://www.tweaking4all.com/forums/topic/bouncy-ball-effect/ https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/

I am also trying to figure out how to tweak "BouncingColoredBalls" to work with your sketch.

i.e. Case 2 calling:

(built in tool did not format correctly) xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx case 0: demoCylon(newDemo); newDemo = false; break; case 1: demoRainbow(newDemo); newDemo = false; break; // case 2: demoRainbowWithGlitter(newDemo); newDemo = false; break; case 2: byte bbcolors[5][3] = { {0xff, 0,0}, // red {0, 0xff, 0}, // green {0, 0, 0xff}, // blue {0xff, 0xff, 0xff},// white {0xff, 0xff, 0} }; // yellow BouncingColoredBalls(5, bbcolors);(newDemo); newDemo = false; break; case 3: demoConfetti(newDemo); newDemo = false; break; case 4: demoSinelon(newDemo); newDemo = false; break; case 5: demoBPM(newDemo); newDemo = false; break; case 6: demoJuggle(newDemo); newDemo = false; break; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I am probably way off :-). I dont think it falls in the correct syntax as you have laid out. Also looking at other examples, I see you did restructure a few things such as in the Cylon pattern, defining case1/case2. I am wondering how much I have to tweak, i.e change "show.pixel" to " FastLED.show();" etc. I am relatively new to programming and any assistance is greatly appreciated!

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx void BouncingColoredBalls(int BallCount, byte colors[][3]) { if (bInit) initDemo();

if (millis() - timeStart < ANIMATION_DELAY) return; timeStart = millis();

float Gravity = -9.81; int StartHeight = 1;

float Height[BallCount]; float ImpactVelocityStart = sqrt( -2 Gravity StartHeight ); float ImpactVelocity[BallCount]; float TimeSinceLastBounce[BallCount]; int Position[BallCount]; long ClockTimeSinceLastBounce[BallCount]; float Dampening[BallCount];

for (int i = 0 ; i < BallCount ; i++) {
ClockTimeSinceLastBounce[i] = millis(); Height[i] = StartHeight; Position[i] = 0; ImpactVelocity[i] = ImpactVelocityStart; TimeSinceLastBounce[i] = 0; Dampening[i] = 0.90 - float(i)/pow(BallCount,2); }

while (true) { for (int i = 0 ; i < BallCount ; i++) { TimeSinceLastBounce[i] = millis() - ClockTimeSinceLastBounce[i]; Height[i] = 0.5 Gravity pow( TimeSinceLastBounce[i]/1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i]/1000;

  if ( Height[i] < 0 ) {                      
    Height[i] = 0;
    ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
    ClockTimeSinceLastBounce[i] = millis();

    if ( ImpactVelocity[i] < 0.01 ) {
      ImpactVelocity[i] = ImpactVelocityStart;
    }
  }
  Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
}

for (int i = 0 ; i < BallCount ; i++) {
  setPixel(Position[i],colors[i][0],colors[i][1],colors[i][2]);
}

showpixel();
setAll(0,0,0);

} } xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

MajicDesigns commented 5 years ago

I am assuming these are "state machines"?

Yes

I am having trouble understanding the "(bool bInit)"

When true, this parameter tells the demo function it is being called for the first time. This usually means that it should be doing any initialisation required for that demo type. Most of the current call initDemo() as a common function and then do any local stuff as required.

I am also trying to figure out how to tweak "BouncingColoredBalls" to work with your sketch.

I would think that you can forgo the ballcount and bbcolors array in the main code and just declare them as static variables array in the demo function. Initialise this when you are called for the first time (bInit == true).

I am wondering how much I have to tweak, i.e change "show.pixel" to " FastLED.show();" etc.

Clearly you need to make it work with the library. You also need to make sure that the code does not loop forever in the function or you will never have anything else being able to run. The usual way is to make it a FSM and break up the code into timed sections so that while it is 'waiting' for its animation delay time it relinquishes control to other tasks so they can run.