Advertisement
Guest User

bike2

a guest
Nov 24th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1.  int revolutions = 0;
  2.  unsigned long lasttime = 0;
  3.  float angularVelocity =0, speed = 0;
  4.  
  5.  void setup(){
  6.  Serial.begin(9600);
  7.  attachInterrupt(0, domeasure, FALLING);
  8.  }
  9.  void loop(){
  10.  if (millis() - lasttime == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
  11.  detachInterrupt(0); //Disable interrupt when calculating
  12.  angularVelocity = float(revolutions * 6.28); // angular velocity = rotation per second (Hz) * 2*Pi
  13.  speed = angularVelocity * 0.12; // speed = angular velocity * 0.12 m (m/s)
  14.  speed = speed * 3.6;// convert m/s to km/h
  15.  Serial.print("Speed =\t");
  16.  Serial.print(speed);
  17.  revolutions = 0; // Restart the rev counter
  18.  lasttime = millis(); // Uptade lasmillis
  19.  attachInterrupt(0, domeasure, FALLING); //enable interrupt
  20.   }
  21.  }
  22.  // this code will be executed every time the interrupt 0 (pin2) gets low.
  23.  void domeasure(){
  24.   revolutions++;
  25.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement