Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. float rps[4]={0.00,0.00,0.00,0.00};
  2. long int PRINT_PERIOD = 1000;
  3. long int lastTimePrinted = 0;
  4.  
  5.  
  6. void setup(){
  7.  
  8. Serial.begin(9600);
  9.  
  10. DDRD &= ~_BV (7); // pinMode (7, INPUT); // rps_1
  11. DDRB &= ~_BV (0); // pinMode (8, INPUT); // rps_2
  12. DDRC &= ~_BV (1); // pinMode (A1, INPUT); // rps_3
  13. DDRD &= ~_BV (2); // pinMode (2, INPUT); // rps_4
  14.  
  15. }
  16.  
  17.  
  18. float rps_out(int k){
  19.  
  20. static int lastReedState;
  21. static unsigned long lastTransition;
  22. static float rps_result;
  23. int reedState;
  24. unsigned long now;
  25. unsigned long revolutionTime;
  26.  
  27. if ( k == 1 ) reedState = (PIND & _BV (7)) == 0; // digitalRead (7);
  28. else if ( k==2 ) reedState = (PINB & _BV (0)) == 0; // digitalRead (8);
  29. else if ( k==3 ) reedState = (PINC & _BV (1)) == 0; // digitalRead (A1);
  30. else if ( k==4 ) reedState = (PIND & _BV (2)) == 0; // digitalRead (2);
  31.  
  32. // On a rising transition of the reed switch:
  33. if (reedState == HIGH && lastReedState == LOW) { // HIGH LOW measuring the low period // LOW HIGH measuring the high period
  34. // Compute time since last valid transition.
  35. now = micros();
  36. revolutionTime = now - lastTransition; // measuring the High period
  37.  
  38.  
  39. // Remember this transition.
  40. lastTransition = now;
  41.  
  42. }
  43.  
  44. // Remember last state.
  45. lastReedState = reedState;
  46.  
  47. return rps_result;
  48.  
  49. }
  50.  
  51.  
  52. void loop() {
  53.  
  54. rps[0] = rps_out(1);
  55. rps[1] = rps_out(2);
  56. rps[2] = rps_out(3);
  57. rps[3] = rps_out(4);
  58.  
  59. if (millis() - lastTimePrinted >= PRINT_PERIOD) {
  60.  
  61.  
  62. Serial.println(rps[0]);
  63. Serial.print('.');
  64. Serial.println(rps[1]);
  65. Serial.print('.');
  66. Serial.print(rps[2]);
  67. Serial.print('.');
  68. Serial.println(rps[3]);
  69.  
  70.  
  71. lastTimePrinted = millis();
  72.  
  73. }
  74.  
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement