Skip to content

Instantly share code, notes, and snippets.

@aaronsherwood
Created September 30, 2012 22:23
Show Gist options
  • Save aaronsherwood/3808608 to your computer and use it in GitHub Desktop.
Save aaronsherwood/3808608 to your computer and use it in GitHub Desktop.
contact mic bleep sketch for arduino's tone function
const int sensorPin = A0;
int sensorValue = 0;
int fundamental = 55;
int psv, frequency;
boolean ready = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
if(sensorValue>1){
Serial.println(sensorValue);
}
//check to see if ready for another note...
if(sensorValue<psv-10 || sensorValue<3 ){
ready=true;
}
//if so, rock it.
else if (ready){
ready=false;
tone(8, frequency,75);
}
//changing octaves, this is rather arbitrary
if(sensorValue<psv-10){
frequency=fundamental; //back to root octave
}
if(sensorValue>psv+4){
frequency*=2; //doubling octaves, do it again...
frequency=constrain(frequency,fundamental, fundamental*64); //don't let it get too high
}
//setting previous sensor value
psv=sensorValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment