Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int revolutions = 0;
- unsigned long lasttime = 0;
- float angularVelocity =0, speed = 0;
- void setup(){
- Serial.begin(9600);
- attachInterrupt(0, domeasure, FALLING);
- }
- void loop(){
- if (millis() - lasttime == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
- detachInterrupt(0); //Disable interrupt when calculating
- angularVelocity = float(revolutions * 6.28); // angular velocity = rotation per second (Hz) * 2*Pi
- speed = angularVelocity * 0.12; // speed = angular velocity * 0.12 m (m/s)
- speed = speed * 3.6;// convert m/s to km/h
- Serial.print("Speed =\t");
- Serial.print(speed);
- revolutions = 0; // Restart the rev counter
- lasttime = millis(); // Uptade lasmillis
- attachInterrupt(0, domeasure, FALLING); //enable interrupt
- }
- }
- // this code will be executed every time the interrupt 0 (pin2) gets low.
- void domeasure(){
- revolutions++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement