Go Down

Topic: Use Timer instead of Delay (Read 2172 times) 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);


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.
I speak for myself, not Arduino.


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.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up