Description:
For this week's assignment, I built a mixer with three units that control three tones that are played in sequence. Each unit consists of a photocell and an LED. If a finger is held above a unit, the led flashes and the tone that corresponds to the unit is played in the sequence; otherwise, the tone is silent. For example, if the finger arrangement was {finger, finger, empty}, the mixer would play and flash {tone, tone, silent}.
Materials:
1 Arduino Uno
1 Solderless breadboard
3 LEDs
3 Photocells
1 Piezo buzzer
Code:
int ledPinA = 11;
int ledPinB = 10;
int ledPinC = 9;
int pcPinA = A0;
int pcPinB = A1;
int pcPinC = A2;
int speakerPin = 3;
boolean playA = false;
boolean playB = false;
boolean playC = false;
int pcPinAVal = 0;
int pcPinBVal = 0;
int pcPinCVal = 0;
int shiftValue = 30;
int delayValue = 50;
// 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 count = 0;
int pcPinCurrent = 0;
void setup() {
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(pcPinA, INPUT);
pinMode(pcPinB, INPUT);
pinMode(pcPinC, INPUT);
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void processPin(int* pcPin, int* pcPinPrevious, boolean* play, int* ledPin, byte note) {
pcPinCurrent = analogRead(*pcPin);
if(pcPinCurrent < *pcPinPrevious - shiftValue) {
*play = true;
}
if(pcPinCurrent > *pcPinPrevious + shiftValue) {
*play = false;
}
*pcPinPrevious = pcPinCurrent;
if(*play) {
analogWrite(*ledPin, 255);
for (count=0;count<=8;count++) { // look for the note
if (names[count] == note) { // ahh, found it
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[count]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[count]);
}
}
}
analogWrite(*ledPin, 0);
} else {
delay(150);
}
}
void loop() {
processPin(&pcPinA, &pcPinAVal, &playA, &ledPinA, 'a');
delay(delayValue);
processPin(&pcPinB, &pcPinBVal, &playB, &ledPinB, 'c');
delay(delayValue);
processPin(&pcPinC, &pcPinCVal, &playC, &ledPinC, 'd');
delay(delayValue);
}
Images:
http://i44.tinypic.com/2lwswuq.jpg
- Login to post comments