Stap 6: Part 2: stap 1: de Code
Allereerst laten we de code die u invoeren in uw computer moet om uw piano werken. De code die u moet invoeren in de Arduino app is hieronder voor u te gebruiken.
int button_C = 2;
int button_D = 3; int button_E = 4; int button_F = 5; int button_G = 6; int button_A = 7; int button_B = 8; int button_Cup = 9;
int spreker = 13;
int buttonstate_C = 0; int buttonstate_D = 0; int buttonstate_E = 0; int buttonstate_F = 0; int buttonstate_G = 0; int buttonstate_A = 0; int buttonstate_B = 0; int buttonstate_Cup = 0;
NOTITIES 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' int tonen [] = {1915 1700, 1519, 1432, 1275, 1136, 1014, 956}; freq int Cur_tone = 0;
void setup {pinMode (button_C, INPUT); pinMode (button_D, INPUT); pinMode (button_E, INPUT); pinMode (button_F, INPUT); pinMode (button_G, INPUT); pinMode (button_A, INPUT); pinMode (button_B, INPUT); pinMode (button_Cup, INPUT);
pinMode (spreker, OUTPUT); }
void loop {buttonstate_C = digitalRead(button_C); buttonstate_D = digitalRead(button_D); buttonstate_E = digitalRead(button_E); buttonstate_F = digitalRead(button_F); buttonstate_G = digitalRead(button_G); buttonstate_A = digitalRead(button_A); buttonstate_B = digitalRead(button_B); buttonstate_Cup = digitalRead(button_Cup);
Als ((buttonstate_C == HIGH) || (buttonstate_E == HIGH) || (buttonstate_G == HIGH) || (buttonstate_D == HIGH) || (buttonstate_F == HIGH) || (buttonstate_A == HIGH) || (buttonstate_B == HIGH) || (buttonstate_Cup == High)) {Als (buttonstate_C == HIGH) {Cur_tone = tonen [0];} als (buttonstate_E == HIGH) {Cur_tone = tonen [1];} als (buttonstate_G == HIGH) {Cur_tone = tonen [2];} als (buttonstate_D == HIGH) {Cur_tone = tonen [3];} als (buttonstate_F == HIGH) {Cur_tone = tonen [4];} als (buttonstate_A == HIGH) {Cur_tone = tonen [5];} als (buttonstate_B == HIGH) {Cur_tone = tonen [6];} als (buttonstate_Cup == HIGH) {Cur_tone = tonen [7];}
digitalWrite (spreker, hoge); delayMicroseconds(Cur_tone); digitalWrite (spreker, laag); delayMicroseconds(Cur_tone); } else //in geval geen knop is ingedrukt, sluit de piezo {digitalWrite (spreker, laag);}
}