Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // REQUIRES the following Arduino libraries:
- // - Adafruit GFX graphics core lib: https://github.com/adafruit/Adafruit-GFX-Library
- // - Adafruit OLED mono displays lib: libray https://github.com/adafruit/Adafruit_SSD1306
- // - SoftwareSerial : bluetooth
- /* tutos et références
- https://www.e-techno-tutos.com/2021/03/10/bluetooth-hc-05/
- https://www.youtube.com/watch?v=eDKbRlADvA0
- https://www.carnetdumaker.net/articles/faire-un-tachymetre-avec-une-carte-arduino-genuino-pour-lire-la-vitesse-de-rotation-dun-ventilateur-de-pc/
- */
- #include "time.h"
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <SoftwareSerial.h>
- #include <Wire.h>
- #define BUTTONPIN 2 // Digital IO pin connected to the button.
- // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
- // The pins for I2C are defined by the Wire-library.
- // On an arduino UNO or Nano: A4(SDA), A5(SCL)
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- #define SCREEN_ADDRESS 0x3C //< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // Mapping Arduino Digital ports to Bluetooth Serial
- #define BT_TX 10
- #define BT_RX 11
- #define DEBOUNCE 3 // debounce latency in ms
- // const int IR_distance = A0; // analog input
- #define IR_detection_pin 3 // digital input 12
- unsigned long elapsed_time, elapsed_time4min, elapsed_time4sec, start_time4min, start_time4sec;
- volatile int NbRPMin = 0;
- volatile int NbRPSec = 0;
- int NbRPM = 0;
- int prevNbRPMin = 0;
- int nCoeff = 1;
- volatile unsigned long IR_time;
- volatile unsigned long IR_last_time = 0;
- volatile unsigned long periode = 0; // comptage retour info ventilo
- word Median_filter(word t); // filtre lissage sur 5 mesures comptage-tours
- // SoftwareSerial BT_Serial(BT_TX, BT_RX); // declaring second serial port.
- void setup() {
- // Serial.begin(115200);
- // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Don't proceed, loop forever
- }
- // Show initial display buffer contents on the screen --
- // the library initializes this with an Adafruit splash screen.
- display.display();
- delay(1000);
- // initialize the pushbutton pin as input:
- //pinMode(BUTTONPIN, INPUT);
- //pinMode(BUTTONPIN, INPUT_PULLUP);
- // Uno and Nano : interruptions only on D2 and D3
- // attachInterrupt(digitalPinToInterrupt(BUTTONPIN), onButtonD2Event, RISING);
- // initialize IR pins
- // pinMode(IR_detection_pin, INPUT);
- pinMode(IR_detection_pin, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(IR_detection_pin), onInfraRedD3Event, RISING);
- start_time4min = start_time4sec = millis();
- }
- //void onButtonD2Event() {
- // nCoeff = nCoeff < 6 ? ++nCoeff : 1;
- //}
- void onInfraRedD3Event() {
- //IR_not_detected = digitalRead( IR_detection_pin);
- // 1 no detection
- // 0 detection
- // Serial.println(IR_not_detected);
- IR_time = millis();
- if (IR_time - IR_last_time > DEBOUNCE) // temporisation x millisecondes pour éviter de compter trop vite :-)
- {
- ++NbRPMin;
- ++NbRPSec;
- IR_last_time = IR_time;
- }
- }
- void loop(){
- display.cp437(true); // Use full 256 char 'Code Page 437' font
- display.clearDisplay(); // Clear the buffer
- display.setTextColor(SSD1306_WHITE); // Draw white text
- display.setTextSize(1); // Normal 1:1 pixel scale
- display.setCursor(0,0); // Start at left-top corner
- display.println(F("Revolutions / minute"));
- // affichage compptage nombre de tours comptés pendant cette minute
- display.setCursor(0, 56);
- //display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
- display.print(nCoeff * NbRPMin);
- // affichage compptage nombre de tours comptés minute précédente
- //display.setCursor(64, 56);
- //display.print(F("Coeff * "));
- //display.print(nCoeff);
- display.setCursor(50, 56);
- display.print(nCoeff * prevNbRPMin);
- // affichage comptage secondes
- display.setCursor(116, 56);
- display.print(int((elapsed_time4min - start_time4min) / 1000));
- // affichage vitesse moyenne calculée en tours/minute
- display.setTextSize(3);
- display.setCursor(32, 20);
- display.print(nCoeff * NbRPM);
- display.display();
- elapsed_time4min = elapsed_time4sec = millis();
- elapsed_time = elapsed_time4sec - start_time4sec;
- // comptage nombre de tours sur une seconde extrapolés à la minute
- if (elapsed_time > 1000) {
- NbRPM = Median_filter(NbRPSec * 1000.0 * 60 / elapsed_time);
- start_time4sec = elapsed_time4sec;
- NbRPSec = 0;
- }
- // remise à zéro du comptage sur la minute
- if ((elapsed_time4min - start_time4min) > 60000) {
- start_time4min = elapsed_time4min;
- //Serial.println(NbRPMin);
- prevNbRPMin = NbRPMin;
- NbRPMin = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement