Assignment 10 – Stranger Things (with David)

Concept

For this project, we thought of recreating a recognizable music theme from film/TV series that we could easily manipulate. We ended up on the eerie and haunting Stranger Things theme, as it is truly one of those few captivating intros that you would never skip. We also decided to dedicate this project to the upcoming final season of the series, which will be released next year.

By using the ultrasonic distance meter and tricolor LED, we made a musical instrument that is responsive to the distance of the hand: the music speeds up when the hand is closer and slows down when it goes farther, and the LED changes its color accordingly.

Sketch

Code Highlight

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState == LOW) { 
for (int thisNote = 0; thisNote < 48; thisNote++) {

// distance measured with ultrasonic sensor

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;

// mapping the tricolor LED values

int redValue = map(distance, 5, 50, 255, 100); 
int greenValue = 0; 
int blueValue = map(distance, 5, 50, 100, 255);

analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);

// mapping to adjust duration between notes

int adjustedNoteDuration = map(distance, 5, 50, StrangerThingsNoteDurations[thisNote] / 2, StrangerThingsNoteDurations[thisNote] * 2);

// playing with the new tempo

tone(buzzerPin, StrangerThingsMelody[thisNote], adjustedNoteDuration);

// a short pause

delay(adjustedNoteDuration * 1.3);
}

} else {

// when button is not pressed, turning off the LED and buzzer

analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
noTone(buzzerPin);
}

delay(50); 
}

It was difficult at first to find the music in the proper format so that it can be implemented into the Arduino code. As we tried to somehow extract the notes from the .mxl (.musicxml) files, we couldn’t find a software that could do that. Hence, we ended up recreating the soundtrack by using the array of sequences (representing the 48 notes and their durations) that we luckily found on the GitHub.

We using mapping for the tricolour LED lights, keeping greenValue at zero so that we could get a somewhat “gradient” effect.

The key (and most challenging) aspect in the code was to speed up the music according to the varied distance. We used mapping to set the shortest and longest duration of the music, as well as implemented  a delay by multiplying the adjustedNoteDuration by 1.3 (a factor commonly used in Arduino to maintain a natural pacing) to add a slight pause between notes, making each note more distinct.

Embedded Sketch

GitHub

Leave a Reply