Created
January 26, 2016 17:30
-
-
Save interstar/b885b4005b92bd70b9bd 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
#include <Bounce.h> | |
int anaPin = 5; // Analogue in pin | |
int button = 2; // button pin | |
int beepPin = 3; // piezo buzzer | |
// edges of our calibration window | |
float aMax; | |
float aMin; | |
float anIn, scaledValue; // actual analogue input value and the scaled value | |
int calibrationMode; // are we in calibration mode or normal? calibrationMode == 1 when in calibration mode | |
Bounce bb = Bounce(button,0.01); // a Bounce object to debounce the push button | |
void setup() { | |
Serial.begin(9600); | |
pinMode(button,INPUT); | |
pinMode(beepPin,OUTPUT); | |
calibrationMode = 1; | |
} | |
void calibrate() { | |
if (anIn != 0) { // don't calibrate when no current running through graphite | |
if (anIn > aMax) { | |
aMax = anIn; | |
} | |
if (anIn < aMin) { | |
aMin = anIn; | |
} | |
/* | |
Serial.print(anIn); | |
Serial.print(", "); | |
Serial.print(aMin); | |
Serial.print(" - "); | |
Serial.println(aMax); | |
*/ | |
noTone(beepPin); | |
} | |
} | |
void oneBeep(int pitch) { | |
tone(beepPin, pitch); | |
delay(100); | |
noTone(beepPin); | |
delay(50); | |
} | |
void doubleBeep(int pitch) { | |
oneBeep(pitch); | |
oneBeep(pitch); | |
oneBeep(pitch); | |
} | |
void loop() { | |
anIn = analogRead(anaPin); | |
// button handler | |
bb.update(); | |
if (bb.risingEdge()) { | |
if (calibrationMode == 0) { | |
aMax=-9999; | |
aMin=9999; | |
doubleBeep(400); | |
calibrationMode = 1; | |
} else { | |
calibrationMode = 0; | |
doubleBeep(200); | |
} | |
} | |
if (calibrationMode) { | |
calibrate(); | |
scaledValue = -1; | |
} else { | |
scaledValue = map(anIn,aMin,aMax,50,1200); | |
if (anIn != 0) { | |
tone(beepPin, scaledValue); | |
Serial.print("["); | |
Serial.print(scaledValue); | |
Serial.println("]"); | |
delay(10); | |
} else { | |
noTone(beepPin); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment