Go Down

Topic: Use Timer instead of Delay (Read 1 time) previous topic - next topic


Dear Diyers,

I'm trying to make a ADSR (envelop generator for synthesizer ) with arduino
i use a 7HC595 with a 2R ladder to make a DAC to generate envelop
A input Multiplexer 4051 scan 8 potentiometers. Only Four potentiometers  Attack, Decay, Sustain, Release are used.
I would like to replace this part of code because i couldn 't use Delay function in my program
How can  i modify my code to use a Timer ?
thank you for your help

Code: [Select]
void loop()

 //scanner les entrĂ©es du 4051
 for (count=0; count<=7; count++)

 oldgate = stategate;  
 stategate = digitalRead (gate);
 if (stategate == HIGH && oldgate != stategate){
   for (int i =0; i<255 ; i++){    
     digitalWrite (latchPin, 0);
     shiftOut (dataPin,clockPin,i );
     digitalWrite (latchPin, 1);
     delayMicroseconds ( valuepot[0] +1);
   for (int j=255; j> valuepot[2]; j--){
     digitalWrite (latchPin, 0);
     shiftOut (dataPin,clockPin,j );
     digitalWrite (latchPin, 1);
     delayMicroseconds ( valuepot[1] +1);
 if (stategate ==LOW && oldgate == HIGH){
   for (int k = valuepot[2] ; k>0; k--){
     digitalWrite (latchPin, 0);
     shiftOut (dataPin,clockPin,k );
     digitalWrite (latchPin, 1);
     delayMicroseconds (  valuepot[3] +1);

Udo Klein

Maybe you should specify what your code is supposed to do in more detail? It is pretty hard to infer from code that does not meet the target specification what the intended specification might be.
Check out my experiments http://blog.blinkenlight.net


The BlinkWithoutDelay tutorial example shows the principle involved.

Clearly you have included only a sample of your code, so I am having difficulty seeing what change of program flow you want to achieve. The delayMicrosecond "needs" to be there. I guess that you want to do something else in the code, while the "note" is is being played out via your shiftout routine.

How to do two things at once, there is an explanation in the playgorund and there is another example here.


Thank you for your fast answer
i will try to replace Delay with millis function


millis looks a little coarse for the example you gave, try micros instead
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


What is wrong with the delays you have? They're short enough but perhaps too short?

For really short delays, turn interrupts off, run a loop that does nothing, then turn interrupts back on.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

shortcuts: alt+s submit/post or alt+p preview