Arduino Ocarina

kilian.moser's picture

Martin and Kilian love playing the Ocarina and the Piezo speakers inspired us :) Hence we built a little Arduino Ocarina that allows you to play one Octave (8 tones).

It is activated by pressing the chin on the Ocarina. The volume can be adjusted using the attached Potentiometer.
We couldn't get the battery pack running. Therefore the USB Cable is still attached to the Arduino to power it.

For a little live demo, please check out our Youtube video: http://www.youtube.com/watch?v=L7kHkkHj_mQ

The following components were used:
- 4 Photo Resistors
- 1 FSR
- 1 Potentiometer
- 1 Piezo Speaker
- 5 Resistors 10kOhm
 

 

The Code:

    // DEBUG switch
    int DEBUG = 0;
     
    // pin setup
    int pinFSR = A1;
    int pinPC1 = A5, pinPC2 = A4, pinPC3 = A3, pinPC4 = A2;
    int pinPiezo = 7;
    int ledPin = 13;
     
    // data
    enum notes {c, d, e, f, g, a, b, C};
    int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
     
    // variables - input
    int valFSR = 0;
    int valPC1 = 0, valPC2 = 0, valPC3 = 0, valPC4 = 0;
     
    // sensitivity thresholds
    int thFSR = 300;
    int thPC1 = 0, thPC2 = 0, thPC3 = 0, thPC4 = 0;
    double thPer = 0.35;
     
    // sensitivity setup
    int maxFSR = 0;
    int maxPC1 = 0, maxPC2 = 0, maxPC3 = 0, maxPC4 = 0;
     
    // switch variable
    int switchVar[4] = {0};
   
    int i = 0;
     
    void setup() {
     Serial.begin(9600);
     
     pinMode(ledPin, OUTPUT);
     pinMode(pinPiezo, OUTPUT);
     
     // calibrate photocells
     maxFSR = analogRead(pinFSR);
     maxPC1 = analogRead(pinPC1);
     maxPC2 = analogRead(pinPC2);
     maxPC3 = analogRead(pinPC3);
     maxPC4 = analogRead(pinPC4);
     thPC1 = maxPC1 * thPer;
     thPC2 = maxPC2 * thPer;
     thPC3 = maxPC3 * thPer;
     thPC4 = maxPC4 * thPer;
    }
     
    void loop() {
      // read analog inteface
      readSensors();
        
      // ocarina active?

      if(!DEBUG){
        if (valFSR >= thFSR){
          for(i=0; i<50; i++)
            playTone(getCurrNote());
        }
      }
    }
     
    void playTone(int note){
      if(note == 8) return;
      digitalWrite(pinPiezo,HIGH);
      delayMicroseconds(tones[note]);
      digitalWrite(pinPiezo, LOW);
      delayMicroseconds(tones[note]);
    }
     
    void readSensors(){
      valFSR = analogRead(pinFSR);
      valPC1 = analogRead(pinPC1);
      valPC2 = analogRead(pinPC2);
      valPC3 = analogRead(pinPC3);
      valPC4 = analogRead(pinPC4);
     
      switchVar[0] = isActive(valPC1, thPC1);
      switchVar[1] = isActive(valPC2, thPC2);
      switchVar[2] = isActive(valPC3, thPC3);
      switchVar[3] = isActive(valPC4, thPC4);
     
      if(DEBUG){
         Serial.print("Photocell 1: ");
         Serial.println(valPC1, DEC);
         Serial.print("Photocell 2: ");
         Serial.println(valPC2, DEC);
         Serial.print("Photocell 3: ");
         Serial.println(valPC3, DEC);
         Serial.print("Photocell 4: ");
         Serial.println(valPC4, DEC);
         Serial.print("FSR: ");
         Serial.println(valFSR, DEC);
      }
    }
     
    int isActive(int val, int thresh){
      if(val <= thresh) return true;
      else return false;
    }
     
    int getCurrNote(){
      if(DEBUG) {
       Serial.print(switchVar[0], DEC);
       Serial.print(switchVar[1], DEC);
       Serial.print(switchVar[2], DEC);
       Serial.println(switchVar[3], DEC);
      }
      if(switchVar[0] && !switchVar[1] && !switchVar[2] && !switchVar[3]){
        if(DEBUG){
           Serial.println("playing note: c");
        }
        return 0;
      }
      if(!switchVar[0] && switchVar[1] && !switchVar[2] && !switchVar[3]){
        if(DEBUG){
           Serial.println("playing note: d");
        }
        return 1;
      }
      if(!switchVar[0] && !switchVar[1] && switchVar[2] && !switchVar[3]){
        if(DEBUG){
           Serial.println("playing note: e");
        }
        return 2;
      }
      if(!switchVar[0] && !switchVar[1] && !switchVar[2] && switchVar[3]){
        if(DEBUG){
           Serial.println("playing note: f");
        }
        return 3;
      }
      if(switchVar[0] && switchVar[1] && !switchVar[2] && !switchVar[3]){
        if(DEBUG){
           Serial.println("playing note: g");
        }
        return 4;
      }
      if(!switchVar[0] && !switchVar[1] && switchVar[2] && switchVar[3]){
        if(DEBUG){
           Serial.println("playing note: a");
        }
        return 5;
      }
      if(switchVar[0] && !switchVar[1] && switchVar[2] && !switchVar[3]){
        if(DEBUG){
           Serial.println("playing note: b");
        }
        return 6;
      }
      if(!switchVar[0] && switchVar[1] && !switchVar[2] && switchVar[3]){
        if(DEBUG){
           Serial.println("playing note: C");
        }
        return 7;
      }
        return 8;
    }
 
 

 

 

0
Your rating: None