Line 164: |
Line 164: |
| | | |
| <code style="color: green;"> | | <code style="color: green;"> |
− | <nowiki>
| + | [[Media:Tone_arduino.ino]] |
− | | |
− | #include "pitches.h"
| |
− | #define TONE_PIN A2
| |
− | | |
− | //C major
| |
− | int notes[] = { NOTE_A3, NOTE_B3, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4 };
| |
− | | |
− | //G major
| |
− | //int notes[] = { NOTE_A3, NOTE_B3, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_A4 };
| |
− | | |
− | //D major
| |
− | //int notes[] = { NOTE_A3, NOTE_B3, NOTE_CS4, NOTE_D4, NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_A4 };
| |
− | | |
− | //A major
| |
− | //int notes[] = { NOTE_A3, NOTE_B3, NOTE_CS4, NOTE_D4, NOTE_E4, NOTE_FS4, NOTE_GS4, NOTE_A4 };
| |
− | | |
− | //E major
| |
− | //int notes[] = { NOTE_A3, NOTE_B3, NOTE_CS4, NOTE_DS4, NOTE_E4, NOTE_FS4, NOTE_GS4, NOTE_A4 };
| |
− | | |
− | //B major
| |
− | //int notes[] = { NOTE_AS3, NOTE_B3, NOTE_CS4, NOTE_DS4, NOTE_E4, NOTE_FS4, NOTE_GS4, NOTE_AS4 };
| |
− | | |
− | //A minor
| |
− | //int notes[] = { NOTE_GS3, NOTE_AS3, NOTE_C4, NOTE_CS4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_GS4 };
| |
− | | |
− | //E minor
| |
− | //int notes[] = { NOTE_GS3, NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_GS4 };
| |
− | | |
− | //B minor
| |
− | //int notes[] = { NOTE_A3, NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_A4 };
| |
− | | |
− | | |
− | | |
− | int oldButton = 0;
| |
− | int newButton = 0;
| |
− | | |
− | void setup() {
| |
− | // put your setup code here, to run once:
| |
− | | |
− | }
| |
− | | |
− | void loop() {
| |
− | delay(10);
| |
− |
| |
− | newButton = readButtons();
| |
− |
| |
− | if(oldButton != newButton){
| |
− | oldButton = newButton;
| |
− | | |
− | if(newButton != 0){
| |
− | tone(TONE_PIN, notes[newButton - 1]);
| |
− | }else{
| |
− | noTone(TONE_PIN);
| |
− | }
| |
− |
| |
− | }
| |
− | | |
− | }
| |
− | | |
− | int readButtons() {
| |
− | int button;
| |
− | if (digitalRead(2) == HIGH) { //button 1
| |
− | button = 1;
| |
− | } else if (digitalRead(3) == HIGH) { //button 2
| |
− | button = 2;
| |
− | } else if (digitalRead(4) == HIGH) { //button 3
| |
− | button = 3;
| |
− | } else if (digitalRead(5) == HIGH) { //button 4
| |
− | button = 4;
| |
− | } else if (digitalRead(6) == HIGH) { //button 5
| |
− | button = 5;
| |
− | } else if (digitalRead(7) == HIGH) { //button 6
| |
− | button = 6;
| |
− | } else if (digitalRead(8) == HIGH) { //button 7
| |
− | button = 7;
| |
− | } else if (digitalRead(9) == HIGH) { //button 8
| |
− | button = 8;
| |
− | } else {
| |
− | button = 0;
| |
− | }
| |
− | return button;
| |
− | }
| |
− | </nowiki>
| |
| </code> | | </code> |
| | | |