Input/Output

Posted by Avery

Avery's picture

Description

Have input and output come from the same object (an octopus? with two arms?). Turning one pot changes rate of the speaker and LED. Turning the other pot changes the note of the speaker.

Materials

  • 1x Light Emitting Diode (LED)
  • 1x Resistor
  • 1x Speaker
  • 2x Potentiometer

Audrino Code

/* Sound Serial (aka Keyboard Serial)
 * ------------
 *
 * Program to play tones depending on the data coming from the serial port.
 *
 * The calculation of the tones is made following the mathematical
 * operation:
 *
 *       timeHigh = 1/(2 * toneFrequency) = period / 2
 *
 * where the different tones are described as in the table:
 *
 * note     frequency     period     PW (timeHigh)    
 * c             261 Hz             3830     1915     
 * d             294 Hz             3400     1700     
 * e             329 Hz             3038     1519     
 * f             349 Hz             2864     1432     
 * g             392 Hz             2550     1275     
 * a             440 Hz             2272     1136     
 * b             493 Hz             2028    1014    
 * C            523 Hz            1912     956
 *
 * (cleft) 2005 D. Cuartielles for K3
 *
 * Updated by Tod E. Kurt <tod@todbot.com> to use new Serial. commands
 * and have a longer cycle time.
 *
 */

int ledPin = 9;
int potPin = 1;
int potPin2 = 2;
int val = 0;
int val2 = 0;
int speakerPin = 7;

// note names and their corresponding half-periods  
byte names[] ={ 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
 
int serByte = -1;
int ledState = LOW;
int count = 0;
int freq = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {
  val = analogRead(potPin);
  Serial.println(val);
  if (val < 146) {
    serByte = 'c';
  }
  if (val >= 146 && val < 292) {
    serByte = 'd';
  }
  if (val >= 292 && val < 483) {
    serByte = 'e';
  }
  if (val >= 438 && val < 584) {
    serByte = 'f';
  }
  if (val >= 584 && val < 730) {
    serByte = 'g';
  }
  if (val >= 730 && val < 876) {
    serByte = 'a';
  }
  if (val >= 876) {
    serByte = 'C';
  }
  val2 = analogRead(potPin2);
  digitalWrite(speakerPin, LOW);
  for (count=0;count<=8;count++) {  // look for the note
  Serial.println(names[count]);
  Serial.println (serByte);
    if (names[count] == serByte) {  // ahh, found it
      for( int i=0; i<val2; i++ ) {   // play it for 50 cycles
        digitalWrite(speakerPin, HIGH);
        digitalWrite(ledPin, ledState);
        delayMicroseconds(tones[count]);
        digitalWrite(speakerPin, LOW);
        ledState = !ledState;
        digitalWrite(ledPin, ledState);
        delayMicroseconds(tones[count]);
      }
    }
  }
}

Demo (Video Link)

http://www.youtube.com/watch?v=Zr3WJFJLxeo

0
Your rating: None