
int interruptPin = 2; // pin of hall sensor
int ledPin = 3;
const int taketime = 5000;  // time of each measurement
unsigned long time; // set the time variable to time
int val = 0; // set the val variable to count 
void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(interruptPin), count, FALLING); // The trigger signal must be changed. We set the trigger on the falling edge.
  time = millis();// start timing,get the current system time
}

void loop() {
  if (millis() >= time)
  {
    Serial.print("Speed = ");
    if(val>20){
      Serial.print(val); // print the number of output triggers
      digitalWrite(ledPin,HIGH);
      Serial.print("    High speed  \n");
    }
    else{
      Serial.println(val); // print the number of output triggers
      }
    time = millis() + taketime; // mark the future time point, execute the if judgment after 1000ms, and output the result 
    val = 0;// cleared after outputting speed results, recording the number of triggers in the next second
    delay(1000);
    digitalWrite(ledPin,LOW);
  }
}

void count() {
  val += 1;
}
