Design Challenge: Make a device to control playback of pre-recorded music.
Parameters:
- Make a device that sends asynchronous serial messages to another device which will play the music.
- All serial messages should be terminated by a linefeed.
- The playback device will echo your message back to you as an acknowledgement message when it executes your command.
The device supports the following features:
- It is operable by a user who cannot see the device.
- The user gets an acknowledgement when they activate any control.
- Start or stop the playback of a track (start plays track from the beginning).
- Pause or resume playback of a track (resume plays track from last position stopped)
- Skip ahead one track in the playlist.
- Skip back one track in the playlist.
- Fast-forward the current track (double speed of the track).
- Skip to a random track in the playlist.
Tools used:
- Arduino IDE
- Arduino MKR1000
- Python server
- P5.js
- P5 Serial Control App
- Buttons (momentary and )
- Vibration Sensor
- Frosted plastic container
I started this assignment by wiring my components to the breadboard and Arduino. As shown below, I wired the 2 toggle buttons, 4 momentary buttons and the vibration sensor.
Arduino Code:
This proved to be quite tricky for me initially. My goal was to create the code from scratch and not rely on references. I encountered errors with the switch state where the serial monitor was just writing the value continually. Adding a delay did not help. I eventually discovered that my syntax was wrong (odd that the IDE did not pick up on this when I verified the code.) The culprit was a semi colon.
const int startStop = 11;
const int pausePlay = 10;
const int next = 9;
const int back = 8;
const int randomSong = 7;
const int fast = 6;
int vibeSensor = 5;
int lastStartStopState = LOW;
int lastPausePlayState = HIGH;
int lastNextState = LOW;
int lastBackState = LOW;
int lastRandomState = LOW;
int lastFastState = LOW;
void setup() {
Serial.begin(9600);
pinMode(startStop, INPUT_PULLUP);
pinMode(pausePlay, INPUT_PULLUP);
pinMode(next, INPUT_PULLUP);
pinMode(back, INPUT_PULLUP);
pinMode(randomSong, INPUT_PULLUP);
pinMode(fast, INPUT_PULLUP);
pinMode(vibeSensor, OUTPUT);
}
void loop() {
int buttonState1 = digitalRead(startStop);
int buttonState2 = digitalRead(pausePlay);
int buttonState3 = digitalRead(next);
int buttonState4 = digitalRead(back);
int buttonState5 = digitalRead(randomSong);
int buttonState6 = digitalRead(fast);
// 1. Start Stop Button
// If the button is on, send a message to serial to start playing the song
if (buttonState1 != lastStartStopState) {
if (buttonState1 == HIGH) {
Serial.println(“start”);
Serial.write(49);
analogWrite(vibeSensor, 255); // give feedback
analogWrite(vibeSensor, 0);
delay(100);
// If the button is off, send a message to serial to stop playing the song
} else {
Serial.println(“stop”);
Serial.write(49);
//
analogWrite(vibeSensor, 255); // give feedback
analogWrite(vibeSensor, 0);
delay(100);
}
lastStartStopState = buttonState1;
}
// 2. Pause Play Button
//If the button is on, send a message to serial to pause the song
if (buttonState2 != lastPausePlayState) {
if (buttonState2 == LOW) {
Serial.println(“pause”);
Serial.write(50);
delay(500);
analogWrite(vibeSensor, 255); // give feedback
delay(100);
analogWrite(vibeSensor, 0);
// If the button is off, send a message to serial to play the song
} else {
Serial.println(“play”);
Serial.write(50);
analogWrite(vibeSensor, 255); // give feedback
delay(100);
analogWrite(vibeSensor, 0);
}
}
lastPausePlayState = buttonState2;
//3. Next Button (skip to the next song)
// If the button is pressed, send a message to serial to skip forward to the next song
if (buttonState3 != lastNextState) {
if (buttonState3 == LOW) {
Serial.println(“next”);
Serial.write(51);
// delay(500);
analogWrite(vibeSensor, 255); // give feedback
delay(100);
analogWrite(vibeSensor, 0);
}
lastNextState = buttonState3;
}
// 4. Back Button (go back and play the previous song)
// If the button is on, send a message to serial to skip to the previous song
if (buttonState4 != lastBackState) {
if (buttonState4 == LOW) {
Serial.println(“back”);
Serial.write(52);
// delay(500);
analogWrite(vibeSensor, 255); // give feedback
delay(100);
analogWrite(vibeSensor, 0);
}
lastBackState = buttonState4;
}
// 5. Random Button (play a random song)
// If the button is on, send a message to serial to skip to a random song
if (buttonState5 != lastRandomState) {
if (buttonState5 == LOW) {
Serial.println(“random”);
Serial.write(53);
// delay(500);
analogWrite(vibeSensor, 255); // give feedback
delay(100);
analogWrite(vibeSensor, 0);
}
lastRandomState = buttonState5;
}
// 6. Fast Button (fast forward through the current song)
// If the button is on, send a message to serial to fast forward through the current song
if (buttonState6 != lastFastState) {
if (buttonState6 == LOW) {
Serial.println(“fast”);
Serial.write(54);
// delay(500);
analogWrite(vibeSensor, 255); // give feedback
delay(100);
analogWrite(vibeSensor, 0);
}
lastFastState = buttonState6;
}
}
P5.js and Serial Communication
I haven’t touched p5 since ICM. However, setting up the serial communication and updating the sample p5 code was the most straightforward part of this assignment.
Using this serial input to P5 tutorial, I installed the P5.serialcontrol app and the p5.serialserver. With the arduino connected to the port, I used the serial control app to ensure the port was available. Then using the command line, opened a python server the tested my controller.
P5 code:
var serial;
var portName = ‘/dev/cu.usbmodem1411’;
var inData;
var song; // the sound file to be played
// the list of songs:
var songs = [‘FastWine.mp3’, ‘FullExtreme.mp3’, ‘Incredible.mp3’, ‘LeaveMeAlone.mp3’, ‘TurnUp.mp3’];
var songCount = songs.length; // number of songs in the music dir
var currentSong = 0; // current song number
function preload() { // load the first song on preload
song = loadSound(‘music/’ + songs[currentSong]);
}
function setup() {
createCanvas(400, 300);
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on(‘connected’, serverConnected); // callback for connecting to the server
serial.on(‘open’, portOpen); // callback for the port opening
serial.on(‘data’, serialEvent); // callback for when new data arrives
serial.on(‘error’, serialError); // callback for errors
serial.on(‘close’, portClose); // callback for the port closing
serial.list(); // list the serial ports
serial.open(portName); // open a serial port
}
function serverConnected() {
println(“We are connected!”);
}
function portOpen() {
println(“Serial Port is open!”);
}
function serialEvent() {
inData = Number(serial.read());
controlSound(inData);
console.log(inData)
}
function serialError(err) {
println(‘Something went wrong with the serial port ‘ + err);
}
function portClose() {
println(‘The serial port is closed’);
}
function draw() {
background(30, 20, 180);
fill(255);
text(“sensor value: ” + inData, 30, 30);
// draw the song’s name and current time in seconds:
text(songs[currentSong], 20, 50);
text(song.currentTime().toFixed(3), 20, 100);
}
function controlSound(input) {
switch (input) {
case 49: // start/stop, press 1
if (song.isPlaying()) {
song.stop();
} else {
song.play();
}
break;
case 50: // play/pause, press 2
if (song.isPlaying()) {
song.pause();
} else {
song.play();
}
break;
case 51: // skip ahead, press 3
// make sure the song number is valid, and increment:
if (currentSong < songs.length - 1) {
currentSong++;
} else {
currentSong = 0;
}
// get new song:
getSong(currentSong);
break;
case 52: // skip back, press 4
// in the first second, just rewind the current track:
if (song.currentTime() > 1.0) {
song.jump(0);
// if more than a second has elapsed, then
// make sure the song number is valid, and decrement:
} else {
if (currentSong > 0) {
currentSong–;
} else {
currentSong = songs.length – 1;
}
// get new song:
getSong(currentSong);
}
break;
case 53: // fast forward, press 5
song.rate(2.0); // double the play speed
if (!song.isPlaying()) {
song.play();
}
break;
case 54: // random song, press 6
currentSong = Math.round(random(songCount)); // get a new song number
getSong(currentSong); // play it
break;
}
}
function getSong(songNumber) {
if (songNumber < songs.length) { // if the song number is in range
if (song.isPlaying()) {
song.stop();
}
// load a new song:
song = loadSound(‘music/’ + songs[currentSong], resumePlay);
return true;
} else { // if the song number was out of range, return false
return false;
}
}
function resumePlay() {
// if the song isn’t playing, play it
if (song.isPlaying()) {
song.stop();
} else {
song.play();
}
}
function keyReleased() {
controlSound(keyCode); // send the ASCII number of the key
}
Experience new music
So, that was soca. In case you were wondering what I was playing. Take a listen to the tracks on my playlist, below.
Like!! I blog frequently and I really thank you for your content. The article has truly peaked my interest.
I really like and appreciate your blog post.
These are actually great ideas in concerning blogging.
Thanks for fantastic info I was looking for this info for my mission.
Your site is very helpful. Many thanks for sharing!
dapoxetine too expensive https://salemeds24.wixsite.com/dapoxetine
cheap careprost from india https://carepro1st.com/
hydroxychloroquine patient reviews https://hhydroxychloroquine.com/
naltrexone low dose cost https://naltrexoneonline.confrancisyalgomas.com/
take ivermectin before drinking alcohol https://ivermectin.mlsmalta.com/
hydroxychloroquine tab 200mg https://hydroxychloroquine.webbfenix.com/
where to order vidalista online safe https://vidalista40mg.mlsmalta.com/
priligy price in india https://ddapoxetine.com/
how much is nolvadex cost https://tamoxifen.mrdgeography.com/
cialis how to use https://wisig.org/
amstyles.com https://amstyles.com/
is chloroquine effective for covid https://hydroxychloroquinee.com/
cialis cost at cvs pharmacy https://tadalafil.cleckleyfloors.com/
uptodate doxycycline http://doxycycline.zolftgenwell.org/
what is prednisone for dogs https://prednisone.bvsinfotech.com/
hydroxychloroquine plaquenil https://hydroxychloroquine.wisig.org/
health warehouse pharmacy https://edmeds.buszcentrum.com/
tadalafil 5mg tab https://cialzi.com/
can prednisone shrink tumors https://bvsinfotech.com/
cenforce dosage mg http://cavalrymenforromney.com/
You can certainly see your expertise in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always go after your heart.
I precisely desired to say thanks again. I’m not certain the things I could possibly have achieved in the absence of the concepts contributed by you about my area of interest. Entirely was a daunting circumstance for me, but coming across a expert avenue you dealt with it forced me to cry for contentment. Extremely grateful for the advice and expect you are aware of a powerful job that you are accomplishing educating many people all through a site. I’m certain you’ve never come across all of us.
I’m also commenting to let you be aware of of the brilliant discovery my child encountered checking the blog. She came to understand a lot of details, including how it is like to have a marvelous coaching character to get certain people completely comprehend a variety of complex issues. You actually did more than her desires. I appreciate you for coming up with such essential, dependable, edifying and cool guidance on that topic to Ethel.
how much viagra to take https://viaplz.com/
Regards for all your efforts that you have put in this. very interesting info .
hydroxychloroquine and coronavirus https://hydroxychloroquine4u.com/
cialis vs viagra costs https://cialis.bee-rich.com/
cialis price per pill https://cialis.stdstory.com/
This is a topic that is close to my heart… Many thanks!
Where are your contact details though? http://antiibioticsland.com/Bactrim.htm
cialis http://cialis.anafassia.com/
How to upload a wordpress backup on to a site?
How to uninstall firefox using command prompt or from registry?
http://cse.google.tm/url?sa=i&url=https://hornyporns.com/
Keep working ,fantastic job! https://chwilowki-pozyczka.pl – chwilówki online
Awesome things here. I am very happy to look your article.
Thanks so much and I am taking a look ahead to touch you.
Will you please drop me a mail? https://cialis.studiowestinc.com/tadalafil
super content, i love it
perfect post
Howdy! I’m at work surfing around your blog from
my new iphone! Just wanted to say I love reading your blog and look forward to all your
posts! Carry on the outstanding work! http://herreramedical.org/acyclovir
very good article
best suited post, i love it
greatest content, i love it
generic drugs without doctor’s prescription buy cheap prescription drugs online
generic pills for ed
over the counter viagra 100mg viagra
online doctor prescription for viagra
viagra from canada viagra without a doctor prescription usa
buying viagra online
where to buy viagra viagra cost
over the counter viagra
best place to buy generic viagra online best over the counter viagra
viagra from canada
viagra price buying viagra online
viagra discount
stromectol cost https://ivermectin1st.com/
greatest article
clindamycin generic: buy chloramphenicol online
flagyl generic
best online international pharmacies india: india pharmacies online order medications online from india
best india pharmacy: india pharmacy mail order generic pills india
best male erectile dysfunction pill: https://edpillsonline24.com/# levitra pills
erectile pills canada: https://edpillsonline24.com/# generic viagra pills
https://www.ekffo150.com 우리카지노사이트
https://www.ajp4949.com/theking 더킹카지노
https://www.ajp4949.com/merit 메리트카지노
https://www.ajp4949.com/sands 샌즈카지노
https://www.ajp4949.com/first 퍼스트카지노
https://www.ajp4949.com/33casino 33카지노
https://www.ajp4949.com/worldcasino 월드카지노
https://www.ajp4949.com/korea 코리아카지노
erectile pills canada: https://edpillsonline24.com/# best male erectile dysfunction pill
Hello every one, here every person is sharing these knowledge, so it’s
pleasant to read this web site, and I used to pay
a quick visit this webpage everyday. 0mniartist asmr
viagra generic: viagra without a doctor prescription canada non prescription viagra
how to get viagra without a doctor: viagra without a prescription no prescription viagra
At this moment I am going away to do my breakfast, when having
my breakfast coming over again to read additional news.
0mniartist asmr
where to buy diflucan pills: buy diflucan – diflucan price south africa
A fascinating discussion is definitely worth comment.
I believe that you should write more on this subject, it may
not be a taboo matter but usually folks don’t speak about such issues.
To the next! All the best!! 0mniartist asmr
viagra for sale
Way cool! Some extremely valid points! I appreciate you penning this write-up plus the rest of the site
is also really good. 0mniartist asmr
doxycycline 150 mg: doxycycline antibiotic – generic for doxycycline
Hi, of course this piece of writing is really nice and I have learned lot of things from it concerning blogging.
thanks. asmr 0mniartist
meet singles
senior dating site
clomiphene generic: buy clomid – cheap clomid