Advertisement
DuboisP

Untitled

Jan 12th, 2022
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. // REQUIRES the following Arduino libraries:
  2. // - Adafruit GFX graphics core lib: https://github.com/adafruit/Adafruit-GFX-Library
  3. // - Adafruit OLED mono displays lib: libray https://github.com/adafruit/Adafruit_SSD1306
  4. // - SoftwareSerial : bluetooth
  5.  
  6. /* tutos et références
  7. https://www.e-techno-tutos.com/2021/03/10/bluetooth-hc-05/
  8. https://www.youtube.com/watch?v=eDKbRlADvA0
  9. https://www.carnetdumaker.net/articles/faire-un-tachymetre-avec-une-carte-arduino-genuino-pour-lire-la-vitesse-de-rotation-dun-ventilateur-de-pc/
  10. */
  11.  
  12. #include "time.h"
  13.  
  14. #include <Adafruit_GFX.h>
  15. #include <Adafruit_SSD1306.h>
  16. #include <SoftwareSerial.h>
  17. #include <Wire.h>
  18.  
  19. #define BUTTONPIN 2 // Digital IO pin connected to the button.
  20.  
  21. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  22. // The pins for I2C are defined by the Wire-library.
  23. // On an arduino UNO or Nano: A4(SDA), A5(SCL)
  24. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  25. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  26. #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  27. #define SCREEN_ADDRESS 0x3C //< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  28. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  29.  
  30. // Mapping Arduino Digital ports to Bluetooth Serial
  31. #define BT_TX 10
  32. #define BT_RX 11
  33.  
  34. #define DEBOUNCE 3 // debounce latency in ms
  35. // const int IR_distance = A0; // analog input
  36. #define IR_detection_pin 3 // digital input 12
  37. unsigned long elapsed_time, elapsed_time4min, elapsed_time4sec, start_time4min, start_time4sec;
  38. volatile int NbRPMin = 0;
  39. volatile int NbRPSec = 0;
  40. int NbRPM = 0;
  41. int prevNbRPMin = 0;
  42. int nCoeff = 1;
  43.  
  44. volatile unsigned long IR_time;
  45. volatile unsigned long IR_last_time = 0;
  46.  
  47. volatile unsigned long periode = 0; // comptage retour info ventilo
  48.  
  49. word Median_filter(word t); // filtre lissage sur 5 mesures comptage-tours
  50.  
  51. // SoftwareSerial BT_Serial(BT_TX, BT_RX); // declaring second serial port.
  52.  
  53. void setup() {
  54. // Serial.begin(115200);
  55.  
  56. // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  57. if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
  58. Serial.println(F("SSD1306 allocation failed"));
  59. for(;;); // Don't proceed, loop forever
  60. }
  61.  
  62. // Show initial display buffer contents on the screen --
  63. // the library initializes this with an Adafruit splash screen.
  64. display.display();
  65. delay(1000);
  66.  
  67. // initialize the pushbutton pin as input:
  68. //pinMode(BUTTONPIN, INPUT);
  69. //pinMode(BUTTONPIN, INPUT_PULLUP);
  70. // Uno and Nano : interruptions only on D2 and D3
  71. // attachInterrupt(digitalPinToInterrupt(BUTTONPIN), onButtonD2Event, RISING);
  72.  
  73. // initialize IR pins
  74. // pinMode(IR_detection_pin, INPUT);
  75. pinMode(IR_detection_pin, INPUT_PULLUP);
  76. attachInterrupt(digitalPinToInterrupt(IR_detection_pin), onInfraRedD3Event, RISING);
  77.  
  78. start_time4min = start_time4sec = millis();
  79. }
  80.  
  81. //void onButtonD2Event() {
  82. // nCoeff = nCoeff < 6 ? ++nCoeff : 1;
  83. //}
  84.  
  85. void onInfraRedD3Event() {
  86. //IR_not_detected = digitalRead( IR_detection_pin);
  87. // 1 no detection
  88. // 0 detection
  89. // Serial.println(IR_not_detected);
  90. IR_time = millis();
  91. if (IR_time - IR_last_time > DEBOUNCE) // temporisation x millisecondes pour éviter de compter trop vite :-)
  92. {
  93. ++NbRPMin;
  94. ++NbRPSec;
  95. IR_last_time = IR_time;
  96. }
  97. }
  98.  
  99. void loop(){
  100.  
  101. display.cp437(true); // Use full 256 char 'Code Page 437' font
  102. display.clearDisplay(); // Clear the buffer
  103.  
  104. display.setTextColor(SSD1306_WHITE); // Draw white text
  105. display.setTextSize(1); // Normal 1:1 pixel scale
  106. display.setCursor(0,0); // Start at left-top corner
  107. display.println(F("Revolutions / minute"));
  108.  
  109. // affichage compptage nombre de tours comptés pendant cette minute
  110. display.setCursor(0, 56);
  111. //display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  112. display.print(nCoeff * NbRPMin);
  113.  
  114. // affichage compptage nombre de tours comptés minute précédente
  115. //display.setCursor(64, 56);
  116. //display.print(F("Coeff * "));
  117. //display.print(nCoeff);
  118. display.setCursor(50, 56);
  119. display.print(nCoeff * prevNbRPMin);
  120.  
  121. // affichage comptage secondes
  122. display.setCursor(116, 56);
  123. display.print(int((elapsed_time4min - start_time4min) / 1000));
  124.  
  125. // affichage vitesse moyenne calculée en tours/minute
  126. display.setTextSize(3);
  127. display.setCursor(32, 20);
  128. display.print(nCoeff * NbRPM);
  129.  
  130. display.display();
  131.  
  132. elapsed_time4min = elapsed_time4sec = millis();
  133. elapsed_time = elapsed_time4sec - start_time4sec;
  134. // comptage nombre de tours sur une seconde extrapolés à la minute
  135. if (elapsed_time > 1000) {
  136. NbRPM = Median_filter(NbRPSec * 1000.0 * 60 / elapsed_time);
  137. start_time4sec = elapsed_time4sec;
  138. NbRPSec = 0;
  139. }
  140. // remise à zéro du comptage sur la minute
  141. if ((elapsed_time4min - start_time4min) > 60000) {
  142. start_time4min = elapsed_time4min;
  143. //Serial.println(NbRPMin);
  144. prevNbRPMin = NbRPMin;
  145. NbRPMin = 0;
  146. }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement