for loop - sequencing

Hi,

I'm currently learning c++ and have used Arduino as well but I don't really know any other communities that could help with c++ specifically.

I need some help in understanding the for loop to iterate through an 8 step sequence, Ive been doing a video course on c++ so have a basic understanding of the language but apologies if the below is confusing!

my main question is - what speed does the for loop iterate through each step? for instance this basic loop function: I=0; I<8; I++;

how would I control the tempo my sequence iterates through each count?

I was looking at setting up some floats or ints to set a clock speed, such as int 'tempo value' * 96 / 60.

96 been the pulses per beat (24pulses per quarter note). this would give me a clock frequency of 192hz.

120bpm * 96 = 11520 pulses / 60 second = 192 samples/pulses per second.

I'm not asking for help with code because I want to learn this myself but I'm having trouble getting my head round this. I would like to iterate through an 8 step sequence with 8 led's. Each led will turn on and off when it iterates through, I would have a tempo/bpm pot to alter the tempo range from 30 - 240bpm.

If someone can give me some help it would be massively appreciated!

many thanks :slight_smile:
P

For loops are great for stuff in setup, like setting pin numbers in arrays as inputs or outputs, but they're not great for stuff in loop, where you want to spend time doing other things, UNLESS you use delay().

But of course, you don't want to use delays, because they just waste time.

So, you have to unravel the for loops and handle the end conditions explicitly

my main question is - what speed does the for loop iterate through each step?

That depends entirely on what is in the body of the for loop.

how would I control the tempo my sequence iterates through each count?

Block for varying lengths of time. There is a famous function for doing just that. Not generally recommended, and you'll figure out when you try to determine how long to diddle to make the next event happen on time.

The easy way with a for loop is to use the delay() function. That stops processing for the delay time so is like a pause. The delay can be adjusted during execution with the tempo/bpm pot. However, the use of delay will affect the responsiveness of the program.

The better way is to use if statements instead of for loops. Then use the millis() function for timing. That way the pot can be read much more often. The several things at a time tutorial and the using millis for timing tutorial are places to start.

wow thanks for the quick replies! :slight_smile:

I will quickly read that thread and will go through in more detail tonight - I'm guessing the for loop isn't the best method for sequencing and I should be looking at if statements or switch/case?

I've just read that there is clock_t for c++ that deals with time, does anyone have experience with this?

More info on what I want to practice - basic 8 step sequence that outputs gate/trig signals with a tempo control. each step will have a button thats an ON/OFF to output the gate/trig.

I was looking at taking a pin out from an stm32 and converting it with the logic level converter to 5v.

Is it more sensible to use c code rather than c++ ?

thanks,
P

It is more usual to use millis() for timing on the Arduino. Unless you need to keep exact time for periods longer than a day. Then you need an RTC chip. (Real time clock.)

C is easier to learn for most people. It took me decades to get my head into the object-oriented parts of C++. But C is a subset of C++ so you can write "your" code in C and still use C++ objects like the Serial object without understanding how objects work.

thanks Morgan. I've only been doing this a few month :fearful: and probably jumping in at the deep end using eclipse ide and learning c++ on udemy - looking to grab a discovery or nucleo board! a little more research and ive discovered 'interrupt' so this may help me :slight_smile: I've bought a few dsp books and c++ books and need to grab a few more. BasicSynth is helping me a lot to understand waveforms, sampling and sample rates but theres not much about sequencing. If anyone has any reading suggestions let me know :slight_smile:

planck_epoch:
I would like to iterate through an 8 step sequence with 8 led's. Each led will turn on and off when it iterates through, I would have a tempo/bpm pot to alter the tempo range from 30 - 240bpm.

If this is the main goal, I'd set up a self-resetting timer which increments a (counter) value, the contents of which are used as the argument for a switch/case structure. Put the LED code within the case(s).

Check out this state machine tutorial by @larryd.

" But of course, you don't want to use delay, because they just waste time."

Let me quote another famous authority

"....ILLOGICAL..."

Your program task is to sequence thru events, there are NO other tasks.
The machine does not care if its "looping thru" the "loop()" managing time or just waiting at each sequence step.

If you are serious about learning programming start here - KISS.

But if you want to impress with complexity of your program , well , do whatever floats your boat.

PS
I would suggest two books to read -
" K&R C programming language" and "A (?) Cooper's Inmates are running the asylum".

Hi Vaclav 232, it's been a while.

You going to behave this time?