Advertisement
cisco404

C++ | Tachometer Arduino Using OLED 128x64

Dec 21st, 2020 (edited)
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <Adafruit_SSD1306.h>// You may have to edit library for 128x64, //default is 128 x 32.
  4. #define OLED_WIDTH 128
  5. #define OLED_HEIGHT 64
  6. #define OLED_ADDR   0x3C // A very common address for these displays.
  7.  
  8. // -------------------------------------------
  9. // Tachometer Arduino Using OLED 128x64
  10. // www.ardukode.blogspot.com
  11. // -------------------------------------------
  12.  
  13. Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);
  14. float value=0;
  15. float rev=0;
  16. int rpm;
  17. int oldtime=0;        
  18. int time;
  19.  
  20. void isr(){         //interrupt service routine
  21.   rev++;
  22. }
  23.  
  24. void setup(){
  25.  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  26.  display.clearDisplay();  
  27.  digitalWrite(2 ,HIGH);// Instead of using a pull up resistor //using pin number 2
  28.  attachInterrupt(0,isr,RISING);  //attaching the interrupt
  29. }
  30.  
  31. void loop(){
  32.   delay(2000);// 2 second delay
  33.   detachInterrupt(0);           //detaches the interrupt while calculating
  34.   time=millis()-oldtime;        //finds the time
  35.   rpm=(rev/time)*60000;         //calculates rpm
  36.   oldtime=millis();             //saves the current time
  37.   rev=0;
  38.   display.clearDisplay();
  39.   display.setTextSize(1);
  40.   display.setTextColor(WHITE);
  41.   display.setCursor(10, 0);// Vertical, Horizontal.
  42.   display.println("Digital Tachometer");
  43.   display.setTextSize(2);
  44.   display.setTextColor(WHITE);
  45.   display.setCursor(0, 25);// Vertical, Horizontal.
  46.   display.println(rpm);
  47.   display.setTextSize(2);
  48.   display.setTextColor(WHITE);
  49.   display.setCursor(85,25);
  50.   display.println("RPM");
  51.   display.setTextSize(1);
  52.   display.setTextColor(WHITE);
  53.   display.setCursor(0, 55);// Vertical, Horizontal.
  54.   display.println("ardukode.blogspot.com");
  55.   display.display();
  56.   attachInterrupt(0,isr,RISING);
  57. }
  58. // avrdude -DV -patmega328p -Pnet:192.168.4.1:23 -carduino -b115200 -U flash:w:firmware.hex:I
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement