Advertisement
Guest User

Untitled

a guest
May 31st, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. void setup() {
  2.   Serial.begin(9600);
  3.   pinMode(2,INPUT_PULLUP);
  4.   pinMode(13,OUTPUT);
  5.   attachInterrupt(digitalPinToInterrupt(2), do_count, FALLING);
  6.   delay(1000);
  7. }
  8.  
  9. int num_stops = 4;
  10.  
  11. volatile boolean led_state=0;
  12. volatile int trigs = 0;
  13. volatile long time_first_trig = 0;
  14. volatile long time_last_trig = 0;
  15.  
  16. void do_count() {
  17.   trigs++;
  18.   digitalWrite(13,led_state=!led_state);
  19.   long time_now = micros();
  20.   if (!time_first_trig) time_first_trig = time_now;
  21.   time_last_trig = time_now;
  22. }
  23.  
  24. void loop() {
  25.   Serial.print("pin="); Serial.print(digitalRead(2)); Serial.print(" ");
  26.   Serial.print("trigs="); Serial.print(trigs); Serial.print(" ");
  27.  
  28.   noInterrupts();
  29.   long td = (time_last_trig - time_first_trig);
  30.   double rps = (trigs-1)/(td/1000000.0)/num_stops;
  31.   interrupts();
  32.   Serial.print("td="); Serial.print(td); Serial.print(" ");
  33.   Serial.print("rps="); Serial.print(rps); Serial.print(" ");
  34.   Serial.print("rpm="); Serial.print(rps*60); Serial.print(" ");
  35.   Serial.println("");
  36.   trigs=0;
  37.   time_last_trig=0;
  38.   time_first_trig=0;
  39.   delay(1000);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement