Last active
October 16, 2019 18:39
-
-
Save AndreyMitsyk/5665c8b741b2f0538cfa1d353921e2f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
bool buttonActive = false; | |
bool longPressActive = false; | |
bool isFirstData = true; | |
// Initial sound state | |
bool soundSwitch = false; | |
// Initial calibration - change it before usage | |
int balance = 7226; | |
// Sensetive | |
int sens = 2; | |
// Button long press time | |
long longPressTime = 1000; | |
// 4 - input generator pin for Arduino Pro micro | |
const int soundPin = 6; | |
const int ledPin = 10; | |
const int pushButton = 9; | |
void setup() { | |
// Button | |
pinMode(pushButton, INPUT); | |
// Buzer | |
pinMode(soundPin, OUTPUT); | |
// Led | |
pinMode(ledPin, OUTPUT); | |
FreqMeasure.begin(); | |
// Delay for stabilization | |
delay(3000); | |
} | |
void loop() { | |
if (FreqMeasure.available()) { | |
sum = sum + FreqMeasure.read(); | |
count = count + 1; | |
if (count > 30) { | |
frequency = FreqMeasure.countToFrequency(sum / count); | |
sum = 0; | |
count = 0; | |
if (isFirstData) { | |
// First balance | |
isFirstData = false; | |
balance = frequency; | |
} | |
} | |
} | |
buttonState = digitalRead(pushButton); | |
if (buttonState == HIGH) { | |
if (!buttonActive) { | |
buttonActive = true; | |
buttonTimer = millis(); | |
} | |
if ((millis() - buttonTimer > longPressTime) && !longPressActive) { | |
longPressActive = true; | |
soundSwitch = !soundSwitch; | |
} | |
balance = frequency; | |
} else { | |
if (buttonActive) { | |
if (longPressActive) { | |
longPressActive = false; | |
} | |
buttonActive = false; | |
} | |
} | |
if (abs(frequency - balance) <= sens) { | |
noTone(soundPin); | |
digitalWrite(ledPin, false); | |
// Auto-balance | |
balance = frequency; | |
} | |
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