Advertisement
learnelectronics

Arduino Macro Frequency Counter

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