Music Instrument

Concept:

I think it was a challenge for us to find an idea for this project. Alex and I shared some videos of cool musical instruments with one another. We also tried to brainstorm some ideas and think of how to do it. We wanted to do something interesting, and creative. After a series of discussions, we finally decided to make a flower and bee instrument. This instrument plays different sounds if the bee comes to a flower, or if sunlight is pointed at a flower. Also, it plays sound when the bee is going to the flower. We did this by making a circuit that had a toggle switch, an LED, four light sensors, one distance sensor, and a piezo buzzer. Would it be cool if one could hear sounds emitted by a flower under different conditions?

Our instrument makes different notes with the interaction between the flower, bee, and light, which I like to think of as a musical narrative that delves into the natural world.

Highlight:

 We began by assembling the circuit and making the flower, bees, and light source.

It was confusing to make the circuit, but it worked out fine in the end. We decided to add a toggle switch to move from the distance sensor to the light sensor, this enhanced the user experience by allowing them to transition between the distance sensor and the light sensors. The light sensor and the distance sensor add different interactive experiences for the user, which I think engages them more.

#include "pitches.h"

const int BUZZER = 8;
const int ECHO_PIN = 9;
const int TRIG_PIN = 10;
const int FLOWER1 = A0;
const int FLOWER2 = A1;
const int FLOWER3 = A2;
const int FLOWER4 = A3;
const int SWITCH = A5;
int flowerSound = 0;
int LED=2;
long duration = 100;

int DARK_THR = 600;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(FLOWER1, INPUT);
  pinMode(FLOWER2, INPUT);
  pinMode(FLOWER3, INPUT);
  pinMode(FLOWER4, INPUT);
  pinMode(BUZZER, OUTPUT);  
  pinMode(ECHO_PIN, INPUT);    // echo pin measure the duration of pulses coming back from the distance sensor
  pinMode(TRIG_PIN, OUTPUT);   // trigger pin output pulses of electricity
  pinMode(LED,OUTPUT);
}

void loop() {
  digitalWrite(LED,HIGH);
  int flower1 = analogRead(FLOWER1);
  delay(1);  // delay in between reads for stability
  int flower2 = analogRead(FLOWER2);
  delay(1);  // delay in between reads for stability
  int flower3 = analogRead(FLOWER3);
  delay(1);  // delay in between reads for stability
  int flower4 = analogRead(FLOWER4);
  delay(1);  // delay in between reads for stability
  int switchOn = digitalRead(SWITCH);
  delay(1);  // delay in between reads for stability

  // Serial.println(switchOn);  ///debugging

  if (switchOn) {
    //dark sensor = lower notes
    if (flower1 <= DARK_THR) {
      tone(BUZZER, NOTE_C3, duration);
    } else if (flower2 <= DARK_THR) {
      tone(BUZZER, NOTE_D3, duration);
    } else if (flower3 <= DARK_THR) {
      tone(BUZZER, NOTE_E3, duration);
    } else if (flower4 <= DARK_THR) {
      tone(BUZZER, NOTE_F3, duration);

    //bright sensor = higher notes
    } else if (flower1 >= 850) {
      tone(BUZZER, NOTE_G3, duration);
    } else if (flower2 >= 850) {
      tone(BUZZER, NOTE_A3, duration);
    } else if (flower3 >= 850) {
      tone(BUZZER, NOTE_B3, duration);
    } else if (flower4 >= 850) {
      tone(BUZZER, NOTE_C4, duration);
    } else {         
      noTone(BUZZER);
    }
  } else { // if switch is changed to distance sensor
    int distance = getDistance();
    Serial.println(distance);
    if (1 < distance && distance < 3) {
      tone(BUZZER, NOTE_C4);
    } else if (3 < distance && distance < 6) {
      tone(BUZZER, NOTE_D4);
    } else if (6 < distance && distance < 9) {
      tone(BUZZER, NOTE_E4);
    } else if (9 < distance && distance < 12) {
      tone(BUZZER, NOTE_F4);
    } else if (12 < distance && distance < 15) {
      tone(BUZZER, NOTE_G4);
    } else if (15 < distance && distance < 18) {
      tone(BUZZER, NOTE_A4);
    } else if (18 < distance && distance < 21) {
      tone(BUZZER, NOTE_B4);
    } else {
      noTone(BUZZER);
    }
  }
}

float getDistance() {
  float echoTime;                   //variable to store the time it takes for a ping to bounce off an object
  float calculatedDistance;         //variable to store the distance calculated from the echo time

  //send out an ultrasonic pulse that's 10ms long
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  echoTime = pulseIn(ECHO_PIN, HIGH);      //pulsein command to see how long it takes for pulse to bounce back to sensor
  calculatedDistance = echoTime / 55.2;    //calculate  distance of object that reflected the pulse in cm 
  return calculatedDistance;  
}

 

The code was simple, we created a nested if condition so that when the switch is on the light sensors (the flowers) would play different notes. The notes change in pitch from low to high depending on whether the bee is on the flower of the light source. Then when the switch is off we move to the distance sensor where the bee moves displaying different sounds. These notes are played with a difference of 3cm in distance. For the distance sensor, we play a whole octave of notes.

 

 Reflection and Future Improvements:

 I enjoyed making this assignment, but I believe there is room for improvement. There is much more that can be done and improved so that the sound is clearer, and the instrument is more interesting. It would be cool if it were a dual-player instrument, which means that the distance sensor and the light sensor play the sounds at the same time. It is a little more complicated to do and would need more time. On the aesthetics aspect, canceling all the wires in a box would be neater, and safer for users, thus providing an appealing look. I believe there is space to enhance this project in terms of functionality and look; however, this project was a challenge technically but sparked our imagination.

Leave a Reply