Go Down

Topic: Arduino + Musical Instrument Shield (Read 9255 times) previous topic - next topic

janost

#15
Apr 19, 2014, 08:57 pm Last Edit: Apr 19, 2014, 09:02 pm by janost Reason: 1
By nonblocking (no delay) I mean you have to find a way to delay (or not do anything) based on time.

That means one of 2 things, either you do your tasks based on time (a counter) or you do it with an ISR.

The first option works like you take action at intervals.
Or  timestamped as Grumpy said.

Timeslice your main loop by checking events.
Either something received or time lapsed.

Don't block it by waiting.
If it's not time to do something, go ahead and do something else.

If you block your code by using a delay it cant check or do multiple tasks.
It's sort of multitasking, do all at once.



 

cooper_hu

Awesome I'm going to read this now, thanks! I'll check back in a bit!

janost

We who are more used to writing realtime code dont't use delays in our code.
We want all the CPU cycles we can get.

You do it like this:

You have 2 kind of events, either a key played or time lapsed.
Increment a counter, if time reached or event happened, do your stuff.

By nonblocking you just increment the counter and do any if count or event.

It's easy.

cooper_hu

I am so confused!

I now understand how millis() is used to get the LED blinking on it's own cycle, and how it's the better way to go about it since delay() is a blocking function. But I still don't understand how to use it…

Code: [Select]


void loop {

noteOn(0,note1,100);
//millis() in here?
noteOff(0,note,100);

}



???

janost

When you do note on you record a time, zero or anything and clear a counter.
When the counter reach a value you turn the note of.

This isn't easy to explain, give me some help any one?

janost

I cant write a code sample.
It will break the rules against showing code.

cooper_hu

lol I appreciate you trying! I realize how valuable knowing how to use this, just can't seem to wrap my head around it! yet… ^_^

Aghhh just a wee sample code ??

janost

If you put an "if" statement in your main loop.
How many times does it get past?

cooper_hu

Quote
f you put an "if" statement in your main loop.
How many times does it get past?


I'm confused on what you're asking? When you say "it" do you mean the if statement?

janost

It was a joke.

You have to write it as a state machine.


cooper_hu


Grumpy_Mike

There is no rule about showing code especially code fragments.  :)
The structure of your loop function is fundamentally diffrent now.
You do not follow the do this then do that then do the other order of things. That is what gets you trapped.
Your code should be
Start loop
Has the thing that triggers the note happened ... -> send note on message, set up note off pending time

Is a note off pending and is it time to turn off ...... -> send note off and clear pending indicator
End loop

Read the examples in the state machi be page.


cooper_hu

If it's so easy could you please just show me! I appreciate you guys taking the time to respond to the thread, but your coyness hasn't been at all helpful!! I've been trying to make sense of your input all day and haven't had any luck. I understand the theory, but have no clue how to execute. Could someone please be a bit more helpful??

janost


If it's so easy could you please just show me! I appreciate you guys taking the time to respond to the thread, but your coyness hasn't been at all helpful!! I've been trying to make sense of your input all day and haven't had any luck. I understand the theory, but have no clue how to execute. Could someone please be a bit more helpful??


You have to break down your problem into a  time stamped solution.
It has to be done in steps.

Grumpy already showed you how to do it.

Go Up