Advertisement
Guest User

Arduino code for 2 LEDs 2 buttons 2 potentiometers & a speaker

a guest
Jun 7th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 27.26 KB | Software | 0 0
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_NeoPixel.h>
  4.  
  5. #define PIN 3 // This is the Arduino Pin controlling the LED pixel.
  6. #define PIN2 5 // This is the Arduino Pin controlling the other LED pixel.
  7. #define POTENTIOMETER_PIN A0
  8. #define POTENTIOMETER_PIN2 A2
  9. #define BUZZER_PIN 10
  10.  
  11.  
  12. //This sketch is to make a potentiometer position determine a color of a LED
  13. // that shows when a button is pressed
  14.  
  15. //If possible, another potentiometer to determine tone
  16.  
  17. #define NUMPIXELS 1
  18. #define BRIGHTNESS 50
  19. Adafruit_NeoPixel pxl = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);
  20. Adafruit_NeoPixel pxl2 = Adafruit_NeoPixel(NUMPIXELS, PIN2, NEO_GRBW + NEO_KHZ800);
  21. #define PUSH_BUTTON   9
  22. #define PUSH_BUTTON2   7
  23.  
  24. #define REST 0
  25. #define NOTE_B0  31
  26. #define NOTE_C1  33
  27. #define NOTE_CS1 35
  28. #define NOTE_D1  37
  29. #define NOTE_DS1 39
  30. #define NOTE_E1  41
  31. #define NOTE_F1  44
  32. #define NOTE_FS1 46
  33. #define NOTE_G1  49
  34. #define NOTE_GS1 52
  35. #define NOTE_A1  55
  36. #define NOTE_AS1 58
  37. #define NOTE_B1  62
  38. #define NOTE_C2  65
  39. #define NOTE_CS2 69
  40. #define NOTE_D2  73
  41. #define NOTE_DS2 78
  42. #define NOTE_E2  82
  43. #define NOTE_F2  87
  44. #define NOTE_FS2 93
  45. #define NOTE_G2  98
  46. #define NOTE_GS2 104
  47. #define NOTE_A2  110
  48. #define NOTE_AS2 117
  49. #define NOTE_B2  123
  50. #define NOTE_C3  131
  51. #define NOTE_CS3 139
  52. #define NOTE_D3  147
  53. #define NOTE_DS3 156
  54. #define NOTE_E3  165
  55. #define NOTE_F3  175
  56. #define NOTE_FS3 185
  57. #define NOTE_G3  196
  58. #define NOTE_GS3 208
  59. #define NOTE_A3  220
  60. #define NOTE_AS3 233
  61. #define NOTE_B3  247
  62. #define NOTE_C4  262
  63. #define NOTE_CS4 277
  64. #define NOTE_D4  294
  65. #define NOTE_DS4 311
  66. #define NOTE_E4  330
  67. #define NOTE_F4  349
  68. #define NOTE_FS4 370
  69. #define NOTE_G4  392
  70. #define NOTE_GS4 415
  71. #define NOTE_A4  440
  72. #define NOTE_AS4 466
  73. #define NOTE_B4  494
  74. #define NOTE_C5  523
  75. #define NOTE_CS5 554
  76. #define NOTE_D5  587
  77. #define NOTE_DS5 622
  78. #define NOTE_E5  659
  79. #define NOTE_F5  698
  80. #define NOTE_FS5 740
  81. #define NOTE_G5  784
  82. #define NOTE_GS5 831
  83. #define NOTE_A5  880
  84. #define NOTE_AS5 932
  85. #define NOTE_B5  988
  86. #define NOTE_C6  1047
  87. #define NOTE_CS6 1109
  88. #define NOTE_D6  1175
  89. #define NOTE_DS6 1245
  90. #define NOTE_E6  1319
  91. #define NOTE_F6  1397
  92. #define NOTE_FS6 1480
  93. #define NOTE_G6  1568
  94. #define NOTE_GS6 1661
  95. #define NOTE_A6  1760
  96. #define NOTE_AS6 1865
  97. #define NOTE_B6  1976
  98. #define NOTE_C7  2093
  99. #define NOTE_CS7 2217
  100. #define NOTE_D7  2349
  101. #define NOTE_DS7 2489
  102. #define NOTE_E7  2637
  103. #define NOTE_F7  2794
  104. #define NOTE_FS7 2960
  105. #define NOTE_G7  3136
  106. #define NOTE_GS7 3322
  107. #define NOTE_A7  3520
  108. #define NOTE_AS7 3729
  109. #define NOTE_B7  3951
  110. #define NOTE_C8  4186
  111. #define NOTE_CS8 4435
  112. #define NOTE_D8  4699
  113. #define NOTE_DS8 4978
  114.  
  115. //#define LED           4
  116. int PXL1[] = {0};
  117. int PXL2[] = {0};
  118.  
  119.  
  120.  
  121. bool BUTTON_PRESSED = false;
  122. bool BUTTON_PRESSED2 = false;
  123.  
  124. int delayval = 50;
  125.  
  126. int melody[] = {
  127.   NOTE_E3, NOTE_DS3, NOTE_FS3, NOTE_E3, NOTE_G3, NOTE_FS3, NOTE_A3, NOTE_G3, NOTE_AS3, NOTE_B3, NOTE_E4, NOTE_E5, NOTE_DS5, NOTE_D5, NOTE_CS5, REST, NOTE_DS5, NOTE_D5, NOTE_CS5, NOTE_C5, REST, NOTE_FS5, NOTE_FS5, NOTE_E5
  128. };
  129.  
  130. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  131. int noteDurations[] = {
  132.   2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 3, 16, 16, 16, 1, 16, 16, 16, 16, 1, 16, 4, 2, 1
  133. };
  134.  
  135. void setup() {
  136.   // put your setup code here, to run once:
  137.      pinMode(BUZZER_PIN, OUTPUT);
  138.   pinMode(PUSH_BUTTON, INPUT_PULLUP);
  139.     pinMode(PUSH_BUTTON2, INPUT_PULLUP);
  140. //  pinMode(LED, OUTPUT);
  141.   pxl.begin();
  142.   pxl.show();            // Turn OFF all strip ASAP
  143.   pxl.setBrightness(BRIGHTNESS);
  144.  
  145.  
  146.   pxl2.begin();
  147.   pxl2.show();            // Turn OFF all strip ASAP
  148.   pxl2.setBrightness(BRIGHTNESS);
  149.  
  150. }
  151.  
  152. void loop() {
  153.  
  154.  
  155.  
  156. if(!digitalRead(PUSH_BUTTON)){
  157.   BUTTON_PRESSED = true;
  158.   delay(200);
  159.   }else{
  160.    BUTTON_PRESSED = false;
  161.     }
  162.  
  163.  
  164.     int potentiometerValue = analogRead(POTENTIOMETER_PIN); //read the potentiometer
  165.     int percent = map(potentiometerValue, 0, 1023, 0, 100); //map the pot value to percent
  166.  
  167.  if(percent >= 0 && percent <= 8){ //between 0 and 4
  168.   //draw1();
  169.    if(BUTTON_PRESSED){
  170.       //create variables to store color values
  171.       //write a line that
  172.       //digitalWrite(LED, HIGH); //LED ON
  173.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 255));
  174.       pxl.show();
  175.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 255));
  176.       pxl2.show();
  177.     }else{
  178.       //digitalWrite(LED, LOW); //LED OFF  
  179.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  180.       pxl.show();
  181.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  182.       pxl2.show();
  183.  }
  184.  
  185.  }else if(percent >= 9 && percent <= 16){
  186.   //draw2();
  187.  if(BUTTON_PRESSED){
  188.       //create variables to store color values
  189.       //write a line that
  190.       //digitalWrite(LED, HIGH); //LED ON
  191.       pxl.setPixelColor(PXL1[0], pxl.Color(128, 0, 255));
  192.       pxl.show();
  193.       pxl2.setPixelColor(PXL2[0], pxl2.Color(128, 0, 255));
  194.       pxl2.show();
  195.     }else{
  196.       //digitalWrite(LED, LOW); //LED OFF  
  197.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  198.       pxl.show();
  199.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  200.       pxl2.show();
  201.  }
  202.  
  203.   }else if(percent >= 17 && percent <= 24){
  204.      if(BUTTON_PRESSED){
  205.       //create variables to store color values
  206.       //write a line that
  207.       //digitalWrite(LED, HIGH); //LED ON
  208.       pxl.setPixelColor(PXL1[0], pxl.Color(255, 0, 255));
  209.       pxl.show();
  210.       pxl2.setPixelColor(PXL2[0], pxl2.Color(255, 0, 255));
  211.       pxl2.show();
  212.     }else{
  213.       //digitalWrite(LED, LOW); //LED OFF  
  214.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  215.       pxl.show();
  216.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  217.       pxl2.show();
  218.     }
  219.  }else if(percent >= 25 && percent <= 32){
  220.      if(BUTTON_PRESSED){
  221.       //create variables to store color values
  222.       //write a line that
  223.       //digitalWrite(LED, HIGH); //LED ON
  224.       pxl.setPixelColor(PXL1[0], pxl.Color(255, 0, 128));
  225.       pxl.show();
  226.       pxl2.setPixelColor(PXL2[0], pxl2.Color(255, 0, 128));
  227.       pxl2.show();
  228.     }else{
  229.       //digitalWrite(LED, LOW); //LED OFF  
  230.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  231.       pxl.show();
  232.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  233.       pxl2.show();
  234.      
  235.  }
  236.  
  237. }else if(percent >= 33 && percent <= 40){
  238.      if(BUTTON_PRESSED){
  239.       //create variables to store color values
  240.       //write a line that
  241.       //digitalWrite(LED, HIGH); //LED ON
  242.       pxl.setPixelColor(PXL1[0], pxl.Color(255, 0, 0));
  243.       pxl.show();
  244.       pxl2.setPixelColor(PXL2[0], pxl2.Color(255, 0, 0));
  245.       pxl2.show();
  246.     }else{
  247.       //digitalWrite(LED, LOW); //LED OFF  
  248.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  249.       pxl.show();
  250.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  251.       pxl2.show();
  252.      
  253.  }
  254.  
  255.  }else if(percent >= 41 && percent <= 48){
  256.      if(BUTTON_PRESSED){
  257.       //create variables to store color values
  258.       //write a line that
  259.       //digitalWrite(LED, HIGH); //LED ON
  260.       pxl.setPixelColor(PXL1[0], pxl.Color(255, 128, 0));
  261.       pxl.show();
  262.       pxl2.setPixelColor(PXL2[0], pxl2.Color(255, 128, 0));
  263.       pxl2.show();
  264.     }else{
  265.       //digitalWrite(LED, LOW); //LED OFF  
  266.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  267.       pxl.show();
  268.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  269.       pxl2.show();
  270.      
  271.  }
  272.  
  273.  }else if(percent >= 49 && percent <= 56){
  274.      if(BUTTON_PRESSED){
  275.       //create variables to store color values
  276.       //write a line that
  277.       //digitalWrite(LED, HIGH); //LED ON
  278.       pxl.setPixelColor(PXL1[0], pxl.Color(255, 255, 0));
  279.       pxl.show();
  280.       pxl2.setPixelColor(PXL2[0], pxl2.Color(255, 255, 0));
  281.       pxl2.show();
  282.     }else{
  283.       //digitalWrite(LED, LOW); //LED OFF  
  284.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  285.       pxl.show();
  286.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  287.       pxl2.show();
  288.      
  289.  }
  290.  
  291.  }else if(percent >= 57 && percent <= 64){
  292.      if(BUTTON_PRESSED){
  293.       //create variables to store color values
  294.       //write a line that
  295.       //digitalWrite(LED, HIGH); //LED ON
  296.       pxl.setPixelColor(PXL1[0], pxl.Color(128, 255, 0));
  297.       pxl.show();
  298.       pxl2.setPixelColor(PXL2[0], pxl2.Color(128, 255, 0));
  299.       pxl2.show();
  300.     }else{
  301.       //digitalWrite(LED, LOW); //LED OFF  
  302.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  303.       pxl.show();
  304.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  305.       pxl2.show();
  306.  }
  307.  
  308.  }else if(percent >= 65 && percent <= 72){
  309.      if(BUTTON_PRESSED){
  310.       //create variables to store color values
  311.       //write a line that
  312.       //digitalWrite(LED, HIGH); //LED ON
  313.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 255, 0));
  314.       pxl.show();
  315.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 255, 0));
  316.       pxl2.show();
  317.     }else{
  318.       //digitalWrite(LED, LOW); //LED OFF  
  319.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  320.       pxl.show();
  321.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  322.       pxl2.show();
  323.  }
  324.  
  325.  }else if(percent >= 73 && percent <= 80){
  326.      if(BUTTON_PRESSED){
  327.       //create variables to store color values
  328.       //write a line that
  329.       //digitalWrite(LED, HIGH); //LED ON
  330.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 255, 128));
  331.       pxl.show();
  332.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 255, 128));
  333.       pxl2.show();
  334.     }else{
  335.       //digitalWrite(LED, LOW); //LED OFF  
  336.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  337.       pxl.show();
  338.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  339.       pxl2.show();
  340.      
  341.  }
  342.  
  343.  }else if(percent >= 81 && percent <= 88){
  344.      if(BUTTON_PRESSED){
  345.       //create variables to store color values
  346.       //write a line that
  347.       //digitalWrite(LED, HIGH); //LED ON
  348.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 255, 255));
  349.       pxl.show();
  350.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 255, 255));
  351.       pxl2.show();
  352.     }else{
  353.       //digitalWrite(LED, LOW); //LED OFF  
  354.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  355.       pxl.show();
  356.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  357.       pxl2.show();
  358.      
  359.  }
  360.  
  361.  }else if(percent >= 89 && percent <= 95){
  362.      if(BUTTON_PRESSED){
  363.       //create variables to store color values
  364.       //then write a line that tells the LED to shine or "show"
  365.       //digitalWrite(LED, HIGH); //LED ON
  366.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 128, 255));
  367.       pxl.show();
  368.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 128, 255));
  369.       pxl2.show();
  370.     }else{
  371.       //digitalWrite(LED, LOW); //LED OFF  
  372.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  373.       pxl.show();
  374.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  375.       pxl2.show();
  376.      
  377.  }
  378.  
  379.  }else if(percent >= 96 && percent <= 98){
  380.      if(BUTTON_PRESSED){
  381.       //create variables to store color values
  382.       //then write a line that tells the LED to shine or "show"
  383.       //digitalWrite(LED, HIGH); //LED ON
  384.       pxl.setPixelColor(PXL1[0], pxl.Color(255, 255, 255));
  385.       pxl.show();
  386.       pxl2.setPixelColor(PXL2[0], pxl2.Color(255, 255, 255));
  387.       pxl2.show();
  388.     }else{
  389.       //digitalWrite(LED, LOW); //LED OFF  
  390.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  391.       pxl.show();
  392.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  393.       pxl2.show();
  394.  }
  395.  
  396.  }else if(percent >= 99 && percent <= 100){
  397.      if(BUTTON_PRESSED){
  398.  
  399.  
  400.    rainbow(4);
  401.   // rainbow2(4);
  402.    
  403.     }else{
  404.       //digitalWrite(LED, LOW); //LED OFF  
  405.       pxl.setPixelColor(PXL1[0], pxl.Color(0, 0, 0));
  406.       pxl.show();
  407.       pxl2.setPixelColor(PXL2[0], pxl2.Color(0, 0, 0));
  408.       pxl2.show();
  409.  }
  410.  
  411.  }
  412.  
  413.  
  414.    
  415.  
  416.      
  417.  
  418.  
  419.  
  420. if(!digitalRead(PUSH_BUTTON2)){
  421.   BUTTON_PRESSED2 = true;
  422.   delay(200);
  423.   }else{
  424.    BUTTON_PRESSED2 = false;
  425.     }
  426.  
  427.  int potentiometerValue2 = analogRead(POTENTIOMETER_PIN2); //read the potentiometer
  428.     int percent2 = map(potentiometerValue2, 0, 1023, 0, 100); //map the pot value to percent
  429.  
  430.    if(percent2 >= 0 && percent2 <= 2){
  431.     if(BUTTON_PRESSED2){
  432. //play tone
  433. tone(BUZZER_PIN, NOTE_B0);
  434.     }else{
  435.  //play no tone
  436.  noTone(BUZZER_PIN);
  437.  }
  438.    }else if(percent2 > 2 && percent2 < 4){
  439.   //draw2();
  440.  if(BUTTON_PRESSED2){
  441.       tone(BUZZER_PIN, NOTE_C1);
  442.     }else{
  443.       noTone(BUZZER_PIN);
  444.  }
  445.  
  446.   }else if(percent2 > 3 && percent2 < 5){
  447.      if(BUTTON_PRESSED2){
  448.       tone(BUZZER_PIN, NOTE_CS1);
  449.     }else{
  450.       noTone(BUZZER_PIN);
  451.     }
  452.  }else if(percent2 > 4 && percent2 < 6){
  453.      if(BUTTON_PRESSED2){
  454.       tone(BUZZER_PIN, NOTE_D1);
  455.     }else{
  456.       noTone(BUZZER_PIN);
  457.     }
  458.  }else if(percent2 > 5 && percent2 < 7){
  459.      if(BUTTON_PRESSED2){
  460.       tone(BUZZER_PIN, NOTE_DS1);
  461.     }else{
  462.       noTone(BUZZER_PIN);
  463.     }
  464.  }else if(percent2 > 6 && percent2 < 8){
  465.      if(BUTTON_PRESSED2){
  466.       tone(BUZZER_PIN, NOTE_E1);
  467.     }else{
  468.       noTone(BUZZER_PIN);
  469.     }
  470.  }else if(percent2 > 7 && percent2 < 9){
  471.      if(BUTTON_PRESSED2){
  472.       tone(BUZZER_PIN, NOTE_F1);
  473.     }else{
  474.       noTone(BUZZER_PIN);
  475.     }
  476.  }else if(percent2 > 8 && percent2 < 10){
  477.      if(BUTTON_PRESSED2){
  478.       tone(BUZZER_PIN, NOTE_FS1);
  479.     }else{
  480.       noTone(BUZZER_PIN);
  481.     }
  482.  }else if(percent2 > 9 && percent2 < 11){
  483.      if(BUTTON_PRESSED2){
  484.       tone(BUZZER_PIN, NOTE_G1);
  485.     }else{
  486.       noTone(BUZZER_PIN);
  487.     }
  488.  }else if(percent2 > 10 && percent2 < 12){
  489.      if(BUTTON_PRESSED2){
  490.       tone(BUZZER_PIN, NOTE_GS1);
  491.     }else{
  492.       noTone(BUZZER_PIN);
  493.     }
  494.  }else if(percent2 > 11 && percent2 < 13){
  495.      if(BUTTON_PRESSED2){
  496.       tone(BUZZER_PIN, NOTE_A1);
  497.     }else{
  498.       noTone(BUZZER_PIN);
  499.     }
  500.  }else if(percent2 > 12 && percent2 < 14){
  501.      if(BUTTON_PRESSED2){
  502.       tone(BUZZER_PIN, NOTE_AS1);
  503.     }else{
  504.       noTone(BUZZER_PIN);
  505.     }
  506.  }else if(percent2 > 13 && percent2 < 15){
  507.      if(BUTTON_PRESSED2){
  508.       tone(BUZZER_PIN, NOTE_B1);
  509.     }else{
  510.       noTone(BUZZER_PIN);
  511.     }
  512.  }else if(percent2 > 14 && percent2 < 16){
  513.      if(BUTTON_PRESSED2){
  514.       tone(BUZZER_PIN, NOTE_C2);
  515.     }else{
  516.       noTone(BUZZER_PIN);
  517.     }
  518.  }else if(percent2 > 15 && percent2 < 17){
  519.      if(BUTTON_PRESSED2){
  520.       tone(BUZZER_PIN, NOTE_CS2);
  521.     }else{
  522.       noTone(BUZZER_PIN);
  523.     }
  524.  }else if(percent2 > 16 && percent2 < 18){
  525.      if(BUTTON_PRESSED2){
  526.       tone(BUZZER_PIN, NOTE_D2);
  527.     }else{
  528.       noTone(BUZZER_PIN);
  529.     }
  530.  }else if(percent2 > 17 && percent2 < 19){
  531.      if(BUTTON_PRESSED2){
  532.       tone(BUZZER_PIN, NOTE_DS2);
  533.     }else{
  534.       noTone(BUZZER_PIN);
  535.     }
  536.  }else if(percent2 > 18 && percent2 < 20){
  537.      if(BUTTON_PRESSED2){
  538.       tone(BUZZER_PIN, NOTE_E2);
  539.     }else{
  540.       noTone(BUZZER_PIN);
  541.     }
  542.  }else if(percent2 > 19 && percent2 < 21){
  543.      if(BUTTON_PRESSED2){
  544.       tone(BUZZER_PIN, NOTE_F2);
  545.     }else{
  546.       noTone(BUZZER_PIN);
  547.     }
  548.  }else if(percent2 > 20 && percent2 < 22){
  549.      if(BUTTON_PRESSED2){
  550.       tone(BUZZER_PIN, NOTE_FS2);
  551.     }else{
  552.       noTone(BUZZER_PIN);
  553.     }
  554.  }else if(percent2 > 21 && percent2 < 23){
  555.      if(BUTTON_PRESSED2){
  556.       tone(BUZZER_PIN, NOTE_G2);
  557.     }else{
  558.       noTone(BUZZER_PIN);
  559.     }
  560.  }else if(percent2 > 22 && percent2 < 24){
  561.      if(BUTTON_PRESSED2){
  562.       tone(BUZZER_PIN, NOTE_GS2);
  563.     }else{
  564.       noTone(BUZZER_PIN);
  565.     }
  566.  }else if(percent2 > 23 && percent2 < 25){
  567.      if(BUTTON_PRESSED2){
  568.       tone(BUZZER_PIN, NOTE_A2);
  569.     }else{
  570.       noTone(BUZZER_PIN);
  571.     }
  572.  }else if(percent2 > 24 && percent2 < 26){
  573.      if(BUTTON_PRESSED2){
  574.       tone(BUZZER_PIN, NOTE_AS2);
  575.     }else{
  576.       noTone(BUZZER_PIN);
  577.     }
  578.  }else if(percent2 > 25 && percent2 < 27){
  579.      if(BUTTON_PRESSED2){
  580.       tone(BUZZER_PIN, NOTE_B2);
  581.     }else{
  582.       noTone(BUZZER_PIN);
  583.     }
  584.  }else if(percent2 > 26 && percent2 < 28){
  585.      if(BUTTON_PRESSED2){
  586.       tone(BUZZER_PIN, NOTE_C3);
  587.     }else{
  588.       noTone(BUZZER_PIN);
  589.     }
  590.  }else if(percent2 > 27 && percent2 < 29){
  591.      if(BUTTON_PRESSED2){
  592.       tone(BUZZER_PIN, NOTE_CS3);
  593.     }else{
  594.       noTone(BUZZER_PIN);
  595.     }
  596.  }else if(percent2 > 28 && percent2 < 30){
  597.      if(BUTTON_PRESSED2){
  598.       tone(BUZZER_PIN, NOTE_D3);
  599.     }else{
  600.       noTone(BUZZER_PIN);
  601.     }
  602.  }else if(percent2 > 29 && percent2 < 31){
  603.      if(BUTTON_PRESSED2){
  604.       tone(BUZZER_PIN, NOTE_DS3);
  605.     }else{
  606.       noTone(BUZZER_PIN);
  607.     }
  608.  }else if(percent2 > 30 && percent2 < 32){
  609.      if(BUTTON_PRESSED2){
  610.       tone(BUZZER_PIN, NOTE_E3);
  611.     }else{
  612.       noTone(BUZZER_PIN);
  613.     }
  614.  }else if(percent2 > 31 && percent2 < 33){
  615.      if(BUTTON_PRESSED2){
  616.       tone(BUZZER_PIN, NOTE_F3);
  617.     }else{
  618.       noTone(BUZZER_PIN);
  619.     }
  620.  }else if(percent2 > 32 && percent2 < 34){
  621.      if(BUTTON_PRESSED2){
  622.       tone(BUZZER_PIN, NOTE_FS3);
  623.     }else{
  624.       noTone(BUZZER_PIN);
  625.     }
  626.  }else if(percent2 > 33 && percent2 < 35){
  627.      if(BUTTON_PRESSED2){
  628.       tone(BUZZER_PIN, NOTE_G3);
  629.     }else{
  630.       noTone(BUZZER_PIN);
  631.     }
  632.  }else if(percent2 > 34 && percent2 < 36){
  633.      if(BUTTON_PRESSED2){
  634.       tone(BUZZER_PIN, NOTE_GS3);
  635.     }else{
  636.       noTone(BUZZER_PIN);
  637.     }
  638.  }else if(percent2 > 35 && percent2 < 37){
  639.      if(BUTTON_PRESSED2){
  640.       tone(BUZZER_PIN, NOTE_A3);
  641.     }else{
  642.       noTone(BUZZER_PIN);
  643.     }
  644.  }else if(percent2 > 36 && percent2 < 38){
  645.      if(BUTTON_PRESSED2){
  646.       tone(BUZZER_PIN, NOTE_AS3);
  647.     }else{
  648.       noTone(BUZZER_PIN);
  649.     }
  650.  }else if(percent2 > 37 && percent2 < 39){
  651.      if(BUTTON_PRESSED2){
  652.       tone(BUZZER_PIN, NOTE_B3);
  653.     }else{
  654.       noTone(BUZZER_PIN);
  655.     }
  656.  }else if(percent2 > 38 && percent2 < 40){
  657.      if(BUTTON_PRESSED2){
  658.       tone(BUZZER_PIN, NOTE_C4);
  659.     }else{
  660.       noTone(BUZZER_PIN);
  661.     }
  662.  }else if(percent2 > 39 && percent2 < 41){
  663.      if(BUTTON_PRESSED2){
  664.       tone(BUZZER_PIN, NOTE_CS4);
  665.     }else{
  666.       noTone(BUZZER_PIN);
  667.     }
  668.  }else if(percent2 > 40 && percent2 < 42){
  669.      if(BUTTON_PRESSED2){
  670.       tone(BUZZER_PIN, NOTE_D4);
  671.     }else{
  672.       noTone(BUZZER_PIN);
  673.     }
  674.  }else if(percent2 > 41 && percent2 < 43){
  675.      if(BUTTON_PRESSED2){
  676.       tone(BUZZER_PIN, NOTE_DS4);
  677.     }else{
  678.       noTone(BUZZER_PIN);
  679.     }
  680.  }else if(percent2 > 42 && percent2 < 44){
  681.      if(BUTTON_PRESSED2){
  682.       tone(BUZZER_PIN, NOTE_E4);
  683.     }else{
  684.       noTone(BUZZER_PIN);
  685.     }
  686.  }else if(percent2 > 43 && percent2 < 45){
  687.      if(BUTTON_PRESSED2){
  688.       tone(BUZZER_PIN, NOTE_F4);
  689.     }else{
  690.       noTone(BUZZER_PIN);
  691.     }
  692.  }else if(percent2 > 44 && percent2 < 46){
  693.      if(BUTTON_PRESSED2){
  694.       tone(BUZZER_PIN, NOTE_FS4);
  695.     }else{
  696.       noTone(BUZZER_PIN);
  697.     }
  698.  }else if(percent2 > 45 && percent2 < 47){
  699.      if(BUTTON_PRESSED2){
  700.       tone(BUZZER_PIN, NOTE_G4);
  701.     }else{
  702.       noTone(BUZZER_PIN);
  703.     }
  704.  }else if(percent2 > 46 && percent2 < 48){
  705.      if(BUTTON_PRESSED2){
  706.       tone(BUZZER_PIN, NOTE_GS4);
  707.     }else{
  708.       noTone(BUZZER_PIN);
  709.     }
  710.  }else if(percent2 > 47 && percent2 < 49){
  711.      if(BUTTON_PRESSED2){
  712.       tone(BUZZER_PIN, NOTE_A4);
  713.     }else{
  714.       noTone(BUZZER_PIN);
  715.     }
  716.  }else if(percent2 > 48 && percent2 < 50){
  717.      if(BUTTON_PRESSED2){
  718.       tone(BUZZER_PIN, NOTE_AS4);
  719.     }else{
  720.       noTone(BUZZER_PIN);
  721.     }
  722.  }else if(percent2 > 49 && percent2 < 51){
  723.      if(BUTTON_PRESSED2){
  724.       tone(BUZZER_PIN, NOTE_B4);
  725.     }else{
  726.       noTone(BUZZER_PIN);
  727.     }
  728.  }else if(percent2 > 50 && percent2 < 52){
  729.      if(BUTTON_PRESSED2){
  730.       tone(BUZZER_PIN, NOTE_C5);
  731.     }else{
  732.       noTone(BUZZER_PIN);
  733.     }
  734.  }else if(percent2 > 51 && percent2 < 53){
  735.      if(BUTTON_PRESSED2){
  736.       tone(BUZZER_PIN, NOTE_CS5);
  737.     }else{
  738.       noTone(BUZZER_PIN);
  739.     }
  740.  }else if(percent2 > 52 && percent2 < 54){
  741.      if(BUTTON_PRESSED2){
  742.       tone(BUZZER_PIN, NOTE_D5);
  743.     }else{
  744.       noTone(BUZZER_PIN);
  745.     }
  746.  }else if(percent2 > 53 && percent2 < 55){
  747.      if(BUTTON_PRESSED2){
  748.       tone(BUZZER_PIN, NOTE_DS5);
  749.     }else{
  750.       noTone(BUZZER_PIN);
  751.     }
  752.  }else if(percent2 > 54 && percent2 < 56){
  753.      if(BUTTON_PRESSED2){
  754.       tone(BUZZER_PIN, NOTE_E5);
  755.     }else{
  756.       noTone(BUZZER_PIN);
  757.     }
  758.  }else if(percent2 > 55 && percent2 < 57){
  759.      if(BUTTON_PRESSED2){
  760.       tone(BUZZER_PIN, NOTE_F5);
  761.     }else{
  762.       noTone(BUZZER_PIN);
  763.     }
  764.  }else if(percent2 > 56 && percent2 < 58){
  765.      if(BUTTON_PRESSED2){
  766.       tone(BUZZER_PIN, NOTE_FS5);
  767.     }else{
  768.       noTone(BUZZER_PIN);
  769.     }
  770.  }else if(percent2 > 57 && percent2 < 59){
  771.      if(BUTTON_PRESSED2){
  772.       tone(BUZZER_PIN, NOTE_G5);
  773.     }else{
  774.       noTone(BUZZER_PIN);
  775.     }
  776.  }else if(percent2 > 58 && percent2 < 60){
  777.      if(BUTTON_PRESSED2){
  778.       tone(BUZZER_PIN, NOTE_GS5);
  779.     }else{
  780.       noTone(BUZZER_PIN);
  781.     }
  782.  }else if(percent2 > 59 && percent2 < 61){
  783.      if(BUTTON_PRESSED2){
  784.       tone(BUZZER_PIN, NOTE_A5);
  785.     }else{
  786.       noTone(BUZZER_PIN);
  787.     }
  788.  }else if(percent2 > 60 && percent2 < 62){
  789.      if(BUTTON_PRESSED2){
  790.       tone(BUZZER_PIN, NOTE_AS5);
  791.     }else{
  792.       noTone(BUZZER_PIN);
  793.     }
  794.  }else if(percent2 > 61 && percent2 < 63){
  795.      if(BUTTON_PRESSED2){
  796.       tone(BUZZER_PIN, NOTE_B5);
  797.     }else{
  798.       noTone(BUZZER_PIN);
  799.     }
  800.  }else if(percent2 > 62 && percent2 < 64){
  801.      if(BUTTON_PRESSED2){
  802.       tone(BUZZER_PIN, NOTE_C6);
  803.     }else{
  804.       noTone(BUZZER_PIN);
  805.     }
  806.  }else if(percent2 > 63 && percent2 < 65){
  807.      if(BUTTON_PRESSED2){
  808.       tone(BUZZER_PIN, NOTE_CS6);
  809.     }else{
  810.       noTone(BUZZER_PIN);
  811.     }
  812.  }else if(percent2 > 64 && percent2 < 66){
  813.      if(BUTTON_PRESSED2){
  814.       tone(BUZZER_PIN, NOTE_D6);
  815.     }else{
  816.       noTone(BUZZER_PIN);
  817.     }
  818.  }else if(percent2 > 65 && percent2 < 67){
  819.      if(BUTTON_PRESSED2){
  820.       tone(BUZZER_PIN, NOTE_DS6);
  821.     }else{
  822.       noTone(BUZZER_PIN);
  823.     }
  824.  }else if(percent2 > 66 && percent2 < 68){
  825.      if(BUTTON_PRESSED2){
  826.       tone(BUZZER_PIN, NOTE_E6);
  827.     }else{
  828.       noTone(BUZZER_PIN);
  829.     }
  830.  }else if(percent2 > 67 && percent2 < 69){
  831.      if(BUTTON_PRESSED2){
  832.       tone(BUZZER_PIN, NOTE_F6);
  833.     }else{
  834.       noTone(BUZZER_PIN);
  835.     }
  836.  }else if(percent2 > 68 && percent2 < 70){
  837.      if(BUTTON_PRESSED2){
  838.       tone(BUZZER_PIN, NOTE_FS6);
  839.     }else{
  840.       noTone(BUZZER_PIN);
  841.     }
  842.  }else if(percent2 > 69 && percent2 < 71){
  843.      if(BUTTON_PRESSED2){
  844.       tone(BUZZER_PIN, NOTE_G6);
  845.     }else{
  846.       noTone(BUZZER_PIN);
  847.     }
  848.  }else if(percent2 > 70 && percent2 < 72){
  849.      if(BUTTON_PRESSED2){
  850.       tone(BUZZER_PIN, NOTE_GS6);
  851.     }else{
  852.       noTone(BUZZER_PIN);
  853.     }
  854.  }else if(percent2 > 71 && percent2 < 73){
  855.      if(BUTTON_PRESSED2){
  856.       tone(BUZZER_PIN, NOTE_A6);
  857.     }else{
  858.       noTone(BUZZER_PIN);
  859.     }
  860.  }else if(percent2 > 72 && percent2 < 74){
  861.      if(BUTTON_PRESSED2){
  862.       tone(BUZZER_PIN, NOTE_AS6);
  863.     }else{
  864.       noTone(BUZZER_PIN);
  865.     }
  866.  }else if(percent2 > 73 && percent2 < 75){
  867.      if(BUTTON_PRESSED2){
  868.       tone(BUZZER_PIN, NOTE_B6);
  869.     }else{
  870.       noTone(BUZZER_PIN);
  871.     }
  872.  }else if(percent2 > 74 && percent2 < 76){
  873.      if(BUTTON_PRESSED2){
  874.       tone(BUZZER_PIN, NOTE_C7);
  875.     }else{
  876.       noTone(BUZZER_PIN);
  877.     }
  878.  }else if(percent2 > 75 && percent2 < 77){
  879.      if(BUTTON_PRESSED2){
  880.       tone(BUZZER_PIN, NOTE_CS7);
  881.     }else{
  882.       noTone(BUZZER_PIN);
  883.     }
  884.  }else if(percent2 > 76 && percent2 < 78){
  885.      if(BUTTON_PRESSED2){
  886.       tone(BUZZER_PIN, NOTE_D7);
  887.     }else{
  888.       noTone(BUZZER_PIN);
  889.     }
  890.  }else if(percent2 > 77 && percent2 < 79){
  891.      if(BUTTON_PRESSED2){
  892.       tone(BUZZER_PIN, NOTE_DS7);
  893.     }else{
  894.       noTone(BUZZER_PIN);
  895.     }
  896.  }else if(percent2 > 78 && percent2 < 80){
  897.      if(BUTTON_PRESSED2){
  898.       tone(BUZZER_PIN, NOTE_E7);
  899.     }else{
  900.       noTone(BUZZER_PIN);
  901.     }
  902.  }else if(percent2 > 79 && percent2 < 81){
  903.      if(BUTTON_PRESSED2){
  904.       tone(BUZZER_PIN, NOTE_F7);
  905.     }else{
  906.       noTone(BUZZER_PIN);
  907.     }
  908.  }else if(percent2 > 80 && percent2 < 82){
  909.      if(BUTTON_PRESSED2){
  910.       tone(BUZZER_PIN, NOTE_FS7);
  911.     }else{
  912.       noTone(BUZZER_PIN);
  913.     }
  914.  }else if(percent2 > 81 && percent2 < 83){
  915.      if(BUTTON_PRESSED2){
  916.       tone(BUZZER_PIN, NOTE_G7);
  917.     }else{
  918.       noTone(BUZZER_PIN);
  919.     }
  920.  }else if(percent2 > 82 && percent2 < 84){
  921.      if(BUTTON_PRESSED2){
  922.       tone(BUZZER_PIN, NOTE_GS7);
  923.     }else{
  924.       noTone(BUZZER_PIN);
  925.     }
  926.  }else if(percent2 > 83 && percent2 < 85){
  927.      if(BUTTON_PRESSED2){
  928.       tone(BUZZER_PIN, NOTE_A7);
  929.     }else{
  930.       noTone(BUZZER_PIN);
  931.     }
  932.  }else if(percent2 > 84 && percent2 < 86){
  933.      if(BUTTON_PRESSED2){
  934.       tone(BUZZER_PIN, NOTE_AS7);
  935.     }else{
  936.       noTone(BUZZER_PIN);
  937.     }
  938.  }else if(percent2 > 85 && percent2 < 87){
  939.      if(BUTTON_PRESSED2){
  940.       tone(BUZZER_PIN, NOTE_B7);
  941.     }else{
  942.       noTone(BUZZER_PIN);
  943.     }
  944.  }else if(percent2 > 86 && percent2 < 88){
  945.      if(BUTTON_PRESSED2){
  946.       tone(BUZZER_PIN, NOTE_C8);
  947.     }else{
  948.       noTone(BUZZER_PIN);
  949.     }
  950.  }else if(percent2 > 87 && percent2 < 89){
  951.      if(BUTTON_PRESSED2){
  952.       tone(BUZZER_PIN, NOTE_CS8);
  953.     }else{
  954.       noTone(BUZZER_PIN);
  955.     }
  956.  }else if(percent2 > 88 && percent2 < 90){
  957.      if(BUTTON_PRESSED2){
  958.       tone(BUZZER_PIN, NOTE_D8);
  959.     }else{
  960.       noTone(BUZZER_PIN);
  961.     }
  962.  }else if(percent2 > 89 && percent2 < 91){
  963.      if(BUTTON_PRESSED2){
  964.       tone(BUZZER_PIN, NOTE_DS8);
  965.     }else{
  966.       noTone(BUZZER_PIN);
  967.     }
  968.  }else if(percent2 > 90 && percent2 <= 100){
  969.      if(BUTTON_PRESSED2){
  970.     //  tone(BUZZER_PIN, NOTE_DS8);
  971. // iterate over the notes of the melody:
  972.   for (int thisNote = 0; thisNote < 24; thisNote++) {
  973.  
  974.     // to calculate the note duration, take one second divided by the note type.
  975.     //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  976.     int noteDuration = 1000 / noteDurations[thisNote];
  977.     tone(8, melody[thisNote], noteDuration);
  978.  
  979.     // to distinguish the notes, set a minimum time between them.
  980.     // the note's duration + 30% seems to work well:
  981.     int pauseBetweenNotes = noteDuration * 1.30;
  982.     delay(pauseBetweenNotes);
  983.     // stop the tone playing:
  984.     noTone(8);
  985.   }
  986.    
  987.     }else{
  988.       noTone(BUZZER_PIN);
  989.     }
  990.  }
  991.  
  992.  
  993.  
  994.  
  995.     }
  996.  
  997.     void rainbow(int wait) {
  998.   // Hue of first pixel runs 5 complete loops through the color wheel.
  999.   // Color wheel has a range of 65536 but it's OK if we roll over, so
  1000.   // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  1001.   // means we'll make 5*65536/256 = 1280 passes through this loop:
  1002.   for(long firstPixelHue = 5*65536; firstPixelHue > 0; firstPixelHue -= 256) {
  1003.     // strip.rainbow() can take a single argument (first pixel hue) or
  1004.     // optionally a few extras: number of rainbow repetitions (default 1),
  1005.     // saturation and value (brightness) (both 0-255, similar to the
  1006.     // ColorHSV() function, default 255), and a true/false flag for whether
  1007.     // to apply gamma correction to provide 'truer' colors (default true).
  1008.     pxl.rainbow(firstPixelHue);
  1009.  
  1010.     pxl2.rainbow(firstPixelHue);
  1011.     // Above line is equivalent to:
  1012.     // strip.rainbow(firstPixelHue, 1, 255, 255, true);
  1013.     pxl.show(); // Update strip with new contents
  1014.     pxl2.show(); // Update strip with new contents
  1015.     delay(wait);  // Pause for a moment
  1016.    
  1017.   }
  1018.     }
  1019.      
  1020.    
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement