Remove these ads by
Signing UpStep 1: Code
int speaker = 9; // Hook up speaker to digital pin 9
int sw1 = 15; // Switch hooked to analog pin 1
int sw2 = 16; // Switch hooked to analog pin 2
int sw3 = 17; // Switch hooked to analog pin 3
int valve1;
int valve2;
int valve3;
void c () { // Presets the note "c" to be used later
digitalWrite(speaker, HIGH);
delayMicroseconds(1915);
digitalWrite(speaker, LOW);
delayMicroseconds(1905);
}
void d () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1700);
digitalWrite(speaker, LOW);
delayMicroseconds(1690);
}
void e () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1519);
digitalWrite(speaker, LOW);
delayMicroseconds(1509);
}
void f () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1432);
digitalWrite(speaker, LOW);
delayMicroseconds(1422);
}
void g () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1275);
digitalWrite(speaker, LOW);
delayMicroseconds(1265);
}
void a () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1136);
digitalWrite(speaker, LOW);
delayMicroseconds(1126);
}
void b () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1014);
digitalWrite(speaker, LOW);
delayMicroseconds(1004);
}
void C () {
digitalWrite(speaker, HIGH);
delayMicroseconds(956);
digitalWrite(speaker, LOW);
delayMicroseconds(946);
}
void setup() {
pinMode(speaker, OUTPUT);
pinMode(sw1, INPUT);
digitalWrite(sw1, HIGH);
pinMode(sw2, INPUT);
digitalWrite(sw2, HIGH);
pinMode(sw3, INPUT);
digitalWrite(sw3, HIGH);
// Serial.begin(9600);
}
void loop() {
valve1 = digitalRead(sw1); // Reads switch 1
valve2 = digitalRead(sw2); // Reads switch 2
valve3 = digitalRead(sw3); // Reads switch 3
if ((valve1 == LOW) && (valve2 == LOW) && (valve3 == LOW)) { // If all valves are pressed, then
c(); // It plays a "c"
}
else if ((valve1 == LOW) && (valve2 == HIGH) && (valve3 == LOW)) { // If the first and third valves are pressed, then
d(); // It plays a "d"
}
else if ((valve1 == LOW) && (valve2 == LOW) && (valve3 == HIGH)) { // If the first and second valves are pressed, then
e(); // It plays a "e"
}
else if ((valve1 == LOW) && (valve2 == HIGH) && (valve3 == HIGH)) { // If the first valve is pressed, then
f(); // It plays a "f"
}
else if ((valve1 == HIGH) && (valve2 == HIGH) && (valve3 == HIGH)) { // If no valves are pressed, then
g(); // It plays a "g"
}
else if ((valve1 == HIGH) && (valve2 == HIGH) && (valve3 == LOW)) { // If the third valve is pressed, then
a(); // It plays a "a"
}
else if ((valve1 == HIGH) && (valve2 == LOW) && (valve3 == HIGH)) { // If the second valve is pressed, then
b(); // It plays a "b"
}
else if ((valve1 == HIGH) && (valve2 == LOW) && (valve3 == LOW)) { // If the second and third valves are pressed, then
C(); // It plays a "C"
}
else{
delay(1);
}
}










































Visit Our Store »
Go Pro Today »




I took your idea, and instead of buttons i used potentiometers, just to see what would happen, it was kinda cool, you could get and hold various tones, might be fun if some one knew how to hack the code to read the potentiometer values, so it was more then an on or off type thing, any cool idea, i also like the piezo blow hole idea, read hears somewhere.
PEACE~
Hankenstien
rduino.com
How can we do to have more sound from the piezo ?
Have we to put an amplifier or something else ?
Thanks !
Which has some interesting stuff on generating sound or playing it. Also this page: http://www.arduino.cc/en/Tutorial/Melody has code for playing a pre coded series of tones on a piezo speaker/earphone and has the actual tones as variables. That think is a little easier. Like: a = 946; (that's not actually true) and then something like this: play(a) I'm thinking you might be able to simply/optimize/shorten your code to make it a little less complicated.
First, you can define your speaker and switch ports using #define instead of declaring them as integer variables. Declaring them as variables stores the value in the memory register instead of in the program itself, which takes up registers, and requires a lookup every time you want to recall the pin value. By defining them, when you compile, the compiler automatically inserts the values themselves into the program, requiring no lookup, making the whole process faster. It would look like this:
#define speaker 9
#define sw1 15
#define sw2 16
#define sw3 17
Second, the brackets on all of those if() statements are not strictly necessary. To make it cleaner, you can do it without any brackets at all because you're only running one line of code. It would look like this:
if ((valve1 == LOW) && (valve2 == LOW) && (valve3 == LOW)) c();
else if ((valve1 == LOW) && (valve2 == HIGH) && (valve3 == LOW)) d();
else if ((valve1 == LOW) && (valve2 == LOW) && (valve3 == HIGH)) e();
else if ((valve1 == LOW) && (valve2 == HIGH) && (valve3 == HIGH)) f();
else if ((valve1 == HIGH) && (valve2 == HIGH) && (valve3 == HIGH)) g();
else if ((valve1 == HIGH) && (valve2 == HIGH) && (valve3 == LOW)) a();
else if ((valve1 == HIGH) && (valve2 == LOW) && (valve3 == HIGH)) b();
else if ((valve1 == HIGH) && (valve2 == LOW) && (valve3 == LOW)) C();
else delay(1);
This is obviously with the comments removed, but you can see what I mean. This makes absolutely no difference in performance and is just for readability. It's pure individual preference, so if you like it the other way, feel free to completely ignore me. And the integer/define thing isn't really necessary if it works, it just makes it run a tiny bit faster in case you ever want to get more advanced. Do like very much, though! I'd love to see what this is like with an emulated trumpet waveform output.
Keep doing stuff like this, it's awesome!
void a () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1136);
digitalWrite(speaker, LOW);
delayMicroseconds(1126);
}