Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Use FreqMeasure from here: https://github.com/AndreyMitsyk/FreqMeasure
#include <FreqMeasure.h>
// System variables
int buttonState = 0;
int frequency = 0;
double sum = 0;
int count = 0;
long buttonTimer = 0;
boolean buttonActive = false;
boolean longPressActive = false;
// Initial sound state
bool soundSwitch = true;
// Initial calibration
int balance = 9243;
// Sensetive
int sens = 2;
// Button long press time
long longPressTime = 1000;
// 8 - input generator pin
const int soundPin = 7;
const int ledPin = 4;
const int pushButton = 10;
void setup() {
// Button
pinMode(pushButton, INPUT);
// Buzer
pinMode(soundPin, OUTPUT);
// Led
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
FreqMeasure.begin();
}
void loop() {
if (FreqMeasure.available()) {
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
frequency = FreqMeasure.countToFrequency(sum / count);
Serial.println(frequency);
sum = 0;
count = 0;
}
}
buttonState = digitalRead(pushButton);
if (buttonState == HIGH) {
if (!buttonActive) {
buttonActive = true;
buttonTimer = millis();
}
if ((millis() - buttonTimer > longPressTime) && !longPressActive) {
longPressActive = true;
soundSwitch = !soundSwitch;
}
balance = frequency;
Serial.println("button");
} else {
if (buttonActive) {
if (longPressActive) {
longPressActive = false;
}
buttonActive = false;
}
}
if (abs(frequency - balance) <= sens) {
noTone(soundPin);
digitalWrite(ledPin, false);
}
else if (frequency > balance) {
if (soundSwitch) {
tone(soundPin, 900);
}
digitalWrite(ledPin, true);
}
else if (frequency < balance) {
if (soundSwitch) {
tone(soundPin, 200);
}
digitalWrite(ledPin, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.