Új hozzászólás Aktív témák

  • Tomika86

    senior tag

    Kibogaráztam egy példaprogramot, ez impulzusokat számol:
    const int hallPin = 2; // pin 2 = int 0
    volatile unsigned long cntTime = 0;
    volatile unsigned long cnt = 0;

    void doCount()
    {
    cnt++;
    cntTime = millis();
    }

    void setup()
    {
    Serial.begin(9600);
    attachInterrupt(digitalPinToInterrupt(hallPin), doCount, FALLING); // hall pin on interrupt 0 = pin 2
    cntTime = millis();
    }

    volatile unsigned long rpm = 0;
    unsigned long measureTime = 0, curTime, startTime = 0;
    int dispCnt = 0, measureCnt = 0;

    const int resetTime = 2000;
    const int minRotNum = 1; // 1 - calc after every rotation

    void loop()
    {
    curTime = millis();
    if ( curTime - cntTime > resetTime) // reset when less than 30RPM (dt>2s)
    {
    cnt = measureCnt = 0;
    rpm = 0;
    }
    if (cnt == 1) startTime = cntTime;
    if (cnt - measureCnt >= minRotNum) {
    rpm = 60000L * (cnt - measureCnt) / (cntTime - measureTime);
    measureCnt = cnt;
    measureTime = cntTime;
    }
    }

    60000L itt mi az L?

Új hozzászólás Aktív témák