Synth program


This is another cool program I whipped up the other day when I got my etherten. I know I could save program space by doing fancy loops and stuff but I tend to stick to KISS. To run the program hook a piezo to pin 9 and ground. Then open the serial monitor and try typing in 3 or 4. This should change the sound the piezo makes

Here you go:

int incomingByte;
int note = 100;
int beat = 50;
int length = 10;
int pin = 9;

void setup() {
 // put your setup code here, to run once:
Serial.begin(9600);
 // initialize the LED pin as an output:
 pinMode(pin, OUTPUT);
}

void loop() {
 tone (pin,note);
 delay(length);
 noTone (pin);
 delay(beat);
 if (Serial.available() > 0) {
 // read the oldest byte in the serial buffer:
 incomingByte = Serial.read();
 // if it's a capital H (ASCII 72), turn on the LED:
 if (incomingByte == 'q') {
 note = note + 10;
 Serial.println (note, DEC);
 } 
 if (incomingByte == 'a') {
 note = note - 10;
 Serial.println (note, DEC);
 } 
 if (incomingByte == 'w') {
 length = length + 10;
 Serial.println (length, DEC);
 } 
 if (incomingByte == 's') {
 length = length - 10;
 Serial.println (length, DEC);
 }
 if (incomingByte == 'e') {
 beat = beat + 10;
 Serial.println (beat, DEC);
 } 
 if (incomingByte == '1') {
 beat = 856;
 length = 45;
 }
 if (incomingByte == '2') {
 beat = 50;
 length = 600;
 }
 if (incomingByte == '3') {
 beat = 10;
 length = 120;
 }
 if (incomingByte == '4') {
 beat = 15;
 length = 100;
 }
 if (incomingByte == '5') {
 beat = 30;
 length = 70;
 }
 if (incomingByte == '0') {
 beat = 0;
 length = 0;
 note = 0;
 Serial.println("synth has been turned off");
 }
 if (incomingByte == '9') {
 beat = 10;
 length = 10;
 note = 10;
 Serial.println("synth has been turned on");

 }
 }
 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s