Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Fidget Spinner Tachometer
- *
- * learnelectronics
- * 11 JUL 2017
- *
- * www.youtube.com/c/learnelectronics
- * arduino0169@gmail.com
- */
- #include <Wire.h> //I2C library for OLED
- #include <Adafruit_SSD1306.h> //driver for OLED
- #define OLED_RESET 4 //OLED reset on pin 4
- Adafruit_SSD1306 display(OLED_RESET); //create instance of Adafruit_SSD1306 called display
- const int hallSensorPin = 2; // connect the hall effect sensor on pin 2
- const unsigned long sampleTime = 1000; //sample time 1000mS or 1S
- const int maxRPM = 1500; //just a guess
- int rpmMaximum = 0; //var to store new maximum rpms
- void setup()
- {
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //start the OLED @ hex arrdess ox3c
- display.display(); //send data in bufer to screen
- display.clearDisplay(); //clear buffer and display
- pinMode(hallSensorPin,INPUT); //set digital pin to as input
- display.setTextSize(1); //display params
- display.setTextColor(WHITE); //display params
- display.setCursor(0,0); //display params
- display.print("Initializing"); //send message to buffer
- delay(1000); //wait
- display.display(); //send data in bufer to screen
- display.clearDisplay(); //clear buffer and display
- }
- void loop()
- {
- delay(100); //wait 1/10 of a second
- int rpm = getRPM(); //int var called rpm equal to val returned by func getRPM
- if (rpm > rpmMaximum) rpmMaximum = rpm; //check for new max rpms
- displayRPM(rpm); //call displayrpm func
- }
- int getRPM() //function to get rpms
- {
- int count = 0; //set int var count to 0
- boolean countFlag = LOW; //set count flag to 0 (off/false)
- unsigned long currentTime = 0; //set current time to 0
- unsigned long startTime = millis(); //set startTime to current millis
- while (currentTime <= sampleTime) //as long as currenttime is lessthan sampletime
- { //check hal sensor
- if (digitalRead(hallSensorPin) == HIGH) //if high (1/ON)
- {
- countFlag = HIGH; //set or keep countflag high
- }
- if (digitalRead(hallSensorPin) == LOW && countFlag == HIGH) //but if it's low (hall triggered)
- {
- count++; //increment count by 1
- countFlag=LOW; //set flag low
- }
- currentTime = millis() - startTime; //new current time is current millis - starttime
- }
- int countRpm = int(60000/float(sampleTime))*count; //do da maths
- return countRpm; //return the rpms
- }
- void displayRPM(int rpm) //displayrpm function
- {
- display.clearDisplay(); //display params
- display.setCursor(0,0); //display params
- display.print("RPM: "); //send title info to buffer
- display.println(rpm,DEC); //send rpm data in decimal format to buffer
- display.print("Max RPM: "); //send title to buffer
- display.print(rpmMaximum, DEC); //send maxrpm data in decimal to buffer
- display.display(); //show me the buffer!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement