Skip to content

Instantly share code, notes, and snippets.

@AndreyMitsyk
Last active February 8, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreyMitsyk/c1c37a74aa30192e856f2a50f2c3377f to your computer and use it in GitHub Desktop.
Save AndreyMitsyk/c1c37a74aa30192e856f2a50f2c3377f to your computer and use it in GitHub Desktop.
// For arduino pro micro 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 (should be >= 1)
int sens = 2;
// Button long press time
long longPressTime = 1000;
// 4 - input generator pin for Arduino Pro micro, 8 - for other versions
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);
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);
// 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