Xylophone – Yesenia & Zaina

When we started thinking about a potential instrument to make we thought of a piano, xylophone or a guitar. We began by experimenting with different analog sensors including pressure/force sensors and piezo elements. We settled on making a xylophone using 4 pressure sensors so that each time the sensor is pressed, the buzzer would make a sound and at the same time a LED would light up (synced with when the note is pressed). Syncing the LEDs with the sensors is what we struggled with the most, we had the wiring as well as the code however we could not get it to work. 

First, we wired up the sensors & buzzer and wrote up the code to make each pressure sensor play a different note when pressed. 

We then wired up a button (digital sensor) with 4 different LEDs. We did these two steps independently in order to make sure each one worked well on its own. However, what we found most challenging was to combine the two circuits and codes to make the whole thing work altogether. We wanted (1) the buzzer to play sound when the pressure sensor was hit, (2) the LED to light up when the pressure sensor was hit and be off otherwise and (3) the button to enable the ON and OFF of the LEDs adding a light feature to the xylophone. 

#include "pitches.h"
#define FORCE_SENSOR_BLUE A0
#define FORCE_SENSOR_GREEN A1
#define FORCE_SENSOR_RED A2
#define FORCE_SENSOR_YELLOW A3

const int ledPinBlue = 7;
const int ledPinGreen = 2;
const int ledPinRed = 3;
const int ledPinYellow = 4;

const int buttonPin = 5;

bool onOff = HIGH;
byte prevButtonState = LOW;
bool blinking = false;

int notes [10] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5};

void setup() { 
  Serial.begin(9600);
  
//  pinMode(ledPinBlue, OUTPUT);
//  pinMode(ledPinGreen, OUTPUT);
//  pinMode(ledPinRed, OUTPUT);
  pinMode(ledPinYellow, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {

  byte buttonState  = digitalRead(buttonPin);
  Serial.println(buttonState);

  if (buttonState == HIGH && prevButtonState == LOW) {

    blinking = !blinking;
  }
  
  prevButtonState = buttonState;
  
  int analogReadYellow = analogRead(FORCE_SENSOR_YELLOW);
  int analogReadRed = analogRead(FORCE_SENSOR_RED);
  int analogReadGreen = analogRead(FORCE_SENSOR_GREEN);
  int analogReadBlue = analogRead(FORCE_SENSOR_BLUE);

  Serial.print(analogReadYellow);
  Serial.print(analogReadRed);
  Serial.print(analogReadGreen);
  Serial.print(analogReadBlue);// print the raw analog reading

  if (analogReadYellow > 100) {  
    Serial.println(" --> YELLOW ");
    tone (6, NOTE_C4, 200);
  }
  if (blinking == true){
    digitalWrite(ledPinYellow, HIGH);
    }
   else {
    digitalWrite(ledPinYellow, LOW);
    } 
    
  if (analogReadRed > 200) {   
    Serial.println(" --> RED ");
    tone (6, NOTE_D4, 200);
    pinMode(ledPinRed, HIGH);
    }
   else {
    digitalWrite(ledPinRed, LOW);
    } 
    
  if (analogReadGreen > 200) {   
    Serial.println(" --> GREEN ");
    tone (6, NOTE_E4, 200);
    pinMode(ledPinGreen, HIGH);
    }
   else {
    digitalWrite(ledPinGreen, LOW);
    } 

  if (analogReadBlue > 200) {   
    Serial.println(" --> BLUE ");
    tone (6, NOTE_F4, 200);
    pinMode(ledPinBlue, HIGH);
    }
   else {
    digitalWrite(ledPinBlue, LOW);
    }
}

We then spent time working on the aesthetics of the instrument. Using cardboard and paper we created the xylophone with a box underneath (where we planned to hide all the wires and breadboard). We made two mallets and had to make them heavy enough so that the sensors could detect the pressure. We also realized that by using the proper resistor (10k) the sensor was more sensitive and did not require for the mallet to be used with much force. Switching the sensors and the wires over to the cardboard box was very difficult as there are a lot of wires, but, what worked was checking the wiring for each sensor by attaching a spare sensor before attaching the wires to the sensor in the box, this was a quick and easy way to make sure it was working properly, before make it so that you can’t see where the wires are and if they’re touch. 

Final

https://youtube.com/shorts/9cNCOIGK0fQ

Leave a Reply