Advertisement
learnelectronics

Arduino Tachometer Using Photo-interrupter

Oct 17th, 2017
3,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. /*
  2.  * Arduino Tachometer Using Photo-interrupter
  3.  *
  4.  * learnelectronics
  5.  * 16 OCT 2013
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  * arduino0169@gmail.com
  9.  */
  10.  
  11. #include <Wire.h>                                                     //Wire library for I2c display
  12. #include <Adafruit_SSD1306.h>                                         //Adafruit OLED display library
  13.  
  14. #define OLED_RESET 4                                                  //does nothing, but needed for library
  15.  
  16. Adafruit_SSD1306 display(OLED_RESET);                                 //create instance of SSD1306 library called display
  17.  
  18.  
  19. int val;                                                              //calibration value
  20. long last=0;                                                          //used in timing - holds thelast milis count
  21. int stat=LOW;                                                         //used in calibration
  22. int stat2;                                                            //used in calibration
  23. int counter=0;                                                        //used to count light/dark iterations
  24.  
  25. int sens=75;                                                          //calibration value
  26.              
  27.              
  28. int slots=20;                                                         //# slots in disc
  29.  
  30. int milisecs=500;                                                     //time of sample
  31.  
  32.  
  33. void setup()
  34. {
  35.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                          //begin oled
  36.   display.display();                                                  //show bufer
  37.   delay(10);                                                          //wait 10 mS
  38.   display.clearDisplay();                                             //clear display and buffer
  39.  
  40.   Serial.begin(9600);                                                 //serial comms for debuging
  41.   pinMode(13,OUTPUT);                                                 //digital pin 13 set for output
  42. }
  43.  
  44. void loop()
  45. {
  46.   val=analogRead(0);                                                  //light or dark?
  47.   if(val<sens)                                                        //within calibartion value?
  48.     stat=LOW;                                                         //set var to LOW
  49.    else                                                               //if not
  50.     stat=HIGH;                                                        //set var HIGH
  51.    digitalWrite(13,stat);                                             //LED on or off
  52.                          
  53.  
  54.    if(stat2!=stat){                                                   //if there has been a change
  55.                      
  56.      counter++;                                                       //increment counter
  57.      stat2=stat;                                                      //remember last value
  58.    }
  59.    if(millis()-last>=milisecs){                                       //has time passed?
  60.      double rps=((double)counter/slots)/2.0*1000.0/milisecs;          //do maths
  61.      double rpm=((double)counter/slots)/2.0*60000.0/(milisecs);       //do more maths
  62.      display.clearDisplay();                                          //clear display & buffer
  63.      display.setTextSize(2);                                          //prepare to show stuff
  64.      display.setTextColor(WHITE);
  65.      display.setCursor(0,0);
  66.      //display.print((counter/2.0));
  67.      //display.println("  RPS ");
  68.      //display.print(rps);
  69.      display.print("RPM ");                                           //print out the units we are using to buffer
  70.      display.println(rpm);                                            //print out the value returned by the maths
  71.      display.display();                                               //show me the buffer!
  72.      
  73.      counter=0;                                                       //reset counter
  74.      last=millis();                                                   //remember this moment
  75.  
  76.                                                                       //the time is gone, the song is over
  77.                                                                       //I thought I'd something more to say...
  78.    }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement