Color Wheel

Posted by rami

rami's picture

 

Description

A color wheel attached to th DC motor and a pot to control the speed. Music also played from speakers that picked up in tempo the faster the wheel spun

Components

Breadboard

Arduino Microcontroller

Resistors

1 Pot

1 Piezo Speaker

Paper for disc

 

 Code

 

/*
 * I modfied this code to have music. When the motor speeds up, both the disc's spin and the song, Blue *Danube Waltz speeds up.
 * one pot fades one motor
 * modified version of AnalogInput
 * by DojoDave <http://www.0j0.org>
 * http://www.arduino.cc/en/Tutorial/AnalogInput
 * Modified again by dave
 * Middle C = C4
 *
 * note     frequency     period     PW (timeHigh)
 * c3 (C)       139 hz                  3597
 * d3 (D)       147 hz                  3401
 * e3 (E)       165 hz                  3030
 * f3 (F)       174 hz                  2907
 * g3 (G)       196 hz                  2551
 * a3 (A)       220 hz                  2273
 * b3 (B)    245 hz                  2041
 * c4            261 Hz             3830     1915     
 * d4             294 Hz             3400     1700
 * dSharp       311 Hz                  1607
 * e4             329 Hz             3038     1519     
 * f4             349 Hz             2864     1432     
 * fsharp       370 hz                   1351
 * g4             392 Hz             2550     1275     
 * a4             440 Hz             2272     1136     
 * b4             493 Hz             2028    1014    
 * c5 (Cfive)    523 Hz            1912     956
 * d5           587                     852
 * e5           659                     759
 * f5           698                     716
 * g5           784                     638
 *
 * (cleft) 2005 D. Cuartielles for K3
 */

int potPin = 0;   // select the input pin for the potentiometer
int motorPin = 9; // select the pin for the Motor
int sensorVal = 0;      // variable to store the value coming from the sensor
int speakerOut = 7;               
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2c1p2c1p2e1p2g1p2g3p2g1p2g2p2e1p2e2p";

// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30

int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 116; // the longer the song, the longer this should be. if short, song will cut off.

void setup() {
  pinMode(speakerOut, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  sensorVal = analogRead(potPin);    // read the value from the sensor, between 0 - 1024
 
  Serial.println(sensorVal);
 
 
  if (sensorVal > 0 && sensorVal <= 200 ){ //these conditions read sensor. if sensor is high, then sound is fast, else, sound is slow. Sound is normal here.
   analogWrite(motorPin, 65); // if sensor val is more than 0 and less than 200, turn motor on.
   for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 50; count3++) { //50 is what makes sound fast or slow
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
        }
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
      }
   }
  }
 
  else if (sensorVal > 200 && sensorVal <= 400 ){ // Sounds speed up when pot is turned.
   analogWrite(motorPin, 90);
   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(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
        }
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
      }
   }
  }
 
  else if (sensorVal > 400 && sensorVal <= 600 ){ // Sounds speed up when pot is turned
   analogWrite(motorPin, 100);
   for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 20; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
        }
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
      }
   }
  }
 
  else if (sensorVal > 600 && sensorVal <= 800 ){ // Sounds speed up when pot is turned
   analogWrite(motorPin, 105);
   for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 10; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
        }
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
      }
   }
  }
 
  else if (sensorVal > 800){ // Sounds speed up when pot is turned
  analogWrite(motorPin, 200);
   for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 5; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerOut,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]);
        }
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
      }
   }
  }
  else if (sensorVal == 0){ // sound and motor turn off when pot val is 0 (off)
      digitalWrite(speakerOut, 0);
      analogWrite(motorPin, 0);
      return;
      }
 
}

0
Your rating: None
Tags: