I'm new to Arduino and been scratching my head around my recent project. I'm looking for some guidance on how to loop around two beatsin16 functions one after the other.
At the moment I have two sub-routines that consist of the beatsin16 function. Each is set-up slightly different with BPMs, duration and phase. I'm happy how each sub-routine operates individually, but the end goal is to run Routine 1 once, wait until this is complete (duration is 750ms ), then run Routine 2 once, wait until this is complete (duration is 1500ms) before repeat to Routine 1 again and so on..
Whenever I introduce a time delay in the main Loop() to run Routine 1, delay 750ms, then Routine 2, this just breaks down the beatsin functions and they don't operate as they did individually.
Please post a sketch that runs just one of the functions and does so to your satisfaction.
It looks like the two are mostly the same, but there are enough oddities that I request you also please post a sketch that runs just the other and does so to your satisfaction.
There are things in both that seem to be entirely without purpose. So it would be good to see each in its original context.
I'm happy with how both sub-routines work individually. The sketch for these are below.
My end goal is for Routine1 to complete once - this is for a trailing LED to light up in accordance to the beatsin function defined (the defined BPM, duration and phase position are all good). Once this function has completed one cycle (duration is 750ms) is to then start Routine2 for one cycle (duration is 1500ms). Routine2 is similar but this moves the trailing LED in the opposite direction and for a different BPM and, duration and phase position.
So in the main loop i can't seem to get it to complete the routines in this order. They either don't work at all or both routines start simultaneously.
You will likely need to write your own beatsin function, as it is you have no idea at which point in the LED strip the function will start.
There also needs to be a way to detect when the code has gone through a complete cycle, then you can run the first function until the cycle finished, then run the second function until its cycle finishes, etc.
They absolutely do not. Run the code below and observe the printouts on the Serial Monitor. Note that each Routine starts and completes before switching to the other Routine.
The function completes, but the OP needs to clarify, as I read it the desire is for the LED strip to complete a full cycle of the sin wave within each function, not just display a single point from each function.
Both functions are designed to be called frequently. Very frequently.
To do one for awhile, then the other, call one a few thousand times, then a call the other one a few thousand times.
Or maybe add some kind of counter and indication of having "completed" whatever portion of their otherwise infinite activity you want to chop it down to.
I should have clarified earlier. My end goal it so have Routine1 to start and completely stop after 1 cycle (duration is 750ms) before starting Routine 2.
My confusion was that it looked as if they both started simultaneously because the each routine was continuously running albeit at different rates.
Agreed. An engineer needs to write their requirements / problem statement with absolute clarity. I insist upon that with the engineers who work for me.
Just so I could see what only calling it a fixed number of times just once looks like:
void loop() {
for (int ii = 0; ii < 333; ii++) {
Routine1();
}
while (1); // hangs here forever
}
I placed a while statement that never finishes after the for statement.
Obvsly you don't wanna do that usually.
Im in transit, so I can't try
void loop() {
for (int ii = 0; ii < 333; ii++) {
Routine1();
}
for (int ii = 0; ii < 333; ii++) {
Routine2();
}
}
That should do a red traveler and then a blue traveler and then a red traveler…
I am compelled to repeat that this may work, but it will mean you can't do much of anything else.
In a sketch that does very much, the loop() function should not have within it any loops that take significant time, as it would be time away from paying attention to switches and sensors and processes other than making the travelers go back and forth.
Ideally the loop() could be free to run thousands of times a second and pay attention to everything as long as all players behave.
Your Routine() and Routine2() were design to "play nice" and not hog the resource which is processor time.
What larger project is this to be part of? That is to ask if all this matters in any way.