Pages: [1]   Go Down
Author Topic: Use Timer instead of Delay  (Read 1156 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void loop()
{

  //scanner les entrées du 4051
  for (count=0; count<=7; count++)
  {
    InitialiseShift();
    ValuePotScan();

  }
  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);
    }      
  }
}
Logged

0
Online Online
Faraday Member
**
Karma: 24
Posts: 3499
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Check out my experiments http://blog.blinkenlight.net

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 33
Posts: 1228
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

millis looks a little coarse for the example you gave, try micros instead
Logged

"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.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4837
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: