Advertisement
Guest User

music player

a guest
Sep 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. */
  4. #define S0 4
  5. #define S1 5
  6. #define S2 6
  7. #define S3 7
  8. #define sensorOut 8
  9. #define SPEAKER 9
  10.  
  11. const int buttonPin = 2;
  12. const int switchPin = 3;
  13.  
  14. int buttonstate = 0;
  15. int switchstate = 0;
  16.  
  17. int frequencies[20] = {0};
  18. int red_frequency = 0;
  19. int blue_frequency = 0;
  20. int green_frequency = 0;
  21. int sound_frequency = 0;
  22.  
  23. int frequency_calculation(int red_freq, int green_freq, int blue_freq) {
  24.   return (red_freq + green_freq + blue_freq);
  25. }
  26. void setup() {
  27.   pinMode(S0, OUTPUT);
  28.   pinMode(S1, OUTPUT);
  29.   pinMode(S2, OUTPUT);
  30.   pinMode(S3, OUTPUT);
  31.   pinMode(sensorOut, INPUT);
  32.   pinMode(buttonPin, INPUT);
  33.   pinMode(switchPin, INPUT);
  34. }
  35.  
  36. void loop() {
  37.   buttonState = digitalRead(buttonPin);
  38.   switchState = digitalRead(swithPin);
  39.   if (switchState == HIGH) {
  40.     for (int i = 0; i < 20; i++) {
  41.       tone(SPEAKER, frequencies[i], 100);
  42.       delay(100);
  43.     }
  44.   } else {
  45.       if (buttonState == HIGH) {
  46.         for (int i = 0; i < 20; i++) {
  47.         /*Getting the red frequency */
  48.         digitalWrite(S2, LOW);
  49.         digitalWrite(S3,LOW);
  50.         red_frequency = pulseIn(sensorOut, LOW);
  51.      
  52.         /*Getting the green frequency */
  53.         digitalWrite(S2,HIGH);
  54.         digitalWrite(S3,HIGH);
  55.         green_frequency = pulseIn(sensorOut, LOW);
  56.        
  57.         /*Getting the blue frequency */
  58.         digitalWrite(S2,LOW);
  59.         digitalWrite(S3,HIGH);
  60.         blue_frequency = pulseIn(sensorOut, LOW);
  61.      
  62.         /*Getting the sound frequency and saving the value in the array*/
  63.         sound_frequency = frequency_calculation(red_frequency, green_frequency, blue_frequency);
  64.         tone(SPEAKER, sound_frequency, 100);
  65.         frequencies[i] = sound_frequency;
  66.         }
  67.     } else {
  68.       /*Getting the red frequency */
  69.       digitalWrite(S2, LOW);
  70.       digitalWrite(S3,LOW);
  71.       red_frequency = pulseIn(sensorOut, LOW);
  72.      
  73.       /*Getting the green frequency */
  74.       digitalWrite(S2,HIGH);
  75.       digitalWrite(S3,HIGH);
  76.       green_frequency = pulseIn(sensorOut, LOW);
  77.      
  78.       /*Getting the blue frequency */
  79.       digitalWrite(S2,LOW);
  80.       digitalWrite(S3,HIGH);
  81.       blue_frequency = pulseIn(sensorOut, LOW);
  82.      
  83.       /*Playing the sound */
  84.       sound_frequency = frequency_calculation(red_frequency, green_frequency, blue_frequency);
  85.       tone(SPEAKER, sound_frequency, 100);
  86.       delay(100);
  87.     }
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement