<- back to main Article

continuously check status and count

this example shows how to check the status of a light barrier continuously within the main loop and therefore detect changes and count them. This method is not very efficient and is only recommended for slow events.

/**************************************************************************************************
*
*   photoInterrupter_simple_01
*
*   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 by continously checking its status in the loop - not recommended
*
***************************************************************************************************/

// 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
//


// for improved performance see other examples
// to improve performance remove Serial.print / println


int pinLB = 12; // LIghtBarrier, Photointerrupter

int counter = 0;

boolean lastLbStatus;


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

        // define pins
        pinMode(pinLB,INPUT);
        // inital value of lastLbStatus
        lastLbStatus = digitalRead(pinLB);

}



void loop(){
        boolean newLbStatus;
        newLbStatus = digitalRead(pinLB);

        // dedect CHANGE
        if (lastLbStatus != newLbStatus) {
                Serial.println("CHANGE");
        }

        // dedect RISING
        if (int(newLbStatus - lastLbStatus) == 1) {
                Serial.println("RISING");
                counter++;
        }

        // dedect FALLING
        if (int(newLbStatus - lastLbStatus) == -1) {
                Serial.println("FALLING");
        }

        // remember LbStatus for next loop
        lastLbStatus = newLbStatus;



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

}


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