Breadboarding

Fahrrad_Drehzahl

Feb 14th, 2016
996
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. volatile float rpm = 0;
  2. unsigned long lastInt = 0; //Letzter Interrupt
  3. unsigned long period = 0; //aktuelle Zeit (Millisekunden)
  4.  
  5. void setup() {
  6.   Serial.begin(9600);
  7.   attachInterrupt(0, rpm_backwheel, FALLING); //interrupt 0 is on pin 2
  8. }
  9.  
  10. void loop() {
  11. }
  12.  
  13. void rpm_backwheel() { /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  14.   period = millis() - lastInt; //Periodendauer zu letztem Impuls
  15.   lastInt = millis();
  16.   serialOut();
  17. }
  18.  
  19. void serialOut() {
  20.   rpm = (1000 / period) * 60;
  21.   Serial.print("RPM: ");
  22.   Serial.println(rpm);
  23.   Serial.print("Hz:");
  24.   Serial.println(1000 / period);
  25. }
RAW Paste Data