Uninspired sound/light machine

timothykc's picture

 

Using the potentiometer as a knob, we have three options on the music box. Turning the knob changes the song, and the color of the led.
 
 
/* Adapted from Theremin, LED, Potentiometer, and lots of other junk
 * --------
 *
 *
 * Most of the credit goes to:
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 */
int aOut = 9;   // LEDs connected to digital pins 9, 10 and 11
int bOut = 10;  //   (Connect cathodes to digital ground)
int cOut = 11;  
int aVal = 0;   // Variables to store the input from the potentiometers
int bVal = 0;  
int cVal = 0; 
 
int ledPin = 13;
int potPin = 0;    // select the input pin for the potentiometer
int speakerPin = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
byte melody2[] = "4c4c4g4g4a4a8g4f4f4e4e4d4d8c4g4g4f4f4e4e8d4g4g4f4f4e4e8d4c4c4g4g4a4a8g4f4f4e4e4d4d8c";
byte melody3[] = "4g4a4b4g4g4a4b4g4b4c8d4b4c8d2d2e2d2c4b4g2d2e2d2c4b4g4g4d8g4g4d8g";
 
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
 
 
 
int val = 0;
 
void setup() {
  pinMode(speakerPin, OUTPUT); 
  pinMode(ledPin, OUTPUT); 
  pinMode(aOut, OUTPUT);   // sets the digital pins as output
  pinMode(bOut, OUTPUT);   
  pinMode(cOut, OUTPUT); 
  Serial.begin(9600);
  Serial.println("ready");
}
 
void loop() {
  digitalWrite(speakerPin, LOW);
  
  val = analogRead(potPin);    // read value from the sensor
  val = map(val, 0, 1023, 0, 255);                 // process the value a little
  //val = val/2;                 // process the value a little
  
  if (val < 85) {
    analogWrite(aOut, 255);    // Send new values to LEDs
    analogWrite(bOut, 0);    // Send new values to LEDs
    analogWrite(cOut, 0);    // Send new values to LEDs
    for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody3[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody3[count*2 + 1]) {       
          digitalWrite(speakerPin,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerPin, LOW);
          delayMicroseconds(tones[count2]);
        } 
        if (melody3[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerPin, 0);
          delayMicroseconds(500);
        }
      }
  }
    }
  }
  else if (val > 171) {
    analogWrite(aOut, 0);    // Send new values to LEDs
    analogWrite(bOut, 255);    // Send new values to LEDs
    analogWrite(cOut, 0);    // Send new values to LEDs
      for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerPin,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerPin, LOW);
          delayMicroseconds(tones[count2]);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerPin, 0);
          delayMicroseconds(0);
        }
      }
  }
    }
  }
  
  else {
    analogWrite(aOut, 0);    // Send new values to LEDs
    analogWrite(bOut, 0);    // Send new values to LEDs
    analogWrite(cOut, 255);    // Send new values to LEDs
      for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody2[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody2[count*2 + 1]) {       
          digitalWrite(speakerPin,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerPin, LOW);
          delayMicroseconds(tones[count2]);
        } 
        if (melody2[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerPin, 0);
          delayMicroseconds(0);
        }
      }
  }
    }
  }
}
 
0
Your rating: None