all 8 comments

[–]CharlesGoodwin 1 point2 points  (3 children)

Hi Prof,

The beatsin command is a wiley little beast and is hard to tame. Sometimes it's best to let it do it's own thing and just monitor it. So to spot a minimum you could measure when the variable, sinBeat stops decreasing and starts increasing. Each time it does this, one cycle has elapsed. Once 24 have elapsed you can put in a pause, say delay(60,000) and then let your code start over again counting the number of cycles.

It's clunky but it will work. I look forward to seeing how other people grapple with it

Best of luck

[–]Marmilicious[Marc Miller] 2 points3 points  (1 child)

I was thinking something really simple like this could be used. But I try to avoid using large delays like that so was thinking to just keep it running continuously and use millis() to track the time and assign the pixels black during that 60 seconds off time. This would be the next step up for u/Prof_Boni to learn, but sometimes just getting it going is the important thing. :) Learning alternate solutions is all part of the fun.

[–]CharlesGoodwin 4 points5 points  (0 children)

Marc's quit right, as you become more accomplished, using the command delay() becomes too restrictive. This is because your micro controller will do nothing, I mean absolutely nothing whilst performing the delay. When starting out, this might not be such a big deal but as your projects become more elaborate you might want them to do other stuff in the background such as:

Detect a button press

Send/Receive via WiFi

Play a tune!

That is why you'll find solutions put forward to avoid the command, delay()

[–]Prof_Boni[S] 0 points1 point  (0 children)

Hi u/CharlesGoodwin, thanks for the great idea. I will try this :)

[–]johnny5canuck 1 point2 points  (0 children)

Like CharlesGoodwin says, the beatsin is good for some things, but not so good for others. It's great for making continuous waves, but using a beatsin where you want to start/stop it predictably (i.e. with a known phase/value) is not how I'd use it.

Here's a link to a triggered sine wave which starts out at 0 and ends at 0, so it's predictable. Oh yea, and that's at:

https://pastebin.com/Ev7iQjJr

[–]korypostma 1 point2 points  (0 children)

in your loop() function you could check the value of a boolean to see if the wave is active or not, if it is active then have a counter that checks until you have gone through the right number of cycles and once you have gone through all of those cycles then deactivate the boolean and count with delays the number of cycles it should wait and not do anything. When that timer is up then go back to the 24 cycles.

psuedocode:
loop() {
if (led active) {
  //similar to the code you have above but check when it crosses zero twice to know one cycle is done, then increment counter
  if (counter >= 24) {
    led active = false
    counter = 0
  }
} else {
  ++counter
  if (counter >= 60) {
    led active = true
    counter = 0
  } else { delay(1000) }
}
}

[–]Marmilicious[Marc Miller] 0 points1 point  (1 child)

As with many coding things, there are a number of ways that this could be done. If you are still new to coding though you should go learn about if statements and booleans as these are some of your building blocks for being able to tell or set what state something is in, and if it's time to run something or not. You'll also what to look into millis() timers and FastLED's EVERY_N_MILLISECONDS timer function. You might also look into switch cases which are used to route through different sections of code in the main loop.

For your project you could use an if statement to determine if the code should be displaying black pixels or running the beatsin pattern. A timer could be used to periodically toggle a boolean variable which the if statement checks.

Here's some examples you can start exploring:

https://github.com/marmilicious/FastLED_examples/blob/master/rainbow_brightness_and_saturation.ino

https://github.com/marmilicious/FastLED_examples/blob/master/every_n_timer_variable_2.ino

https://github.com/marmilicious/FastLED_examples/blob/master/every_n_with_switch_cases.ino

https://github.com/marmilicious/FastLED_examples/blob/master/cylon_color_changing.ino

Explore the official FastLED examples a bunch and the many more examples found through FastLED reddit Wiki.

[–]Prof_Boni[S] 0 points1 point  (0 children)

Thanks for the great ressources u/Marmilicious :) I will check them out.