Advertisement
learnelectronics

Arduino Macro Frequency Counter

Aug 29th, 2017
21,496
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 1 0
  1. /*
  2.  * Arduino Macro Frequency Counter
  3.  *
  4.  * learnelectronics
  5.  * 27 AUG 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  * arduino0169@gmail.com
  9.  *
  10.  * NOTES: Signal must be logic level
  11.  *        Input pin is D5
  12.  *        No analogWrite() on 3, 9, 10, 11
  13.  */
  14.  
  15. #include <Wire.h>                                     //I2C library for the OLED
  16. #include <Adafruit_SSD1306.h>                         //OLED driver
  17. #include <FreqCount.h>                                //FreqCount library for you know...counting frequencies
  18.                                                       //find it here: https://github.com/PaulStoffregen/FreqCount
  19.  
  20. #define OLED_RESET 4                                  //it's a thing
  21.  
  22.  
  23.  
  24.  
  25. Adafruit_SSD1306 display(OLED_RESET);                 //create instance of the SSD1306 called display
  26.  
  27.  
  28.  
  29. void setup()   {                
  30.  
  31.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);          //begin the OLED @ hex addy )x3C
  32.   display.display();                                  //show the buffer
  33.   delay(2000);                                        //bask in the glory of LadyAda
  34.   display.clearDisplay();                             //enough basking
  35.   FreqCount.begin(1000);                              //start counting 1,2,3,4...
  36.  
  37. }
  38.  
  39.  
  40. void loop() {
  41.  
  42.  
  43.   if (FreqCount.available()) {                        //if the code if working
  44.     float count = FreqCount.read();                   //create float var called count and populate it with current frequency count
  45.     float period = (1/count);                         //create float var called period and populate it with the inverse of the frequency
  46.   display.clearDisplay();                             //always clear the display first
  47.   display.setTextSize(1);                             //smallest text size
  48.   display.setTextColor(WHITE);                        //my only choice, really
  49.   display.setCursor(0,0);                             //cursor to upper left
  50.   display.println("Arduino Freq. Counter");           //print heading to buffer
  51.   display.println("------- ----- -------");           //print some pretty line to buffer
  52.   display.println("");                                //skip a line
  53.   display.print("Freq:   ");                          //print the name of the function to buffer
  54.   display.print(count);                               //print the actual counted frequency to buffer
  55.   display.println("Hz");                              //print units to buffer & drop down 1 line
  56.   display.print("Period: ");                          //print the name of the fuction to buffer
  57.   display.print(period*1000);                         //print the period of signal in milliseconds to buffer
  58.   display.print("mS");                                //print the units to buffer
  59.   display.display();                                  //SHOW ME THE BUFFER!!!!
  60.   }
  61.  
  62.  
  63.  
  64. }                                                     //That is all, carry on.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement