Loading...
Pages: [1]   Go Down
Author Topic: Use Timer instead of Delay  (Read 546 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
Offline Offline
Faraday Member
**
Karma: 16
Posts: 3196
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
Online Online
God Member
*****
Karma: 17
Posts: 833
Have you testrun your PDE 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
Online Online
Brattain Member
*****
Karma: 137
Posts: 19021
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.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2905
I only know some basic electricity....
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

Examples can be found at Learning in the Main Site and at the Playground

Pages: [1]   Go Up
Print
 
Jump to: