ORG030

DEEP SEA BIG PINEAPPLE GROUP

Arduino Processing Practice

Remote-Controlled Music Player

Demo Introduction

Utilize the interaction between Processing and Arduino to implement the functionality of a music player. Songs can be controlled for switching and pausing through an infrared remote control. The player’s interface displays information about the music player’s status, and there is a dynamically changing circle at the center that corresponds to the volume of the music.

List of components

  • HX1838 Infrared Wireless Remote Control Kit IR Expansion Module (Remote Control + Receiver Board)

  • potentiometer

Schematic circuit diagram

Arduino code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <IRremote.h>

int RECV_PIN = 11; // Pin for the infrared receiver
IRrecv irrecv(RECV_PIN);
decode_results results;

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

void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}

Processing code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import processing.serial.*;
import ddf.minim.*; // Library for audio processing
color circleColor = color(255); // Variable to store the color of the circle
Minim minim;
AudioPlayer player;
Serial arduino;
float circleSize;
String currentSong = "Please Select a Track"; // Store information about the current song

void setup() {
size(600, 600);
surface.setTitle("Music Player"); // Set window title
arduino = new Serial(this, "COM4", 9600); // Replace COMx with the serial port connected to Arduino
minim = new Minim(this);
player = minim.loadFile("music1.mp3"); // Path to the audio file
}

void draw() {
// Handle serial data from Arduino
while (arduino.available() > 0) {
String data = arduino.readStringUntil('\n');
if (data != null && data.trim().length() > 0) {
try {
long val = Long.parseLong(data.trim(), 16);
changeSong(val);
} catch (NumberFormatException e) {
println("Error parsing hex value: " + data.trim());
}
}
}

background(255);
// Display information about the currently playing song
textAlign(CENTER);
textSize(32);
fill(0);
text(currentSong, width / 2, height / 2 - 150);

// Draw a circle based on the volume level
circleSize = map(player.mix.level(), 0, 1, 50, 500);
fill(circleColor);
ellipse(width / 2, height / 2, circleSize, circleSize);
}

void changeSong(long key) {
// Switch or stop the song based on serial information
if (key == 0xFF30CF) { // Button 1
player.close();
player = minim.loadFile("music1.mp3");
player.play();
circleColor = color(135, 206, 250); // Circle color is sky blue
currentSong = "Currently Playing\nCastle in the Sky";
} else if (key == 0xFF18E7) { // Button 2
player.close();
player = minim.loadFile("music2.mp3");
player.play();
circleColor = color(0, 0, 139); // Circle color is dark blue
currentSong = "Currently Playing\nThe Rain";
} else if (key == 0xFF7A85) { // Button 3
player.close();
player = minim.loadFile("music3.mp3");
player.play();
circleColor = color(255, 255, 224); // Circle color is light yellow
currentSong = "Currently Playing\nMerry Go Round";
} else if (key == 0xFFA25D) { // Stop button
player.close(); // Stop the music
circleColor = color(255, 255, 255); // Circle color reset to white
currentSong = "Please Select a Track";
}
}

Run result

Reference

Arduino基础入门之九 红外遥控

Processing 入门教程(三十四) Audio 音频播放