Advertisement
pippero

suoni

Mar 6th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include "pitches.h"
  2. byte a = 0;
  3. byte a1 = 0;
  4. int noteDuration;
  5.  
  6. // notes in the melody:
  7. int melody[] = {
  8. NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3
  9. };
  10. int melody1[] = {
  11. NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5
  12. };
  13. int noteDurations[] = {
  14. 4, 8, 8, 4
  15. };
  16. int noteDurations1[] = {
  17. 4, 1, 1, 4
  18. };
  19. unsigned long previousMillis = 0;
  20. unsigned long pauseBetweenNotes;
  21. int thisNote = 10;
  22.  
  23. void setup() {
  24. Serial.begin(9600);
  25. }
  26.  
  27. void loop() {
  28. if (Serial.available() > 0) {
  29. char state = Serial.read();
  30. if (state == '+' ) {
  31. Serial.print("+ ");
  32. a = 2;
  33. }
  34. if (state == '-' ) {
  35. Serial.print("- ");
  36. a = 1;
  37. }
  38. }
  39. unsigned long currentMillis = millis();
  40. if (a != 0 && a != a1) {
  41. thisNote = 0;
  42. Serial.println(" cambio ");
  43. }
  44. a1 = a;
  45. if (thisNote < 4 && currentMillis - previousMillis >= pauseBetweenNotes) {
  46. Serial.println(" Note ");
  47. previousMillis = currentMillis;
  48. if (a == 1) {
  49. noteDuration = 1000 / noteDurations[thisNote];
  50. tone(8, melody1[thisNote], noteDuration);
  51. }
  52. if (a == 2) {
  53. noteDuration = 1000 / noteDurations1[thisNote];
  54. tone(8, melody[thisNote], noteDuration);
  55. }
  56. pauseBetweenNotes = noteDuration * 1.30;
  57. thisNote++;
  58.  
  59. }
  60. if (thisNote >= 4 ) {
  61. a = 0;
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement