Skip to content

Instantly share code, notes, and snippets.

@medecau
Created July 24, 2009 09:44
Show Gist options
  • Save medecau/153942 to your computer and use it in GitHub Desktop.
Save medecau/153942 to your computer and use it in GitHub Desktop.
/*
* simple example of a synthesizer
*breadboard schematics: http://www.flickr.com/photos/28914418@N02/3751185789/sizes/o/
*
* rights: http://tinyurl.com/by-sa-3-0
*/
int speakerPin = 9;
int tempo = 0;
int potPin1 = 0;
int potVal1 = 0;
int potPin2 = 1;
int potVal2 = 0;
int potPin3 = 2;
int potVal3 = 0;
void playTone(int tone, int duration) {
if (tone == 0){
duration = -1;
}
if (duration == 0){
duration = -1;
}
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
potVal1 = analogRead(potPin1);
potVal2 = analogRead(potPin2);
potVal3 = analogRead(potPin3);
if (potVal3>1000) {
potVal3=1000;
}
if (potVal2>potVal3) {
potVal2=potVal3;
}
Serial.print("pot1:");
Serial.print(potVal1);
Serial.print(" - pot2:");
Serial.print(potVal2);
Serial.print(" - pot3:");
Serial.println(potVal3);
playTone(potVal1*2, potVal2);
// pause between notes
delay(potVal3-potVal2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment