1#include "U8glib.h"
2#include <FreqCount.h>
3
4const int buzzerPin = 12;
5const int ledPin = 4;
6
7const unsigned int umbralInferior = 500;
8const unsigned int umbralSuperior = 1500;
9U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);
10unsigned long count = 0;
11void setup() {
12 Serial.begin(115200);
13 FreqCount.begin(1000);
14 pinMode(buzzerPin, OUTPUT);
15 pinMode(ledPin, OUTPUT);
16}
17void loop() {
18 if (FreqCount.available()) {
19 count = FreqCount.read();
20 Serial.println(count);
21 u8g.firstPage();
22 do {
23 draw();
24 } while (u8g.nextPage());
25
26 if (count < umbralInferior) {
27buzzer(40);
28 } else {
29 digitalWrite(buzzerPin, LOW);
30 digitalWrite(ledPin, LOW);
31 }
32
33 if (count > umbralSuperior) {
34buzzer(20);
35 } else {
36 digitalWrite(buzzerPin, LOW);
37 digitalWrite(ledPin, LOW);
38 }
39 }
40}
41void draw(void) {
42
43 u8g.setFont(u8g_font_unifont);
44
45 u8g.setPrintPos(0, 20);
46 u8g.print("Freq:");
47 u8g.print(count);
48 u8g.print("Hz");
49
50 u8g.setPrintPos(0, 40);
51 u8g.print("U.Inf:");
52 u8g.print(umbralInferior);
53 u8g.print("Hz");
54 u8g.setPrintPos(0, 60);
55 u8g.print("U.Sup:");
56 u8g.print(umbralSuperior);
57 u8g.print("Hz");
58}
59void buzzer(int t){
60 digitalWrite(ledPin, HIGH);
61 digitalWrite(buzzerPin, HIGH);
62 delay(t);
63 digitalWrite(buzzerPin, LOW);
64 delay(t);
65 digitalWrite(buzzerPin, HIGH);
66 delay(t);
67 digitalWrite(buzzerPin, LOW);
68 delay(t);
69 digitalWrite(ledPin, LOW);
70 }