frere_jacques.ino 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <Automaton.h>
  2. #include "musical_notes.h"
  3. // Playback 'Frere Jacques' with button trigger and speed control
  4. Atm_player player;
  5. Atm_button button;
  6. Atm_analog speed;
  7. int pattern[] = {
  8. _G4, _N04, 0, _A4, _N04, 0, _B4, _N04, 0, _G4, _N04, 0, // Frere Jacques
  9. _G4, _N04, 0, _A4, _N04, 0, _B4, _N04, 0, _G4, _N04, 0,
  10. _B4, _N04, 0, _C5, _N04, 0, _D5, _N04, _N04, // Dormez vous?
  11. _B4, _N04, 0, _C5, _N04, 0, _D5, _N04, _N04,
  12. _D5, _N08, 0, _E5, _N08, 0, _D5, _N08, 0, _C5, _N08, 0, _B4, _N04, 0, _G4, _N04, 0, // Sonnez les matines
  13. _D5, _N08, 0, _E5, _N08, 0, _D5, _N08, 0, _C5, _N08, 0, _B4, _N04, 0, _G4, _N04, 0,
  14. _G4, _N04, 0, _D4, _N04, 0, _G4, _N04, _N04, // Ding dang dong
  15. _G4, _N04, 0, _D4, _N04, 0, _G4, _N04, _N04,
  16. };
  17. void setup() {
  18. player.begin( 19 ) // A passive buzzer or speaker on pin 19
  19. .play( pattern, sizeof( pattern ) )
  20. .repeat( -1 );
  21. button.begin( 2 ) // A button on pin 2 toggles playback on and off
  22. .onPress( player, player.EVT_TOGGLE );
  23. speed.begin( A0 ) // An analog pot on pin A0 controls playback speed
  24. .range( 50, 300 ) // From 50% to 300% of original speed
  25. .onChange( []( int idx, int v, int up ) {
  26. player.speed( v );
  27. });
  28. }
  29. void loop() {
  30. automaton.run();
  31. }