Lab5 - IO Play

Submitted by elee on Wed, 03/06/2013 - 00:30
elee's picture

Description

For this lab I sought out to combine the photocell with the piezo speaker to create a device that will change the pitch of a preset tune using the amount of light captured by the photocell.

Materials

  • Arduino Uno
  • Photocell Resistor
  • Piezo Speaker
  • 220 Ohm Resistor
  • 10K Ohm Resistor
  • Many Wires

Code

 

// Output
int redPin   = 10;
int greenPin = 11;
int bluePin  = 9;
 
// Input
int sensorPin = A0;
int sensorValue = 0;
 
int fsrPin = A4; // Force Sensitive Resistor
int fsrVal = 0;
 
int photoPin = A0;
int photoVal = 0;
 
int r = 255;
int g = 1;
int b = 1;
 
int speakerPin = 7;
byte names[] ={ 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
 
int i = 0;     // Loop counter
int runningSum = 0;
int pitchChange = 0;
 
int wait = 10; // 50ms (.05 second) delay; shorten for faster  fades
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial
 
int myMax = 30; // observed 'maximum' value
 
void setup()
{
  pinMode(redPin,   OUTPUT); // these are actually output
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin,  OUTPUT);
  pinMode(speakerPin,  OUTPUT);
  if (DEBUG) {
  }
  Serial.begin(9600);
}
 
// Main program
void loop()
{
  // int switchValue = digitalRead(13);
  // Serial.println(switchValue);
  
  int photoVal = analogRead(photoPin);
  
  int r = 1;
  int g = 1;
  int b = 1;
  float fraction = 1;
  if (photoVal < 255) {
    fraction = float(photoVal) / 255;
    g = fraction * 255;
    if (g >= 255) { g = 255; }
    r = 255 - g;
    if (r <= 0) { r = 1; }
    b = 1;
    pitchChange = -100;
  }
  else if (photoVal < 509) {
    fraction = float(photoVal % 255)/255;
    b = fraction * 255;
    if (b >= 255) { b = 255; }
    g = 255 - b;
    if (g <= 0) { g = 1; }
    r = 1;
    pitchChange = 0;
  }
  else if (photoVal < 763) {
    fraction = float(photoVal % 255)/255;
    r = fraction * 255;
    if (r >= 255) { r = 255; }
    b = 255 - r;
    if (b <= 0) { b = 1; }
    g = 1;
    pitchChange = 100;
  }
  else {
    r = 255;
    b = 1;
    g = 1;
  }
  
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
  
  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[] = "1a2d1a2d2g8p";
  int count = 0;
  int count2 = 0;
  int count3 = 0;
  int MAX_COUNT = 24;
  int statePin = LOW;
  digitalWrite(speakerOut, LOW);     
  for (count = 0; count < MAX_COUNT; count++) {
    statePin = !statePin;
    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]+pitchChange);
          digitalWrite(speakerOut, LOW);
          delayMicroseconds(tones[count2]+pitchChange);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
    }
  }
  
  delay(20);
}

 

 

 

0
Your rating: None
Drupal theme by Kiwi Themes.