City of Stars ONLY player

For this week’s instrument assignment, I originally planned to make something similar to Ons and David’s project, where I use the ultrasonic proximity sensor to make a “keyboard” where I can play notes and music. However, as I saw their projects in class, I thought I might change mine up a little bit to avoid having similar projects.

First, my original idea was to have a digital input button that triggered sound whenever it was pressed. The sound played will be decided by the distance sensed by the ultrasonic proximity sensor. The distances correlated with an octave of notes.

A problem that I noticed with this approach, however, was that the music I played was usually chopped up. The interface required me to constantly remember where I have to put the “distance blocker”, and the sound of pressing the button interfered with the song every time I pressed it.

Due to these concerns, I decided that I want my project to just play ONE song, and the distance measurements won’t correlate to notes in order, but the order of the notes in the song. This way, as the “distance blocker” moves further, the song just progresses by itself, and the player could continue to hold down the button.

Tweaks

As I re-designed my project, one obvious problem that would arise is the length of the interface. The song I want to play is City of stars. To code all of the song into arduino, while correlating with distance is problematic. I can’t reduce the space given to each note because of sensitivity issues, where it’d be too hard for the player to control the distance. However, giving each note enough distance would also make the interface too long, and the player wouldn’t be able to reach the end of it.

The solution I came up with is to integrate a knob. The entire rotation of the knob is separated into three parts. When the knob is in the first part, the interface is programmed to play part 1 of the song. Similarly, the second part of the knob programs the interface to play part 2, and the same for part 3. This way I could (sort of) dynamically change the interface when I’m done with a part of the song.

Full code:

#include "pitches.h"

int trig = 7;
int echo = 6;
int piezo = 12;
int button = 2;
int buttonState = 0;

//part 1
int notes[11] = {G3, A3, AS3, D4, E4, F4, D4, E4, C4, D4, A3};
//part 2
int notes2[11] = {A3, AS3, D4, C4, D4, A3, A4, G4, D4, E4, C4};
//part 3
int notes3[11] = {C4, D4, A4, G4, F4, D4, 0, 0, 0, 0, 0};

void setup() {
  Serial.begin (9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(button, INPUT);
}

void loop() {
  int time;
  int distance;

  digitalWrite(trig, HIGH);
  delay(0.01);
  digitalWrite(trig, LOW);
    
  time = pulseIn(echo, HIGH);
    
  //speed of sound = 343 m/s = 0.0343 cm/µs
  // distance = time * speed
  // "taken from David thank you David"
  distance = time * 0.0343 / 2;
//  Serial.print(distance);
//  Serial.println(" cm");

  int knobValue = analogRead(A0);
  Serial.println(knobValue);
  
  int note = map(distance, 2, 40, 0, 11);
  int note2 = map(distance, 2, 40, 0, 11);
  int note3 = map(distance, 2, 40, 0, 11);
  buttonState = digitalRead(button);
  
  if (buttonState == true){
    if (knobValue <= 1023 && knobValue >= 682){
      tone(piezo, notes[note]);
    }
    else if (knobValue < 682 && knobValue >= 341){
      tone(piezo, notes2[note2]);
    }
    else if (knobValue>=0 && knobValue < 341){
      tone(piezo, notes3[note3]);
    }
  }
  else if (buttonState == false){
    noTone(piezo);
  }
}

Arduino Layout

Demonstration

I’m gonna find them all :D

Seven nations army is a classic and that should never be questioned. It is also very repetitive which is helpful if you’re trying to make your Arduino board play it.

For this weeks assignment I created a knob steered Seven Nations Army player, where the users can go through the notes of the tune and play it themselves. I also introduced the breaks, where in-between the notes there are 0 notes, which emit no sounds. By doing that I gained manual control over the length of each note played. I also used a RED EMERGENCY BUTTON for when the speaker was getting to nerve wrecking. When the button is pressed no sounds is emitted 🙂

#include "pitches.h"

#define speaker 4
#define btn 3
#define knob 2

int whichNote = 0;
int knobValue;
int lastNote = 14;

int notes[15] = {0, NOTE_E2, 0, NOTE_E2, 0, NOTE_G2, 0, NOTE_E2, 0, NOTE_D2, 0, NOTE_C2, 0, NOTE_B1, 0};

void setup() {
  pinMode (btn, INPUT);
  Serial.begin (9600);
}

void loop() {
  if (digitalRead(btn) == LOW) {
    whichNote = map(analogRead(A0), 0, 1023, 0, 14);
    tone(4, notes[whichNote], 1000);
  }

  if (whichNote == lastNote) {
    int i, k, t;
    int n = sizeof(notes) / sizeof(notes[0]);
    for (i = 0; i < n / 2; i++) {
      t = notes[i];
      notes[i] = notes[n - i - 1];
      notes[n - i - 1] = t;
    }
    if (lastNote == 0) {
      lastNote = 14;
    }
    else {
      lastNote = 0;
    }
  }
}

 

Keyboard/Theremin – Distance Measuring Sensor

introduction

When we were still working with processing, I made a keyboard simulator for one of my assignments. However, it wasn’t very practical. It was controlled with the arrow keys and space bar, meaning it took some time to travel between notes. So, for this week’s assignment, I decided to redo it with Arduino and a distance measuring sensor. A note will play based on the distance of an object/your hand from the sensor.

The sensor resulted in some noise, so I marked the exact spot where each note would sound decent, using lines on some papers, as shown here:

circuit And functionality

This is how the wiring goes:

As mentioned, I used a distance measuring sensor to (obviously) measure the distance of some object from the sensor. The range of the distance is limited (using constrain and map).
I also used a button. This is used to activate the sound when pressed. This is useful to control the length of a note since the note stops playing when the button is not pressed.
Finally, there is a buzzer which produces the sounds. It is only triggered when the button is pressed and when the object is within the distance range. It plays a specific note based on the distance from the sensor.

difficulties

The wiring for this assignment was quite straightforward. My main struggle this week was with the code. Even though it is brief, it took me a lot of time, trial, and error to figure out how to:

        • initialize the sensor
        • try to eliminate the noise as much as possible with the distance.

Even though I tried my best to eliminate the noise, I wasn’t able to fully do that, that’s why I marked the spot where the note would play clearly with a line for each note.

Code

#include "pitches.h"

int button = 2;
int sensorEcho = 3;
int sensorTrig = 4;
int buzzer = 5;
long distance;
long duration;
int buttonState;

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

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  pinMode(sensorEcho,INPUT);
  pinMode(sensorTrig,OUTPUT);
  pinMode(button, INPUT);
  pinMode(buzzer, OUTPUT);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(button);
  
  //initializing the distance measuring sensor
  digitalWrite(sensorTrig,HIGH);
  delayMicroseconds(1000);
  digitalWrite(sensorTrig, LOW);
  duration=pulseIn(sensorEcho, HIGH); 
  distance =constrain((duration/2)/29.1 ,4,81); //getting the distance value
  delay(10);
   
////  Serial.println(temp);
//
//  Serial.print(duration);
//  Serial.print(" , ");
//  Serial.println(distance);

  //if the button is not pressed or you are outside of the distance range
  if (buttonState == LOW || distance < 3 || distance > 80) 
  {
    noTone(buzzer); //the buzzer will not play anything
  }
  //if the button is pressed and you are within the distance range
  else if (buttonState == HIGH) 
  {
    int note = map(distance, 4, 81, 0, 8); //determine which note to play based on the distance
    tone(buzzer, notes[note]); //play the note
  }
}

//pitches.h
/*************************************************

 * Public Constants

 *************************************************/


#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

final outcome

This is the final outcome.
In the end, I tried (TRIED) to demonstrate how this works by playing a small snippet of a song. However, I’m pretty sure the song is only clear to me because I already know what it is (haha), let me know if you can tell what song it is.

 

Musical instrument with an ultrasonic distance sensor

This week’s assignment was my favorite arduino task thus far. I think it’s because the final outcome seems more reusable for other creative tasks. I used an ultrasonic distance sensor, a push button, and a piezo buzzer to emulate the interactive artwork Aaron once introduced to us in the beginning of the course (tried to find the artwork to show what I was inspired by but was never successful…).

The way it works is very simple. The push button initiates the sound, so only when the button is pressed down, you will be able to hear the notes. You can variate the distance between your hand (or any surface) and the ultrasonic distance sensor to create 3 different notes – middle C (~10 cm), middle D (~15 cm), and middle E (~20 cm). The hand should not move away from the sensor for more than 20 cm for the sound to be activated. Once these conditions are met, the buzzer will emit sound that is corresponding to the notes described above.

While making this instrument, it was important to note the formula for distance that sound travels. This was crucial for me to determine the distance range for each note. Another thing I had to be aware is the fact that echo pin takes in the number for total distance of the sound travel. In other words, I had to be aware that this number represented the round-trip of the sound against the surface and that it had to be halved to obtain the actual distance between the sensor and the object.

Without further ado, here is the video of my working instrument. Please bear in mind that I changed the note after making this video and the notes are now slightly lower and more accurate (Do, Re, Mi).

#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330

int trig = 13;
int echo = 12;
int piezo = 11;
int buttonPin = 3;
int buttonState = 0;

void setup() {
    Serial.begin (9600);
    pinMode(trig, OUTPUT);
    pinMode(echo, INPUT);
    pinMode(buttonPin, INPUT);
}

void loop() {
    int time;
    int distance;
    
    digitalWrite(trig, HIGH);
    delay(0.01);
    digitalWrite(trig, LOW);
    
    time = pulseIn(echo, HIGH);
    
    //speed of sound = 343 m/s = 0.0343 cm/µs
    // distance = time * speed
    distance = time * 0.0343 / 2; // sound wave travels forward and bounces backward.

    Serial.print(distance);
    Serial.println(" cm");

    buttonState = digitalRead(buttonPin);

    if( buttonState == HIGH ) {
      if (distance > 20 || distance < 0){
        noTone(piezo);
        }
      else if (distance < 10){
        tone(piezo, NOTE_C4);
        }
      else if (distance < 15){
        tone(piezo, NOTE_D4);
        }
      else{
        tone(piezo, NOTE_E4);  
        }
    }
    else {
      noTone(piezo);
    }
    delay(100);
}

 

Week 9: Creating an Instrument

As a singer, one of the things I struggle with is finding nice harmonies, as I am really new at that. That’s what inspired me to create an instrument where you can pick a note, and fine the Soprano, Alto, Tenor, and Bass notes for it.

I decided to go for a range of notes that was close to my vocal range, so I went for 15 notes, from D3 to A5. I set up 4 buttons, one resembling each note, soprano being the original melody. The goal was to use the Potentiometer to pick a note, listen to it with the red button (soprano), and then find the 4 part harmony with the other buttons. In the end I should be able to press all four, and hear the harmony.

I mapped the analogue to match the notes I wanted to use, and each button just jumped a few notes from the original, to be a harmony. I only went through a few hours of debugging and creating this, but when I finished I encountered an obstacle i didn’t know how to fix. My sound was very muffled. When I would only have 1 button in use in the code, the sound would be fine. When I tried the code one button at a time, each button worked perfectly. But when I put it together, the sound sounded muffled, laggy, and muddy. The notes also didn’t come outright. At first I realised i had the resistor in the wrong place in some buttons, so when I fixed that it helped, but the issue didnt seem to go away anymore. Im not sure if this is an issue with the button being so small, or not.

#include "pitches.h"
int knob = A0;
int soprano = 11;
int alto = 10;
int tenor = 9;
int bass = 8;

int notes [15] = {NOTE_D3 , NOTE_E3 , NOTE_F3 , NOTE_G3 , NOTE_A4 , NOTE_B4 , NOTE_C4 , NOTE_D4 , NOTE_E4 , NOTE_F4 , NOTE_G4 , NOTE_A5};
//int whichNote = 0;

void setup() {
  pinMode(soprano, INPUT);
  pinMode(alto, INPUT);
  pinMode(tenor, INPUT);
  pinMode(bass, INPUT);
  Serial.begin(9600);
  // put your setup code here, to run once:
}

void loop() {

// put your main code here, to run repeatedly:
int knobValue = analogRead(knob);
int sopranoState = digitalRead(soprano);
int altoState = digitalRead(alto);
int tenorState = digitalRead(tenor);
int bassState = digitalRead(bass);
Serial.println (sopranoState);

//mapping the potentiometer values from 0 - 800 for easy reference
int mappedValue =  map(knobValue, 0, 1023, 0, 15);
//
if (sopranoState == HIGH) {
  tone(4, notes[mappedValue]);
  } else {
    tone(4, 0);
  }
if (altoState == HIGH) {
  tone(4, notes[(mappedValue -2 )]);
  } else {
    tone(4, 0);
  }
if (tenorState == HIGH) {
  tone(4, notes[(mappedValue- 6)]);
  } else {
    tone(4, 0);
  }
if (bassState == HIGH) {
  tone(4, notes[(mappedValue- 8)]);
  } else {
    tone(4, 0);
  }
}


Each one on its own:

All together:

Week 9: Happy Birthday?

Intro + Initial Attempts

This week’s assignment was quite challenging! I tried many things and had to change up details in my idea a lot to get it to work. I was really inspired by the Sound Wall example from Peter Vogel and wanted to incorporate that in the assignment so I can play around with both the photoresistor and the servo motor. Initially, I coded my servo to automatically keep sweeping from left to right, over the photoresistor. I had a small piece of paper taped on my servo arm to cast a shadow over the photoresistor.

 The goal was to have a song playing, and then whenever the arm is over the photoresistor, the song switches to a lower octave to simulate a darker sound. The issue I had here is that the code for sweeping the servo arm relied on the delay(), and I think that interfered with triggering the buzzer in a regular manner.

 

 

//sweep code from Arduino Reference//
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    //delay(40);                       // waits 40ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    //delay(40);                       // waits 40ms for the servo to reach the position
}

So, I decided to move the servo arm in an analog matter using the potentiometer and was presented with a second issue. I struggled with having the music playing all the time simultaneously with moving the servo, so I couldn’t have it switch between songs like I had imagined. I’m still a bit confused about how the movement of the servo interferes with the buzzer functions and how to navigate that and want to look into that.

Final Idea

So, I decided to make an “instrument” that plays Happy Birthday when the photoresistor reads a high value of intensity, and then when the knob is turned and the servo arm casts a shadow over the photoresistor the song stops. Then, when the photoresistor detects light again, it starts the song from the beginning.

Here’s my set-up, I used the eraser to elevate my servo!

 

 

 

 

 

 

 

Here’s a demo:

As you can see, my buzzer is lagging which presented another issue that I’m confused about. The durations of the notes are accurate, and I used the concept of the rate that we covered in class which makes me question if moving a servo and playing the buzzer at the same time causes this.

 

For the digital input, I used a switch. If the switch is flipped, it moves the servo arm to cover the photoresistor and doesn’t allow the user to move the arm, turning the music off and locking the device.

Overall, brainstorming adding a lot of interacting elements was a lot of fun for me! I would love to find a way to make my initial idea (where the pitches lower in response to shadow) work, though.

Here’s my circuit diagram and code:

 

 

 

 

 

 

 

#include "pitches.h"
#include <Servo.h>

Servo servo;
//defining photoresistor pin
int photoResistor = A1;
//potentiometer
int potenKnob = A0;
//buzzer pin
int buzzerLoc = 4;
//switch pin
int switchPin = 6;


//array for birthday song notes
int notes[25] = {NOTE_C5, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_F5, NOTE_E5, NOTE_C5, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_G5, NOTE_F5, NOTE_C5, NOTE_C5, NOTE_C6, NOTE_A5, NOTE_F5, NOTE_E5, NOTE_D5, NOTE_AS5, NOTE_AS5, NOTE_A5, NOTE_F5, NOTE_G5, NOTE_F5};
//array for the duration of each note
int durations[25] = {8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 4, 4, 8, 8, 4, 4, 4, 2};

//variable to keep track of which note in the song is playing
int whichNote = 0;


void setup() {
  servo.attach(2);
  Serial.begin(9600);
  pinMode(switchPin, INPUT);

}

void loop() {
  //variable to read the switch state
  int switchState = digitalRead(switchPin);

  //variable that reads from the photoresistor
  int prVal = analogRead(photoResistor);
  //variable to check previous photoresistor value
  int prevPrVal;
  //reading the potentiometer location
  int knobValue = analogRead(potenKnob);
  //mapping to 180 cause it's being used to move the servo
  int mappedKnobValue = map(knobValue, 0, 1023, 0, 180);

  //moving the servo
  servo.write(mappedKnobValue);

  //rate at which the music is meant to play, settled 600 after trial and error
  int rate = 600 / durations[whichNote];


  //condition that checks if servo arm is above photoresistor
  if (prVal >= 800) {
  //condition that checks if the photoresistor was previously covered, so the song starts over when the arm moves, rather than resuming from the middle
    if (prevPrVal < 800) {
      whichNote = 0; //back to beginning
    }

    if (millis() % rate == 0) { //condition to move through each note
      tone(buzzerLoc, notes[whichNote], rate); //plays the note by referring to the note from the array
      whichNote = (whichNote + 1) % 25; //to loop through the song
      delay(1);

    }



  }
  //condition that checks digital input from switch,
  if (switchState == HIGH) { //if the switch is flipped, the instrument is "locked" and no one can play the song by moving the potentiometer knob
    servo.writeMicroseconds(2100); //writing a value to the servo to move it digitally
  }
  prevPrVal  = prVal; //setting the current photoresistor reading to the previous variable, to check next time


}

 

 

Intro to IM Week 9: Musical Instruments

IDEA

For this week’s instrument, I wanted to replicate one of the first musical instruments that I have ever learned: a piano. When I first learned the piano at a young age, the first few pieces of music I had learned were “Mary Had a Little Lamb” and “Twinkle Twinkle Little Stars”. Also, I added another scale to play because I remember always playing scales first as a warmup before I played any other pieces. So, I wanted to be able to replicate me playing these two pieces of music and the scale with my assignment.

MAIN DIFFICULTIES & ADJUSTMENTS

Originally,  I decided to make my button  be able to switch between the different pieces to play at a random order and to simply play the notes in each piece with the twist of the potentiometer.

However, I thought of a major problem which is that every time I press the button, there’s a possibility that it won’t switch to another piece of music to be played by landing on the same piece again due to the random factor. Therefore, I decided to make an index where whenever the button was pressed, it’ll add to the index with it being no greater than 2 (because there’s only three pieces of music in total, 0 being first, 1 being second, 2 being the third piece). By doing, this, I can guarantee that it will not repeat the same piece of music to be played but also let me be clearer upon what I’m going to be playing.

Another major problem I encountered was that I was unable to hear repeated notes from the piazo disk clearly most likely because the loop  was causing it to be played too fast for me to clearly hear that it was two notes.

Mary Had A Little LambTwinkle Twinkle Little Star

To resolve this problem, I added another button to help create the repeated notes by making it play when pressed and not when not pressed.

SETUP

CODE 

int prevButtonState = LOW;
int randomNum;

#include "pitches.h"
// tere are 3 different sets of arrays consisting of 10 different notes
int notesIndex = 0;
int notes[3][10] = { {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5}, {NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_G4 }, {NOTE_C4,NOTE_G4,NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4 }};
const int button = 5;
const int button2 =  3;
const int speaker = 4;
const int durations[10];



void setup() {
  Serial.begin(9600);
}

void loop() {
  buttonpress();
  if ( digitalRead(button2) == HIGH) {
    knobturn();
  }else{
    noTone(speaker);
  }
}

void buttonpress() { // whenever the button's pressed, it switches to a different array of notes
  int currentbuttonstate = digitalRead(button);
  if (currentbuttonstate == HIGH && prevButtonState == LOW) {
    if ( notesIndex < 2) { // if notes index is less than 2
      notesIndex++;
    } else {
      notesIndex = 0; //go back to first array of notes
    }
    //    randomNum = random(1, 4); //pick a random number between 1-3, 1 for an array of notes, 2 for another array, 3 for another array
    Serial.println(notesIndex);
  }
  prevButtonState = currentbuttonstate;
}



void knobturn() {
  int knob = analogRead(A0);
  Serial.println(knob);
  //  tone(4, notes[val]);
  //  int rate = 1000 / durations[whichNote];

  //change the values of the knob to 0 to 9, corresponding to the number of notes in the array
  if (notesIndex == 0) {
    knob = map(knob, 0, 1023, 0, 9);
    tone(speaker, notes[0][knob]);
  }
  // if random number is 2, play the notes 2 array of notes according to the knob
  if (notesIndex == 1) {
    knob = map(knob, 0, 1023, 0, 8);
    tone(speaker, notes[1][knob]);
  }

  if (notesIndex == 2) {
    knob = map(knob, 0, 1023, 0, 8);
    tone(speaker, notes[2][knob]);
  }
}

/*************************************************
 
 * Public Constants
 
 *************************************************/
 
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

FINAL PRODUCT  (how do I include the youtube video?)

https://youtu.be/9z_CXOcHvvM

WK9 : Meera

To celebrate the early  spirit of Christmas, I wanted to make the buzzer play a Christmas tune. The tune plays  by pressing  the button ( switch ). I added , also, a Knob ( potentiometer) to control the volume of the tune. I struggled a lot with the Knob , I thought that the way I connected it was right because I used the example specified for the buzzer and knob but it turned out that their is a different way to connect it when a button is involved. Also the the code was difficult to figure out  but once that was sorted I was finally able to finish this project since connecting the buzzer to the button was very simple and fun to build.

jingle buzzer

Finally I hope you enjoy this project, it was fun to make:

 

 

 

#define buzzer 9
#define button 7

#include "pitches.h"

#define melodyPin 9

// Jingle Bells

int melody[] = {
  NOTE_E5, NOTE_E5, NOTE_E5,
  NOTE_E5, NOTE_E5, NOTE_E5,
  NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
  NOTE_E5,
  NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
  NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
  NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
  NOTE_D5, NOTE_G5
};

int tempo[] = {
  8, 8, 4,
  8, 8, 4,
  8, 8, 8, 8,
  2,
  8, 8, 8, 8,
  8, 8, 8, 16, 16,
  8, 8, 8, 8,
  4, 4
};




void setup() {// put your setup code here, to run once:
  pinMode ( button, INPUT);

  Serial.begin (9600);
  

}

void loop() {

  if (  digitalRead ( button) == HIGH) {

     Serial.println(" 'Jingle Bells'");
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000 / tempo[thisNote];

      tone(melodyPin, melody[thisNote], noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      tone (melodyPin, 0, noteDuration);

    }

    int knobValue = analogRead (A0);

    byte pwm = map(knobValue, 0, 1023, 0, 255);
    
    Serial.print ( knobValue);
    Serial.print ( "-");
   
    Serial. println ( pwm);

    
    
     analogWrite( tone, pwm);
   
    
    
    
    delay (1);

  } else {
    noTone (buzzer);
  
}
  
  }

 

LDT – Light Dependent Theremin

When I was thinking about what to make for this week’s assignment I was came across a clip from The Big Bang Theory where Sheldon was playing the Theremin.

I theremin is the musical instrument that makes sound based on how electromagnetic fields produced by two antennae are altered by the hand movements between the two.

To mimic this I initially tried to use a Ultrasonic sensor to alter the sound. While this worked it only altered the sound based on the distance of the object[hand] from the sensor, the sound was altered based on only one direction.

I then used a LDR to do the same thing. This allowed me to change the sound by moving a light source around the LDR. This allowed me control the sounds by movement of the light source both horizontally and vertically.

I also added a button that multiplies the value of the photoresistor by 2 when pushed once and then the function gets disabled when pushed again. This yields a siren like effect when the button is repeatedly pushed.

int ldrPin = A0;
int playButton = 7;

bool multiplyFrequency = false;
int prevButtonState = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(ldrPin, INPUT);
  pinMode(playButton, INPUT);
}

void loop() {
  changeState();
  Serial.println(multiplyFrequency);
  //  int ultSonData = getUltraSonicData();
  int ldrData = getLDRData();
  //  int transformed = transformData(related);
  if (multiplyFrequency) {
    tone(4, ldrData * 1.5);
  } else {
    tone(4, ldrData);
  }

}

float getLDRData() {
  return floor(analogRead(ldrPin));
}

float changeState() {
  //  Serial.println(digitalRead(playButton));
  if (prevButtonState == LOW && digitalRead(playButton) == HIGH) {
    multiplyFrequency = !multiplyFrequency;
  }
  prevButtonState = digitalRead(playButton);
}

LDT

 

Week 9: Musical Instrument

IDEA

For this week I tried several thins. First, I wanted to recreate a song I played long, long time ago. Then I focused more on having a somewhat randomly generated melody which can be changed on the go through analog and digital input. I sticked with this idea and kept adding more ideas. Unfortunately, it seemed to be too much at some point and I could not find the error. I also wanted two different melodies to be triggered by two different buttons but for some reason the second button would not connect properly.

PROCESS

For the stage of ideation and prototyping that you can see in the video, holding down the blue button triggers a melody. The speed can be adjusted with the potentiometer. To the arms of the servo, two metal earrings are attached that make another sound when clinging to a glass or to each other. As they also swing a little bit, this adds a variable that is out of Arduino control.

OUTCOME

In the time I committed to this exercise, I did not quite manage to achieve the output I envisioned at the beginning but I enjoyed wandering off and adding new ideas and trying out different things on the go. At first, I also had no idea how to extend the sound making beyond Arduino into a physical level but the little holes in the plastic part that I attached to the Servo served quite well to attach earrings and I think it would be fun to have little metal bells attached to the Servo with little strings and see (rather hear) what that sounds like.

 

 

#include <Servo.h> //include servo (hardware)
#include "pitches.h" //include pitches extrta tab

Servo servo;
int servoPos = 100; //position
int whichNote = 0;
int notes[10] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5};
int durations[10];
int bluebuttonPin = 6;
int redbuttonPin = 9;
//int knob = A0;

void setup() {
  servo.attach(2); //attach to Pin 2
  pinMode(8, OUTPUT); //for blue LED
  pinMode(bluebuttonPin, INPUT); //get button input
  pinMode(redbuttonPin, INPUT);
  Serial.begin(9600);

  //  // set the durations with a random coinflip
  //  for (int i = 0; i < 10; i++) {
  //    int coinFlip = random(2);
  //    if (coinFlip == 0)
  //      durations[i] = knobValue;
  //    else
  //      durations[i] = knobValue/2;
  //  }
}

void loop() {
  int knobValue = analogRead(A0); //get analog input from knob
  map(knobValue, 0, 1023, 3, 8); //mapping knowbvalue to use for durations
  Serial.println (knobValue);

  // set the durations with a random coinflip and analog input from knob
  for (int i = 0; i < 10; i++) {
    int coinFlip = random(2);
    if (coinFlip == 0)
      durations[i] = knobValue;
    else
      durations[i] = knobValue / 2;
  }


  int val = analogRead(A0); //to use knob input
  int bluebuttonState = digitalRead(bluebuttonPin);
  int redbuttonState = digitalRead(redbuttonPin);

  // the rate is 1 second divided by the duration of the note
  int rate = 1000 / durations[whichNote];

  // get the current time
  unsigned long currentTime = millis(); //unsigned long to store long number

  //two different songs for two different buttons
  if (bluebuttonState == HIGH) {
    // trigger a note
    if (currentTime % rate == 0  ) {
      tone(4, notes[whichNote], random(100, 400)); //random within range
      whichNote = random(10);
      delay(1); //seemed to help Aaron
    }

    // do the servo at half speed
    if (currentTime % (rate * 2) == 0  ) {
      servoPos = 30;
      servo.write(servoPos);
    }

    // else if not triggereing the servo, then every 10 milliseconds move the servo arm back a little bit
    // can't do it every frame as that is too fast for the servo
    else if (currentTime % 10 == 0) { //every ten millisec substract 1 from servo position
      servoPos -= 1;
      servo.write(servoPos);
    }

  } else {
    return;
  }

  if (redbuttonState == HIGH) {
    // trigger a note
    if (currentTime % rate == 0  ) {
      tone(4, notes[whichNote], random(100, 400)); //random within range
      whichNote = random(10);
      delay(1); //seemed to help Aaron
    }

    // do the servo at half speed
    if (currentTime % (rate * 2) == 0  ) {
      servoPos = 30;
      servo.write(servoPos);
    }

    // else if not triggereing the servo, then every 10 milliseconds move the servo arm back a little bit
    // can't do it every frame as that is too fast for the servo
    else if (currentTime % 10 == 0) { //every ten millisec substract 1 from servo position
      servoPos -= 1;
      servo.write(servoPos);
    }

  } else {
    return;
  }

}