Получите скорость чтения данных из роторного энкодера - PullRequest
0 голосов
/ 10 апреля 2020

Я читаю данные о положении с поворотного энкодера с помощью этого кода:

#define outputA 6
#define outputB 7

int counter = 0; 
int aState;
int aLastState;  

void setup()
{
    pinMode (outputA,INPUT);
    pinMode (outputB,INPUT);
    Serial.begin (9600);
    // Reads the initial state of the outputA
    aLastState = digitalRead(outputA);   
} 

void loop()
{
    aState = digitalRead(outputA); // Reads the "current" state of the outputA

    // If the previous and the current state of the outputA are different, that means a Pulse has occured
    if (aState != aLastState)
    {
        // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
        if (digitalRead(outputB) != aState) 
        { 
            counter ++;
        }
        else 
        {
            counter --;
        }
        Serial.print("Position: ");
        Serial.println(counter);
    } 
    aLastState = aState; // Updates the previous state of the outputA with the current state
}

Что мне нужно, если я читаю 20 данных за 1 секунду, я хочу получить 20 data/s информацию о скорости чтения данных , И я хочу измерять скорость непрерывно каждую 1 секунду (даже я не меняю положение). Как я могу это сделать?

...