<- back to main Article

count external interrupts

use external interrupt to count pulses

/**************************************************************************************************
*
*   photoInterrupter_countWithInterrupt_00
*
*   Version:      00 - Mai 2010
*   Author:       Tom Pawlofsky www.caad.arch.ethz.ch tom-DOT-pawlofsky-AT-arch-DOT-ethz-DOT-ch
*
*   Desc:         count the events on a photointerrupter / lightbarrier within an interrupt
*
***************************************************************************************************/

// using a Sharp GP1A53HR
// the Infrared-Led is connected
// no 1 Anode via a 470 Ohm to 5V -> less then 10 mA
// no 2 Cathode to Gnd
// the Recievers Pins are connect
// no 3 Vcc to Vcc 5V
// no 4 Vo  to Digital Input Pin of MicroController
// no 5 GND to Ground Gnd
//
// Note: this Photointerrupter has a Schmitt Trigger as Output
// this reduces the sensitivity for noise and will always give back LOW or HIGH
//


// this methode of counting offers much better performance then countinously comparing the lightBarrier-Status in the loop script
//


int pinLB = 2; // LIghtBarrier, Photointerrupter -> you must use Pin 2 or 3 see http://arduino.cc/en/Reference/AttachInterrupt
int counter = 0;

void setup(){
        // start serial communication
        Serial.begin(9600);

        pinMode(pinLB,INPUT);

        attachInterrupt(0,countLbEvent,RISING); // count wenn Lightbarrier gets closed
        // index 0 will refer to pin 2
        // there the following modes
        //  attachInterrupt(0,countLBEvent,LOW);
        //  attachInterrupt(0,countLBEvent,CHANGE);
        //  attachInterrupt(0,countLBEvent,RISING);
        //  attachInterrupt(0,countLBEvent,FALLING);
}



void loop(){

        // feedback of counter
        Serial.print("counter ");
        Serial.println(counter,DEC);

        //
        //delay(300);
}

// keep this function smart and short
// no parameters, no return value
void countLbEvent(void) {
        counter++;
}


<- back to main Article


Article by Tom Pawlofsky Mai 2010

contact
pawlofsky–AT-arch-DOT-ethz-DOT-ch
please feel free to email any comments, misstakes or improvements.


this page is linked to:

This website has been archived and is no longer maintained.