Étape 4: Code
Si vous ouvrez votre Arduino sur votre ordinateur et démarrer un nouveau fichier et copier coller le code sous :
#include « pitches.h »
const int buttonPin = 2 ; bool volatils aan = false ;
const int aantalNoten = 8 ;
la mélodie d’int [] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4} ;
int noteDurations [] = {4, 8, 8, 4, 4, 4, 4, 4} ;
void setup() {pinMode(buttonPin, INPUT) ; attachInterrupt(digitalPinToInterrupt(buttonPin), playpause, RISING);}
void loop() {si (aan == true) {pour (int thisNote = 0; thisNote < aantalNoten ; thisNote ++) {}
int noteDuration = 1000 / noteDurations [thisNote] ; ton (8, mélodie [thisNote], noteDuration) ;
int pauseBetweenNotes = noteDuration * 1.30 ; Delay(pauseBetweenNotes) ; noTone(8) ;
Si (aan == false) break ; } } }
void playpause() {delay(15) ; if (digitalRead(buttonPin) == HIGH) {aan =! aan;}}