Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Fidget Spinner Tachometer
- *
- * learnelectronics
- * 11 JUL 2017
- * ElectronicSavage
- * 14 JUL 2017
- *
- * www.youtube.com/c/learnelectronics
- */
- #include <Wire.h> //I2C library for OLED
- #include <Adafruit_SSD1306.h> //driver for OLED
- //constants
- const int displayInterval = 300;
- const int8_t OLED_RESET = 4; //OLED reset on pin 4
- const int hallSensorPin = 2; //connect the hall effect sensor on pin 2
- const unsigned long sampleTime = 1000; //sample time 1000mS or 1S
- const unsigned long maxIntervalTime = 1000000;
- //variables
- Adafruit_SSD1306 display(OLED_RESET); //create instance of Adafruit_SSD1306 called display
- unsigned long lastDisplay = 0;
- float maxRPM = 0; //var to store maximum rpms
- float smoothedRPM = 0; //storing smoothed value of rpms here
- //prototypes
- float getRPM();
- void displayLoop();
- void setup() {
- pinMode(hallSensorPin, INPUT); //set digital pin to as input
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //start the OLED @ hex address 0x3c
- display.clearDisplay(); //clear buffer
- display.setTextSize(1); //display params
- display.setTextColor(WHITE); //display params
- display.setCursor(0, 0); //display params
- display.print("Initializing"); //send message to buffer
- display.display(); //send data in buffer to screen
- delay(1000); //wait
- display.clearDisplay(); //clear buffer
- display.display(); //send data in buffer to screen
- }
- void loop() {
- float rpm = getRPM(); //getting rpm
- if (rpm > maxRPM) { //check for new max rpms
- maxRPM = rpm; //setting max
- }
- smoothedRPM = 0.1 * rpm + 0.9 * smoothedRPM; //equivalent of averaging over 10 values
- displayLoop(); //call displayloop
- }
- float getRPM() //function to get rpms
- {
- unsigned long startTime = micros(); //set startTime to current micros
- unsigned long pulseOne = 0; //variables for pulse timing
- unsigned long pulseTwo = 0;
- boolean waitingForLow = false;
- while (micros() - startTime < maxIntervalTime) { //while we aren't running into timeout
- if (digitalRead(hallSensorPin) == HIGH) //if high (1/ON)
- {
- if(!waitingForLow){
- waitingForLow = true;
- if (pulseOne != 0) { //if pulse one is set
- pulseTwo = micros(); //set pulse two
- break; //and exit loop
- } //else
- pulseOne = micros(); // set pulse one
- }
- }else{
- waitingForLow = false;
- }
- }
- if (pulseOne == 0 || pulseTwo == 0) { //checking if we ran into a timeout
- return 0; //return 0 if we didn't get a pulse in time
- }
- float RPS = 1000000.0 / (pulseTwo - pulseOne); //rotations per second. 1 million divided by the time between pulses
- return RPS * 60; //return rotations per minute
- }
- uint8_t getDigitCount(int number) { //function to count the digits a number will use when printed
- uint8_t cnt = 0; //count variable
- while (number > 0) { //loop while number is > 0
- cnt++; //increment count
- number /= 10; //divide by ten
- }
- return cnt; //return count
- }
- 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
- int empty = space - getDigitCount((int) number); //count of empty spaces to leave
- for (int i = 0; i < empty; i++) {
- display.print(" "); //print empty spaces
- }
- }
- void displayLoop() { //displayloop function
- if (millis() - lastDisplay > displayInterval) {
- lastDisplay = millis();
- display.clearDisplay(); //clearing display
- display.setCursor(0, 0); //setting cursor to top left
- display.print("RPM: "); //RPM label
- printAlignment(smoothedRPM); //print alignment
- display.println(smoothedRPM, 2); //print rpm and move to next line
- display.print("Max RPM: "); //Max RPM label
- printAlignment(maxRPM); //print alignment
- display.print(maxRPM, DEC, 2); //send maxrpm data in decimal to buffer
- display.display(); //show me the buffer!
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement