Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. //#include <SoftwareSerial.h>
  2. //#define rxPin 9
  3. //#define txPin 10
  4. //
  5. //SoftwareSerial mySerial(rxPin, txPin);
  6.  
  7. unsigned long start;
  8. const byte encoderPinA = 2;//A pin -> interrupt pin 0
  9. const byte encoderPinB = 4;//B pin -> digital pin 4
  10. volatile long pulse;
  11. volatile bool pinB, pinA, dir;
  12. const byte ppr = 8, upDatesPerSec = 10;
  13. const int fin = 100;
  14. const float konstant = (60.0 * upDatesPerSec / (16));
  15. double rpm = 0, rpm2 = 0, rpm3 = 0, rpm4 = 0, rpm5 = 0,
  16. rpm6 = 0, rpm7 = 0, rpm8 = 0, rpm9 = 0, rpm10 = 0,
  17. rpm11 = 0, rpm12 = 0, rpm13 = 0, rpm14 = 0, rpm15 = 0,
  18. rpm16 = 0, rpm17 = 0, rpm18 = 0, rpm19 = 0, rpm20 = 0,
  19. rpm21 = 0, rpm22 = 0, rpm23 = 0, rpm24 = 0, rpm25 = 0,
  20. rpm26 = 0, rpm27 = 0, rpm28 = 0, rpm29 = 0, rpm30 = 0,
  21. rpm31 = 0, rpm32 = 0, rpmt = 0;
  22. int val, previousState , currentState, readEncoder = 2;
  23.  
  24. void setup() {
  25. // pinMode(rxPin, INPUT);
  26. // pinMode(txPin, OUTPUT);
  27. // mySerial.begin(9600);
  28.  
  29. Serial.begin(9600);
  30. // Serial1.begin(9600);
  31. // attachInterrupt(0, readEncoder, RISING);
  32. pinMode(encoderPinA, INPUT);
  33. pinMode(encoderPinB, INPUT);
  34. pinMode(readEncoder, INPUT);
  35. }
  36.  
  37. void loop() {
  38. // analogWrite(10, 255);
  39. val = digitalRead(readEncoder); // read input value
  40. if (val == HIGH) { // check if the input is HIGH (button released)
  41. // digitalWrite(ledPin, HIGH); // turn LED on
  42. currentState = 1;
  43. }
  44. else {
  45. // digitalWrite(ledPin, LOW); // turn LED off
  46. currentState = 0;
  47. }
  48.  
  49. if (currentState != previousState) {
  50. if (currentState == 1) {
  51. pulse++;
  52. // counter = counter + 1;
  53. // Serial.println(counter);
  54. }
  55. }
  56.  
  57. previousState = currentState;
  58.  
  59. if (millis() - start > fin)
  60. {
  61. start = millis();
  62. rpm = pulse * konstant;
  63. rpmt = (rpm + rpm2 + rpm3 + rpm4 + rpm5 + rpm6 + rpm7 + rpm8 + rpm9 + rpm10 + rpm11 + rpm12 + rpm13 + rpm14 + rpm15 + rpm16) / 16;
  64. // rpmt = (rpm + rpm2 + rpm3 + rpm4 + rpm5 + rpm6 + rpm7 + rpm8 + rpm9 + rpm10 + rpm11 + rpm12 + rpm13 + rpm14 + rpm15+rpm16 + rpm17 + rpm18+ rpm19 + rpm20 + rpm21 + rpm22 + rpm23 + rpm24 + rpm25 + rpm26 + rpm27 + rpm28 + rpm29 + rpm30 + rpm31 + rpm32) / 32;
  65. Serial.println(rpmt);
  66. // mySerial.println("asup");
  67. // Serial.print(rpm);
  68. // Serial.print("\t");
  69. // Serial.println(pulse);
  70. // rpm32 = rpm31;
  71. // rpm31 = rpm30;
  72. // rpm30 = rpm29;
  73. // rpm29 = rpm28;
  74. // rpm28 = rpm27;
  75. // rpm27 = rpm26;
  76. // rpm26 = rpm25;
  77. // rpm25 = rpm24;
  78. // rpm24 = rpm23;
  79. // rpm23 = rpm22;
  80. // rpm22 = rpm21;
  81. // rpm21 = rpm20;
  82. //
  83. // rpm20 = rpm19;
  84. // rpm19 = rpm18;
  85. // rpm18 = rpm17;
  86. // rpm17 = rpm16;
  87. rpm16 = rpm15;
  88. rpm15 = rpm14;
  89. rpm14 = rpm13;
  90. rpm13 = rpm12;
  91. rpm12 = rpm11;
  92. rpm11 = rpm10;
  93. rpm10 = rpm9;
  94. rpm9 = rpm8;
  95. rpm8 = rpm7;
  96. rpm7 = rpm6;
  97. rpm6 = rpm5;
  98. rpm5 = rpm4;
  99. rpm4 = rpm3;
  100. rpm3 = rpm2;
  101. rpm2 = rpm;
  102. pulse = 0;
  103. }
  104. }
  105.  
  106. //void readEncoder() {
  107. // // state = !state;
  108. // // delay(10);
  109. // static unsigned long last_interrupt_time = 0;
  110. // unsigned long interrupt_time = millis();
  111. // // If interrupts come faster than 200ms, assume it's a bounce and ignore
  112. // if (interrupt_time - last_interrupt_time > 10)
  113. // {
  114. // pulse++;
  115. // }
  116. // last_interrupt_time = interrupt_time;
  117. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement