Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. //#include "musicBox.h" //this is for our tones and is for reference.
  2. int knob = A4; //analog pin for the potentiometer
  3. int ledPin1 = 5;
  4. int ledPin2 = 6;
  5. int ledPin3 = 7;
  6. int speakerPin = 8;
  7. int length = 15; // the number of notes
  8. char notes[] = "ccggaagffeeddc "; // a space represents a rest
  9. int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
  10. int tempo = 300;
  11. int btn = 12; //pin for the button
  12.  
  13. void playTone(int tone, int duration) {
  14. for (long i = 0; i < duration * 1000L; i += tone * 2) {
  15. digitalWrite(speakerPin, HIGH);
  16. delayMicroseconds(tone);
  17. digitalWrite(speakerPin, LOW);
  18. delayMicroseconds(tone);
  19. }
  20. }
  21.  
  22. void playNote(char note, int duration) {
  23. char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  24. int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  25.  
  26. // play the tone corresponding to the note name
  27. for (int i = 0; i < 8; i++) {
  28. if (names[i] == note) {
  29. playTone(tones[i], duration);
  30. }
  31. }
  32. }
  33.  
  34.  
  35. void setup() {
  36. pinMode(speakerPin, OUTPUT);
  37. Serial.begin(57600); //start printing out to the serial port for processing to read from
  38. pinMode(knob, INPUT);//add a input for your potentiometer
  39. pinMode(btn, INPUT_PULLUP);//add an input for the button. We're using input pull up because otherwise, there'd be a short
  40. //you can also do this in hardware. I often do. See pull up resistor
  41.  
  42. pinMode(ledPin1, OUTPUT);
  43. pinMode (ledPin2, OUTPUT);
  44. pinMode(ledPin3, OUTPUT);
  45.  
  46.  
  47. }
  48.  
  49. void loop() {
  50. int data = analogRead(knob); //take an analog read on the knob to read resistance(0 to 1024)
  51. int btnState = digitalRead(btn); //digitally read the button (0 or 1)
  52. //Serial.println(btnState)
  53. ; //optional print line for testing
  54. data = map(data, 0, 1024, 65, 800);//using the map function, transform the range of the
  55. //tone that the resistor is in, 0 to 1024 to the range of tones we want to play.
  56.  
  57. //turn on
  58. digitalWrite(ledPin1, HIGH);
  59. digitalWrite(ledPin2, HIGH);
  60. digitalWrite(ledPin3, HIGH);
  61.  
  62. //wait 1 sec
  63. delay(1000);
  64. //turn off
  65. digitalWrite(ledPin1, LOW);
  66. digitalWrite(ledPin2, LOW);
  67. digitalWrite(ledPin3, LOW);
  68.  
  69. //wait 1 sec
  70. delay(1000);
  71.  
  72. if(btnState==0) // the button is pushed down
  73. {
  74. int pauseBetweenNotes = 2 * 1.30;//make a little pause to not overwhelm arduino
  75. tone(8, data,500); // play a tone on pin 8, at the mapped sound frequency for 500ms
  76. delay(pauseBetweenNotes); // a little pause
  77. Serial.println(data); // print it out
  78. noTone(8);//turn off the pad
  79. }else{ //the button is not pushed
  80. noTone(8); //make no noise
  81. }
  82. for (int i = 0; i < length; i++) {
  83. if (notes[i] == ' ') {
  84. delay(beats[i] * tempo); // rest
  85. } else {
  86. playNote(notes[i], beats[i] * tempo);
  87. }
  88.  
  89. // pause between notes
  90. delay(tempo / 2);
  91. }
  92.  
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement