Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Arduino Tachometer Using Photo-interrupter
- *
- * learnelectronics
- * 16 OCT 2013
- *
- * www.youtube.com/c/learnelectronics
- * arduino0169@gmail.com
- */
- #include <Wire.h> //Wire library for I2c display
- #include <Adafruit_SSD1306.h> //Adafruit OLED display library
- #define OLED_RESET 4 //does nothing, but needed for library
- Adafruit_SSD1306 display(OLED_RESET); //create instance of SSD1306 library called display
- int val; //calibration value
- long last=0; //used in timing - holds thelast milis count
- int stat=LOW; //used in calibration
- int stat2; //used in calibration
- int counter=0; //used to count light/dark iterations
- int sens=75; //calibration value
- int slots=20; //# slots in disc
- int milisecs=500; //time of sample
- void setup()
- {
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //begin oled
- display.display(); //show bufer
- delay(10); //wait 10 mS
- display.clearDisplay(); //clear display and buffer
- Serial.begin(9600); //serial comms for debuging
- pinMode(13,OUTPUT); //digital pin 13 set for output
- }
- void loop()
- {
- val=analogRead(0); //light or dark?
- if(val<sens) //within calibartion value?
- stat=LOW; //set var to LOW
- else //if not
- stat=HIGH; //set var HIGH
- digitalWrite(13,stat); //LED on or off
- if(stat2!=stat){ //if there has been a change
- counter++; //increment counter
- stat2=stat; //remember last value
- }
- if(millis()-last>=milisecs){ //has time passed?
- double rps=((double)counter/slots)/2.0*1000.0/milisecs; //do maths
- double rpm=((double)counter/slots)/2.0*60000.0/(milisecs); //do more maths
- display.clearDisplay(); //clear display & buffer
- display.setTextSize(2); //prepare to show stuff
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- //display.print((counter/2.0));
- //display.println(" RPS ");
- //display.print(rps);
- display.print("RPM "); //print out the units we are using to buffer
- display.println(rpm); //print out the value returned by the maths
- display.display(); //show me the buffer!
- counter=0; //reset counter
- last=millis(); //remember this moment
- //the time is gone, the song is over
- //I thought I'd something more to say...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement