Pet Dolphin

Posted by apoorvas

apoorvas's picture

 

 

Description:

The objective of this lab was to integrate the various sensors we have used over the past few labs and create an interactive object where the input and output are on the same objective, i.e. there is an input output coincidence.

 

Materials

  • Arduino Uno Board
  • 2 10k Resistors
  • Wires/Board
  • Photocells (3 Photocells)
  • A softtoy.

Programming:

My inspiration was a “pet” toy that reacts to user input by playing music and flashing lights. So I created a pet dolphin which responds in the following ways:-

If you press the left hand of the dolphin it plays a pre-recorded tune, when its right side is pressed it plays a different tune. If both of the hands are pressed together ( as shown in the video) it plays another different tune.  Depending on which side is pressed the LED near the eye of the respective side also lights up.

Also if you pet the dolphin, and shower it with love, both the LEDs light up , to show its happiness and then it allows you to create your own tunes. You can influence the tunes created by pressing the hands of the dolphin for different periods of time. (To keep with the principle of the Piezo speakers).

If the user gets bored of creating their own tunes, they can pet the dolphin again and it goes back to the original mode of playing the pre-recorded tunes. It allow for immediate feedback and integrates both the input and output on the same object. i.e the dolphin.

 

Design Decision:

I initially thought of using the FSR to detect presses by the user, however, I wanted to allow for multiple inputs and since we only had one FSR , I decided to use the photocells instead. The Photocells work on the same principle as the FSR and allow the user to influence the values by blocking the light sensed by the photocell when they press the hand of the dolphin.

 

Click here to view the video.

 

Arduino code:

 

/*
 * Resistive Sensor Input
 * Takes the input from a resistive sensor, e.g., FSR or photocell
 * Dims the LED accordingly, and sends the value (0-255) to the serial port
 */
int nosePin = 2;  // select the input pin for the sensor
int leftPin = 1; // its left
int rightPin = 0;
int ledPin = 9;
int ledPin2 = 10;// select the output pin for the LED
int speakerPin = 7;
int leftVal = 0;        // variable to store the value coming from the sensor
int rightVal = 0;
int noseVal = 0;
boolean uPlay = false;
 
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
 
 
void setup() {
  pinMode(speakerPin, OUTPUT); 
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  noseVal = analogRead(nosePin);
  Serial.println(noseVal);
  if(noseVal < 20) {
  uPlay = !uPlay;
  analogWrite(ledPin, 50);
  analogWrite(ledPin2, 50);
  }
  else {
    analogWrite(ledPin, 0);
    analogWrite(ledPin2, 0);
  }
  delay(50);
  digitalWrite(speakerPin, LOW);
  leftVal = analogRead(leftPin); // read the value from the sensor, 0-1023
  //Serial.println(leftVal);
  rightVal = analogRead(rightPin); // read the value from the sensor, 0-1023
  //Serial.println(rightVal);
  
  if(uPlay) {
   for(int i = 0;i<500;i++ ) { // play it for 50 cycles
    analogWrite(ledPin, 50);
    analogWrite(ledPin2, 0);
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(leftVal);
    leftVal = analogRead(leftVal);
    digitalWrite(speakerPin, LOW);
    analogWrite(ledPin, 0);
    analogWrite(ledPin2, 50);
    delayMicroseconds(rightVal);
    rightVal = analogRead(rightVal);
   }
  }
   else {
   // Serial.println("in outside uPlay");
    if(leftVal < 20 & rightVal < 20) {
      Serial.print("in both melody");
    for (count = 0; count < MAX_COUNT; count++) {
    analogWrite(ledPin, 50);
    analogWrite(ledPin2, 50);
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerPin, HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerPin, LOW);
          delayMicroseconds(tones[count2]);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerPin, 0);
          delayMicroseconds(500);
        }
      }
    }
      }
    }
 
   else if ( leftVal < 20 ) {
    analogWrite(ledPin, 50);
    analogWrite(ledPin2, 0);
     Serial.print("in left melody");
    for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=7;count2>=0;count2--) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerPin,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerPin, LOW);
          delayMicroseconds(tones[count2]);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerPin, 0);
          delayMicroseconds(500);
        }
      }
    }
  }
   }
  
  else if(rightVal < 20) {
    Serial.println("in right melody");
    analogWrite(ledPin, 0);
    analogWrite(ledPin2, 50);
     for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=7;count2>=0;count2--) {
        if (names[count2] == melody[count*2 + 1]) {       
          digitalWrite(speakerPin,HIGH);
          delayMicroseconds(tones[count2]);
          digitalWrite(speakerPin, LOW);
          delayMicroseconds(tones[count2]);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          digitalWrite(speakerPin, 0);
          delayMicroseconds(500);
        }
      }
    }
  }
  }
   }
    analogWrite(ledPin, 0);
    analogWrite(ledPin2, 0);
}
  
 /* if(val < 20)
  analogWrite(ledPin, 50);
  else analogWrite(ledPin, 0);  // analogWrite (dimming the LED) can be between 0-255
  Serial.println(val);       // writing the value to the PC via serial connection 
  delay(50);                   // rest a little... */

 

 


0
Your rating: None