Arduino stränginstrument (6 / 6 steg)
Steg 6: Lägga det alldeles
Här är koden för ett två högtalare instrument:#include < Tone.h >
int potValue;
int potValue2;
Tonar högtalare [2].
int noterar [] = {
NOTE_A3,
NOTE_B3,
NOTE_C4,
NOTE_D4,
NOTE_E4,
NOTE_F4,
NOTE_G4,
NOTE_A4,
NOTE_B4,
NOTE_C5,
NOTE_D5,
NOTE_E5,
NOTE_F5,
NOTE_G5,
NOTE_A5,
NOTE_B5,
NOTE_C6};
void setup() {
initiera seriell kommunikation på 9600 bps:
Serial.BEGIN(9600);
Speaker[0].BEGIN(7);
Speaker[1].BEGIN(8);
}
void loop() {
potValue = analogRead(A0); läsa värdet pot
Serial.println(potValue);
om (potValue < 100) {
Speaker[0].Play(Notes[0]);
Speaker[1].Play(Notes[12]);
}
annars om (potValue < 200) {
Speaker[0].Play(Notes[1]);
Speaker[1].Play(Notes[11]);
}
annars om (potValue < 250) {
Speaker[0].Play(Notes[2]);
Speaker[1].Play(Notes[10]);
}
annars om (potValue < 320) {
Speaker[0].Play(Notes[3]);
Speaker[1].Play(Notes[9]);
}
annars om (potValue < 400) {
Speaker[0].Play(Notes[4]);
Speaker[1].Play(Notes[8]);
}
annars om (potValue < 450) {
Speaker[0].Play(Notes[5]);
Speaker[1].Play(Notes[7]);
}
annars om (potValue < 500) {
Speaker[0].Play(Notes[6]);
Speaker[1].Play(Notes[4]);
}
annars om (potValue < 550) {
Speaker[0].Play(Notes[7]);
Speaker[1].Play(Notes[3]);
}
annars om (potValue < 700) {
Speaker[0].Play(Notes[8]);
Speaker[1].Play(Notes[2]);
}
annars om (potValue < 900) {
Speaker[0].Play(Notes[9]);
Speaker[1].Play(Notes[1]);
}
annat {
Speaker[0].stop();
Speaker[1].stop();
}