Advertisement
learnelectronics

Arduino Tachometer Using Photo-interrupter

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