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

  • Tomika86

    senior tag

    válasz Aryes #15598 üzenetére

    // Fordulatszám méréshez
    const int hallPin = 2; // pin 2 = int 0
    volatile unsigned long cntTime = 0;
    volatile unsigned long cnt = 0;
    volatile unsigned long rpm = 0;
    unsigned long readings[numReadings];
    unsigned long readIndex;
    unsigned long total;
    unsigned long average;
    const byte numReadings = 2;
    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;
    }
    // Smoothing RPM:
    total = total - readings[readIndex];
    readings[readIndex] = rpm;
    total = total + readings[readIndex];
    readIndex = readIndex + 1;
    if (readIndex >= numReadings)
    {
    readIndex = 0;
    }
    average = total / numReadings;

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

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

Hirdetés