Advertisement
learnelectronics

Spinner RPM modified by ElectronicsSavage

Jul 16th, 2017
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.12 KB | None | 0 0
  1. /*
  2.  * Fidget Spinner Tachometer
  3.  *
  4.  * learnelectronics
  5.  * 11 JUL 2017
  6.  * ElectronicSavage
  7.  * 14 JUL 2017
  8.  *
  9.  * www.youtube.com/c/learnelectronics
  10.  */
  11.  
  12. #include <Wire.h>                                                 //I2C library for OLED
  13. #include <Adafruit_SSD1306.h>                                     //driver for OLED
  14.  
  15. //constants
  16. const int displayInterval = 300;
  17. const int8_t OLED_RESET = 4;                                      //OLED reset on pin 4
  18.  
  19. const int hallSensorPin = 2;                                      //connect the hall effect sensor on pin 2
  20. const unsigned long sampleTime = 1000;                            //sample time 1000mS or 1S
  21. const unsigned long maxIntervalTime = 1000000;
  22.  
  23. //variables
  24. Adafruit_SSD1306 display(OLED_RESET);                             //create instance of Adafruit_SSD1306 called display
  25. unsigned long lastDisplay = 0;
  26.  
  27. float maxRPM = 0;                                                 //var to store maximum rpms
  28. float smoothedRPM = 0;                                            //storing smoothed value of rpms here
  29.  
  30. //prototypes
  31. float getRPM();
  32. void displayLoop();
  33.  
  34. void setup() {
  35.     pinMode(hallSensorPin, INPUT);                                  //set digital pin to as input
  36.  
  37.     display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                      //start the OLED @ hex address 0x3c
  38.     display.clearDisplay();                                         //clear buffer
  39.  
  40.     display.setTextSize(1);                                         //display params
  41.     display.setTextColor(WHITE);                                    //display params
  42.     display.setCursor(0, 0);                                        //display params
  43.     display.print("Initializing");                                  //send message to buffer
  44.     display.display();                                              //send data in buffer to screen
  45.     delay(1000);                                                    //wait
  46.     display.clearDisplay();                                         //clear buffer
  47.     display.display();                                              //send data in buffer to screen
  48. }
  49.  
  50. void loop() {
  51.     float rpm = getRPM();                                           //getting rpm
  52.     if (rpm > maxRPM) {                                             //check for new max rpms
  53.         maxRPM = rpm;                                               //setting max
  54.     }
  55.  
  56.     smoothedRPM = 0.1 * rpm + 0.9 * smoothedRPM;                    //equivalent of averaging over 10 values
  57.     displayLoop();                                                  //call displayloop
  58. }
  59.  
  60. float getRPM()                                                      //function to get rpms
  61. {
  62.     unsigned long startTime = micros();                             //set startTime to current micros
  63.  
  64.     unsigned long pulseOne = 0;                                     //variables for pulse timing
  65.     unsigned long pulseTwo = 0;
  66.     boolean waitingForLow = false;
  67.     while (micros() - startTime < maxIntervalTime) {                //while we aren't running into timeout
  68.         if (digitalRead(hallSensorPin) == HIGH)                     //if high (1/ON)
  69.         {
  70.             if(!waitingForLow){
  71.                 waitingForLow = true;
  72.                 if (pulseOne != 0) {                                    //if pulse one is set
  73.                     pulseTwo = micros();                                //set pulse two
  74.                     break;                                              //and exit loop
  75.                 }                                                       //else
  76.                 pulseOne = micros();                                    // set pulse one
  77.             }
  78.         }else{
  79.             waitingForLow = false;
  80.         }
  81.     }
  82.  
  83.     if (pulseOne == 0 || pulseTwo == 0) {                           //checking if we ran into a timeout
  84.         return 0;                                                   //return 0 if we didn't get a pulse in time
  85.     }
  86.  
  87.     float RPS = 1000000.0 / (pulseTwo - pulseOne);                    //rotations per second. 1 million divided by the time between pulses
  88.  
  89.     return RPS * 60;                                                //return rotations per minute
  90. }
  91.  
  92. uint8_t getDigitCount(int number) {                   //function to count the digits a number will use when printed
  93.     uint8_t cnt = 0;                                  //count variable
  94.     while (number > 0) {                              //loop while number is > 0
  95.         cnt++;                                        //increment count
  96.         number /= 10;                                 //divide by ten
  97.     }
  98.     return cnt;                                       //return count
  99. }
  100.  
  101. template<typename T> void printAlignment(T number, uint8_t space = 6) {   //this is a fancy template, it means the T can be replaced by different types
  102.     int empty = space - getDigitCount((int) number);                      //count of empty spaces to leave
  103.     for (int i = 0; i < empty; i++) {
  104.         display.print(" ");                                               //print empty spaces
  105.     }
  106. }
  107.  
  108. void displayLoop() {                                                //displayloop function
  109.     if (millis() - lastDisplay > displayInterval) {
  110.         lastDisplay = millis();
  111.         display.clearDisplay();                                     //clearing display
  112.         display.setCursor(0, 0);                                    //setting cursor to top left
  113.         display.print("RPM: ");                                     //RPM label
  114.         printAlignment(smoothedRPM);                                //print alignment
  115.         display.println(smoothedRPM, 2);                            //print rpm and move to next line
  116.         display.print("Max RPM: ");                                 //Max RPM label
  117.         printAlignment(maxRPM);                                     //print alignment
  118.         display.print(maxRPM, DEC, 2);                                 //send maxrpm data in decimal to buffer
  119.         display.display();                                          //show me the buffer!
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement