Répondre à la discussion
Affichage des résultats 1 à 3 sur 3

diminue le delay dans un programme arduino

  1. #1

    diminue le delay dans un programme arduino


    salut tout le monde, pour ce qui est de ma part je me demande si ya moyen de diminue le delay de se programme ( d'habitude ya juste une ligne de code ou on fixe le délais entre deux exécution mais là c'est pas vraiment le cas) ...... ce programme est celui d'un capteur de lumière.... alors si quelqu'un peut me dire comment faut faire sa serai gentille et merci
    // setup the TLS230R to Arduion Mega mapping
    #define TSL_FREQ_PIN 2 // output use digital pin2 for interrupt
    #define TSL_FREQ_INT 2 //pin 18=int5, 21=int2, 2=int0
    #define READ_TM 2 // milliseconds between pulse count tabulation on the interrupt
    #define NUM_BUFFER 5 // number of samples in the buffer for smoothing function
    #define PERCENTILE 15 //cut off for throwing out outliers: bottom/top percentile
    #define OUTPUT_MODE 0 //This will output ascii over serial or binary for graphing on Processing (0=ascii, 1=binary)
    //I'm not really sure what is the best way to do this
    //I want the interrupt to act as a continuous sampling engine
    //I want the loop to get a sample (free of spikes) whenever I want
    //I don't want to get close to actual temporal resolution of interrupt tabulation periods
    //but I don't want to get repeats too often (buffer not updated)
    int ledPin = 13; //led pin #
    int calcSensitivity; //only for sensor with sensitivity setting (230R)
    unsigned long pulseCount = 0; // pulseCount is always in use
    unsigned long currentTime = millis();
    unsigned long startTime = currentTime;
    //I think I need to rethink this hex value for something more appropriate
    unsigned int startTag = 0xDEAD;  // Analog port maxes at 1023 so this is a safe termination value
    int holder=0; //a dummy value for sending over serial ports on unused signals
    unsigned int bufferCounter = 0;
    volatile unsigned int buffer[NUM_BUFFER];
    unsigned int bufferSnapshot[NUM_BUFFER];
    unsigned int nSmoothSample = 0;
    // freq will be modified by the interrupt handler so needs to be volatile
    // freq holds the latest frequency calculation
    unsigned long frequency;
    float uWattCm2;
    volatile unsigned int curPulseCount;
    volatile unsigned int windowCount;
    unsigned int windowCountSnapshot;
    volatile unsigned int timeElapsed;
    volatile unsigned int timeElapsedSample;
    unsigned int timeElapsedSnapshot;
    unsigned int count=0, i=0;
    unsigned int scale;   // holds the TSL scale value, see below
    int ledLevel = 0;
    void setup() {
     //led stuff, if you aren't powering an LED you don't need this
     pinMode(ledPin, OUTPUT);
     ledLevel = map(120, 0, 500, 0, 255);
     digitalWrite(ledPin, ledLevel);
     //initialize arrays
     for (i=0;i<NUM_BUFFER;i++) {
       buffer[i] = 0;
       bufferSnapshot[i] = 0;
     // attach interrupt to pin2, send output pin of TSL235R to arduino 2
     // call handler on each rising pulse
     //something weird may be going on with interrupts with Arduino Mega
     //pin 2, doesn't work on Mega, dunno why
     // attachInterrupt(0, add_pulse, RISING);
     attachInterrupt(0, addPulse, RISING); //interrupt 2, matches pin 21
     pinMode(TSL_FREQ_PIN, INPUT);
     scale = 1; // set this to match TSL_S2 and TSL_S3 for 230R, not needed for 235R
    void loop() {
     nSmoothSample = getSmoothedSample(); //Get smoothed out sample every go round
     if (OUTPUT_MODE == 0) {
       // this is just for debugging
       // it shows that you can sample freq whenever you need it
       // even though it is calculated 100x a second
       // note that the first time that freq is calculated, it is bogus
       Serial.print("winCount: ");
       Serial.print(" count: " );
       Serial.print(" smooth: ");
       //    Serial.print(" freq: ");
       //    Serial.println(getFrequency(nSmoothSample), DEC);
       //might be possible to figure this out for 235R, but this is intended for 230R
       //Serial.print("\tuW/cm: ");
       //Serial.print(getUwattCm2(), DEC);
     else {
       //This has to start with startTag, and then there must be six values
       //passed for the RTG Processing app to display correctly
       Serial.write( (unsigned byte*)&startTag, 2);
       Serial.write((unsigned byte*)&curPulseCount, 2);
       Serial.write((unsigned byte*)&nSmoothSample, 2);
       //   Serial.write((unsigned byte*)&timeElapsed, 2);
       Serial.write((unsigned byte*)&holder, 2);
       //     Serial.write((unsigned byte*)&windowCount, 2);
       Serial.write((unsigned byte*)&holder, 2);
       Serial.write((unsigned byte*)&holder, 2);
       Serial.write((unsigned byte*)&holder, 2);
     delay((READ_TM * NUM_BUFFER)); //delay for msec btwn tabulations * (# of running samples) + 1 to make sure we don't get a repeat
    void addPulse() {
     // DON'T calculate anything or smooth the data every READ_TM ms
     // just store the pulse count to be used outside of the interrupt
     pulseCount++; // increase pulse count
     currentTime = millis();
     timeElapsed = currentTime - startTime;
     //Tabulate pulses if the time elapsed surpasses READ_TM
     if( timeElapsed >= READ_TM ) {
       curPulseCount = pulseCount;  // use curPulseCount for calculating freq/uW
       buffer[bufferCounter] = curPulseCount;
       //increment and roll over if necessary
       if (bufferCounter == (NUM_BUFFER-1)) {
         bufferCounter = 0; // roll over
         windowCount++; //keep track of windows computed
         timeElapsedSample = timeElapsed; //could poll this if you want to see how close this is to READ_TM
       else {
         bufferCounter++; //increment
       pulseCount = 0;
       startTime = millis();
    * This gets the frequency (counts/sec)
    * getFrequency(unsigned int)
    double getFrequency(unsigned int sample) {
     return (sample*(1000.0/timeElapsedSnapshot));
    * This returns the irradiance of light based on things
    * known about the 230R sensor. Could be adapted to 235R possibly.
    long getUwattCm2() {
     // copy pulse counter and multiply.
     // the multiplication is necessary for the current
     // frequency scaling level.
     frequency = curPulseCount * scale;
     // get uW observed - assume 640nm wavelength
     // calc_sensitivity is our divide-by to map to a given signal strength
     // for a given sensitivity (each level of greater sensitivity reduces the signal
     // (uW) by a factor of 10)
     float uw_cm2 = (float) frequency / (float) calcSensitivity;
     // extrapolate into entire cm2 area
     uWattCm2  = uw_cm2  * ( (float) 1 / (float) 0.0136 );
    * Gets rid of spikes in buffer and returns a smoothed
    * value. It also takes a snapshot of the timeElapsed
    * for this particular snapshot of the buffer
    * so it can be used elsewhere
    unsigned int getSmoothedSample() {
     static unsigned int sorted[NUM_BUFFER];
     unsigned int intAvg=0, mod=0;
     unsigned int j=0, nValid=0, temp=0, top=0, bottom=0, total=0;    
     boolean done;
     //This is probably overkill: copy, copy, sort
     //duplicate samples in the buffer at this point in time
     for (i=0;i<NUM_BUFFER;i++) {
       bufferSnapshot[i] = buffer[i];
     //copy the value for use within loop
     timeElapsedSnapshot = timeElapsedSample;
     windowCountSnapshot = windowCount;
     //copy the data into yet another array for sorting
     for (i=0;i<NUM_BUFFER;i++) {
       sorted[i] = bufferSnapshot[i];
     // simple swap sort, sorts numbers from lowest to highest
     for (done=0;done!=1;) {
       done = 1;
       for (j = 0; j < (NUM_BUFFER - 1); j++){
         if (sorted[j] > sorted[j + 1]){     // numbers are out of order - swap
           temp = sorted[j + 1];
           sorted [j+1] =  sorted[j] ;
           sorted [j] = temp;
           done = 0;
     // throw out top and bottom PERCENTILE of samples - limit to throw out at least one from top and bottom
     bottom = max(((NUM_BUFFER * PERCENTILE)  / 100), 1);
     top = min((((NUM_BUFFER * (100-PERCENTILE)) / 100) + 1  ), (NUM_BUFFER - 1));   // the + 1 is to make up for asymmetry caused by integer rounding
     for (j=bottom; j<top; j++){
       total += sorted[j];  // total remaining indices
       nValid++; // # of good values
     intAvg = total/nValid;
     mod = total%nValid;
     //this is simulating floating point math, a bit more accurate than just rounding
     //takes the divisor, multiples the modulus by # of samples and divides it by how
     //many samples it is based on
     return ((intAvg * NUM_BUFFER) + ((mod * NUM_BUFFER)/(nValid)));

    Dernière modification par Antoane ; 24/03/2016 à 15h34. Motif: Remplacement des balises [php] par [CODE].

  2. #2

    Re : diminue le delay dans un programme arduino


    Le délai entre chaque exécution de la boucle est fixé ligne 103: delay((READ_TM * NUM_BUFFER));
    C'est équivalent à delay(10);

  3. #3

    Re : diminue le delay dans un programme arduino

    #define READ_TM 2 // milliseconds between pulse count tabulation on the interrupt
    #define NUM_BUFFER 5 // number of samples in the buffer for smoothing function

    delay((READ_TM * NUM_BUFFER)); //delay for msec btwn tabulations * (# of running samples) + 1 to make sure we don't get a repeat

    2 * 5 = 10 milliseconds
    A+, pat

Discussions similaires

  1. VTT Electrique - Programme Arduino
    Par David_DRS dans le forum Technologies
    Réponses: 4
    Dernier message: 10/05/2015, 15h13
  2. Réponses: 4
    Dernier message: 20/11/2014, 20h12
  3. programme arduino
    Par julienslr dans le forum Électronique
    Réponses: 26
    Dernier message: 24/07/2014, 11h33
  4. Programme sur Arduino
    Par yhyh dans le forum Programmation et langages, Algorithmique
    Réponses: 5
    Dernier message: 22/02/2013, 22h36
  5. Programme C bouton, delay
    Par Groumpif dans le forum Électronique
    Réponses: 17
    Dernier message: 13/07/2010, 16h34
Découvrez nos comparatifs produits sur l'informatique et les technologies.