Article Catalog

  • Arduino Big Job Experiment (Original Version)
  • Arduino Big Work Experiment (Advanced Version)
  • Overall Code for Final Experiments


Arduino Big Job Experiment (Original Version)

Color sensor

The color sensor is used to identify the painting disc for 1s at a time, and its rgb value corresponds to the corresponding musical style (for example, red is defined as the passionate musical style). Use the switch function to remove the music clip from the sd card.

Code:

Read Color Functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void readColor() {//从传感器获取数据 
//红色:
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redFrequency = pulseIn(sensorOut, LOW); // 读取红色频率
redColor = map(redFrequency, redMin, redMax, 255, 0); // 映射红色值
Serial.println(redColor);
delay(100);
//绿色:
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenFrequency = pulseIn(sensorOut, LOW); // 读取绿色频率
greenColor = map(greenFrequency, greenMin, greenMax, 255, 0); // 映射绿色值
Serial.println(greenColor);
delay(100);
//蓝色:
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueFrequency = pulseIn(sensorOut, LOW); // 读取蓝色频率
blueColor = map(blueFrequency, blueMin, blueMax, 255, 0); // 映射蓝色值
Serial.println(blueColor);
delay(100);
}

The color corresponds to the melody function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void decideColor() {
// 格式化颜色值:
redColor = constrain(redColor, 0, 255);
greenColor = constrain(greenColor, 0, 255);
blueColor = constrain(blueColor, 0, 255);

// 判断颜色:
if (redColor > 250 && greenColor > 250 && blueColor > 250) {
color = 1; // 白色
} else if (redColor < 50 && greenColor < 50 && blueColor < 50) {
color = 2; // 黑色
} else if (redColor > 230 && greenColor > 200 && blueColor < 160) {
color = 3; // 黄色
} else if (redColor > 200 && greenColor < 120 && blueColor < 150) {
color = 4; // 红色
} else if (redColor > 175 && greenColor > 200 && blueColor > 175) {
color = 5; // 绿色
} else if (redColor > 155 && greenColor < 200 && blueColor < 200) {
color = 6; // 蓝色
} else {
color = 0; // 未知
}
}

Calibration Color Function

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
void calibrate() {
Serial.println("Calibrating..."); // 校准中...
Serial.println("White"); // 请将传感器对准白色物体
// 设置校准值:
digitalWrite(13, HIGH); // 点亮内置LED
delay(2000); // 延时
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redMin = pulseIn(sensorOut, LOW); // 读取红色最小值
delay(100);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenMin = pulseIn(sensorOut, LOW); // 读取绿色最小值
delay(100);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueMin = pulseIn(sensorOut, LOW); // 读取蓝色最小值
delay(100);
Serial.println("next..."); // 接下来,请对准黑色物体
digitalWrite(13, LOW);
delay(2000);
Serial.println("Black"); // 黑色校准
// 设置校准值:
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redMax = pulseIn(sensorOut, LOW); // 读取红色最大值
delay(100);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenMax = pulseIn(sensorOut, LOW); // 读取绿色最大值
delay(100);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueMax = pulseIn(sensorOut, LOW); // 读取蓝色最大值
delay(100);
Serial.println("Done calibrating."); // 校准完成
digitalWrite(13, LOW); // 熄灭内置LED
}


mp3 module

Store the music corresponding to different colors in the sd card, named 1,2,3,4,5,6. And use the switch statement to call the color in it.

Code:

Play Music Functions

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
oid playMusic() {
// 根据颜色播放音乐
switch (color) {
case 1: // 白色
Serial.println("情绪:纯净");
myDFPlayer.play(1); // whitesong
break;
case 2: // 黑色
Serial.println("情绪:沉默");
myDFPlayer.play(2); // blacksong
break;
case 3: // 黄色
Serial.println("情绪:活力");
myDFPlayer.play(3); // yellowsong
break;
case 4: // 红色
Serial.println("情绪:热情");
myDFPlayer.play(4); // redsong
break;
case 5: // 绿色
Serial.println("情绪:愉悦");
myDFPlayer.play(5); // greensong
break;
case 6: // 蓝色
Serial.println("情绪:平和");
myDFPlayer.play(6); // bluesong
break;
default:
Serial.println("未知颜色");
break;
}
}

Steering gear section

The steering gear part uses a 180 degree SG90 motor and a 360 degree SG90 motor, and SG90180 controls the rotation of the rotor arm. The SG90 360 controls the painting to rotate in order to achieve color recognition of the entire disk

Vocal part

Use the buzzer

Integral 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <Servo.h>

// Arduino引脚定义:
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6
#define switchPin1 7
Servo myservo;
// 来自传感器的输出:
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;
// 格式化的颜色值:
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
// 用于校准的值
int redMin;
int redMax;
int greenMin;
int greenMax;
int blueMin;
int blueMax;
int color = 0;

// DFPlayer Mini相关设置
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

void setup() {
// 初始化Arduino引脚
pinMode(switchPin1, INPUT_PULLUP);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
myservo.attach(7);
pinMode(sensorOut, INPUT);

// 初始化DFPlayer Mini
mySoftwareSerial.begin(9600);
Serial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println("DFPlayer Mini 初始化失败!");
while (true);
}
myDFPlayer.volume(30); // 设置音量 (0~30)

// 将频率缩放设置为20%:
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
calibrate(); // 校准传感器
}

void loop() {

myservo.write(0);
delay(2000);
myservo.write(90);
delay(500);
readColor(); // 读取颜色
decideColor(); // 决定颜色和情绪
playMusic(); // 播放音乐
delay(1000); // 每秒检测一次
}


void readColor() {//从传感器获取数据
//红色:
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redFrequency = pulseIn(sensorOut, LOW); // 读取红色频率
redColor = map(redFrequency, redMin, redMax, 255, 0); // 映射红色值
Serial.println(redColor);
delay(100);
//绿色:
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenFrequency = pulseIn(sensorOut, LOW); // 读取绿色频率
greenColor = map(greenFrequency, greenMin, greenMax, 255, 0); // 映射绿色值
Serial.println(greenColor);
delay(100);
//蓝色:
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueFrequency = pulseIn(sensorOut, LOW); // 读取蓝色频率
blueColor = map(blueFrequency, blueMin, blueMax, 255, 0); // 映射蓝色值
Serial.println(blueColor);
delay(100);
}

void decideColor() {
// 格式化颜色值:
redColor = constrain(redColor, 0, 255);
greenColor = constrain(greenColor, 0, 255);
blueColor = constrain(blueColor, 0, 255);

// 判断颜色:
if (redColor > 250 && greenColor > 250 && blueColor > 250) {
color = 1; // 白色
} else if (redColor < 50 && greenColor < 50 && blueColor < 50) {
color = 2; // 黑色
} else if (redColor > 230 && greenColor > 200 && blueColor < 160) {
color = 3; // 黄色
} else if (redColor > 200 && greenColor < 120 && blueColor < 150) {
color = 4; // 红色
} else if (redColor > 175 && greenColor > 200 && blueColor > 175) {
color = 5; // 绿色
} else if (redColor > 155 && greenColor < 200 && blueColor < 200) {
color = 6; // 蓝色
} else {
color = 0; // 未知
}
}

void calibrate() {
Serial.println("Calibrating..."); // 校准中...
Serial.println("White"); // 请将传感器对准白色物体
// 设置校准值:
digitalWrite(13, HIGH); // 点亮内置LED
delay(2000); // 延时
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redMin = pulseIn(sensorOut, LOW); // 读取红色最小值
delay(100);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenMin = pulseIn(sensorOut, LOW); // 读取绿色最小值
delay(100);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueMin = pulseIn(sensorOut, LOW); // 读取蓝色最小值
delay(100);
Serial.println("next..."); // 接下来,请对准黑色物体
digitalWrite(13, LOW);
delay(2000);
Serial.println("Black"); // 黑色校准
// 设置校准值:
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redMax = pulseIn(sensorOut, LOW); // 读取红色最大值
delay(100);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenMax = pulseIn(sensorOut, LOW); // 读取绿色最大值
delay(100);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueMax = pulseIn(sensorOut, LOW); // 读取蓝色最大值
delay(100);
Serial.println("Done calibrating."); // 校准完成
digitalWrite(13, LOW); // 熄灭内置LED
}

void playMusic() {
// 根据颜色播放音乐
switch (color) {
case 1: // 白色
Serial.println("情绪:纯净");
myDFPlayer.play(1); // whitesong
break;
case 2: // 黑色
Serial.println("情绪:沉默");
myDFPlayer.play(2); // blacksong
break;
case 3: // 黄色
Serial.println("情绪:活力");
myDFPlayer.play(3); // yellowsong
break;
case 4: // 红色
Serial.println("情绪:热情");
myDFPlayer.play(4); // redsong
break;
case 5: // 绿色
Serial.println("情绪:愉悦");
myDFPlayer.play(5); // greensong
break;
case 6: // 蓝色
Serial.println("情绪:平和");
myDFPlayer.play(6); // bluesong
break;
default:
Serial.println("未知颜色");
break;
}
}

Wiring diagram



Problems encountered:

  1. The sound quality of the buzzer is not good
  2. 360 degrees of the steering gear corresponds to the speed, and the rotation of the steering gear cannot be controlled at a constant speed. Even using a for loop can only increase and decrease its speed.
  3. No switch control, start to rotate when powered on.
  4. Poor experimental effect


Arduino Big Work Experiment (Advanced Version)

Improvement of the original effect: the color of the painting plate varies with the drawing of different people, and there is randomness. As a result, the melody of the song is confused and the listening feeling is not good. To this end, we decided to replace the song fragments that correspond to different colors with different sound effects (such as tapping, tapping, and drumming). It is applied to the beat of a relatively universal basic bgm, and the recognized delay number corresponds to the number of beats.

Double track implementation
Then use an mp3 module to store bgm.

Improvement of steering gear
Replace the 360-degree steering with a stepper motor

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 引入Stepper库
#include <Stepper.h>

// 定义步进电机的步数和引脚连接
const int stepsPerRevolution = 2048; // 步进电机一圈的步数
const int motorPin1 = A0; // IN1连接到A0
const int motorPin2 = A1; // IN2连接到A1
const int motorPin3 = A2; // IN3连接到A2
const int motorPin4 = A3; // IN4连接到A3

// 创建Stepper对象
Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2, motorPin3, motorPin4);

void setup() {
// 设置步进电机的速度
myStepper.setSpeed(5); // 5 RPM
}

void loop() {
// 正向旋转360度
myStepper.step(stepsPerRevolution);
delay(1000); // 延迟1秒
}


The switch is triggered
When the switch is pressed and released, the uno controls the rotation of the SG90 servo and the rotation of the stepper motor.

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
#include <Servo.h>
#include <Stepper.h>
Servo myServo; // 创建一个Servo对象// 创建Stepper对象
int switchPin = 7; // 设置开关连接的数字引脚为2
int switchState = 0; // 用于存储开关状态
int servoPosition = 0; // 用于存储舵机当前位置
const int stepsPerRevolution = 2048; // 步进电机一圈的步数
const int motorPin1 = A0; // IN1连接到A0
const int motorPin2 = A1; // IN2连接到A1
const int motorPin3 = A2; // IN3连接到A2
const int motorPin4 = A3; // IN4连接到A3
Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2, motorPin3, motorPin4);
void setup() {
myServo.attach(9); // 将舵机连接到Arduino的数字引脚9
pinMode(switchPin, INPUT); // 将开关引脚设置为输入模式
// 设置步进电机的速度
myStepper.setSpeed(5); // 5 RPM
}

void loop() {
switchState = digitalRead(switchPin); // 读取开关状态

if (switchState == HIGH) {
// 如果开关被按下
while (switchState == HIGH) {
switchState = digitalRead(switchPin); // 检测开关是否释放
delay(20); // 等待一小段时间,避免检测到多次按下
}
// 正向旋转360度
myStepper.step(stepsPerRevolution);
delay(1000); // 延迟1秒
// 开关释放后,开始从0度到90度缓慢旋转
for (int angle = 0; angle <= 180; angle += 1) {
myServo.write(angle); // 将舵机旋转到指定角度
delay(20); // 调整延迟以控制旋转速度
}
delay(1000); // 在旋转到180度后等待1秒

for (int angle = 180; angle >= 0; angle -= 1) {
myServo.write(angle); // 将舵机旋转到指定角度
delay(20); // 调整延迟以控制旋转速度
}
}
}

Wiring diagram:

Problem
Uno is not able to control servo servos and stepper motors in parallel, and the two are sequential, and experiments require them to run in parallel.
Ways to improve
Use two UNO boards for control and connect in parallel with switches

Buzzer replaced with power amplifier module
Precautions
In the previous experiment, the pins of the buzzer were connected to the SPK_1 and SPK_2 ports of the MP3 module. If you want to use the power horn module, you need to connect DAC_R or DAC_I port.

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
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

void setup() {
Serial.begin(115200);
mySoftwareSerial.begin(9600);

if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println("Unable to begin:");
Serial.println("1.Please recheck the connection!");
Serial.println("2.Please insert the SD card!");
while (true);
}

myDFPlayer.volume(30); // 设置音量 (0~30)

// 播放第1首歌曲
playMP3(1);
}

void loop() {
// 在这里可以添加其他循环代码
}

void playMP3(uint16_t trackNumber) {
Serial.print("Playing MP3 Track: ");
Serial.println(trackNumber);

// 使用 play() 函数播放指定编号的 MP3 文件
myDFPlayer.play(trackNumber);

// 等待播放完成
while (myDFPlayer.readState() == 1) {
delay(100);
}

Serial.println("MP3 finished");
}


Overall Code for Final Experiments

UNO board 1

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <Servo.h>

// Arduino引脚定义:
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6
#define switchPin1 7
Servo myservo;
// 来自传感器的输出:
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;
// 格式化的颜色值:
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
// 用于校准的值
int redMin;
int redMax;
int greenMin;
int greenMax;
int blueMin;
int blueMax;
int color = 0;

// DFPlayer Mini相关设置
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

void setup() {
// 初始化Arduino引脚
pinMode(switchPin1, INPUT_PULLUP);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
myservo.attach(8);
pinMode(sensorOut, INPUT);

// 初始化DFPlayer Mini
mySoftwareSerial.begin(9600);
Serial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println("DFPlayer Mini 初始化失败!");
while (true);
}
myDFPlayer.volume(30); // 设置音量 (0~30)

// 将频率缩放设置为20%:
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
calibrate(); // 校准传感器
}

void loop() {
switchState = digitalRead(switchPin); // 读取开关状态

if (switchState == HIGH) {
// 如果开关被按下
while (switchState == HIGH) {
switchState = digitalRead(switchPin); // 检测开关是否释放
delay(20); // 等待一小段时间,避免检测到多次按下
}
// 开关释放后,开始从0度到90度缓慢旋转
for (int angle = 0; angle <= 180; angle += 10) {
myServo.write(angle); // 将舵机旋转到指定角度
readColor(); // 读取颜色
decideColor(); // 决定颜色和情绪
playMusic(); // 播放音乐
delay(2000); // 调整延迟以控制旋转速度
}
delay(1000); // 在旋转到180度后等待1秒

for (int angle = 180; angle >= 0; angle -= 1) {
myServo.write(angle); // 将舵机旋转到指定角度
delay(20); // 调整延迟以控制旋转速度
}
}
}


void readColor() {//从传感器获取数据
//红色:
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redFrequency = pulseIn(sensorOut, LOW); // 读取红色频率
redColor = map(redFrequency, redMin, redMax, 255, 0); // 映射红色值
Serial.println(redColor);
delay(100);
//绿色:
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenFrequency = pulseIn(sensorOut, LOW); // 读取绿色频率
greenColor = map(greenFrequency, greenMin, greenMax, 255, 0); // 映射绿色值
Serial.println(greenColor);
delay(100);
//蓝色:
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueFrequency = pulseIn(sensorOut, LOW); // 读取蓝色频率
blueColor = map(blueFrequency, blueMin, blueMax, 255, 0); // 映射蓝色值
Serial.println(blueColor);
delay(100);
}

void decideColor() {
// 格式化颜色值:
redColor = constrain(redColor, 0, 255);
greenColor = constrain(greenColor, 0, 255);
blueColor = constrain(blueColor, 0, 255);

// 判断颜色:
if (redColor > 250 && greenColor > 250 && blueColor > 250) {
color = 1; // 白色
} else if (redColor < 50 && greenColor < 50 && blueColor < 50) {
color = 2; // 黑色
} else if (redColor > 230 && greenColor > 200 && blueColor < 160) {
color = 3; // 黄色
} else if (redColor > 200 && greenColor < 120 && blueColor < 150) {
color = 4; // 红色
} else if (redColor > 175 && greenColor > 200 && blueColor > 175) {
color = 5; // 绿色
} else if (redColor > 155 && greenColor < 200 && blueColor < 200) {
color = 6; // 蓝色
} else {
color = 0; // 未知
}
}

void calibrate() {
Serial.println("Calibrating..."); // 校准中...
Serial.println("White"); // 请将传感器对准白色物体
// 设置校准值:
digitalWrite(13, HIGH); // 点亮内置LED
delay(2000); // 延时
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redMin = pulseIn(sensorOut, LOW); // 读取红色最小值
delay(100);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenMin = pulseIn(sensorOut, LOW); // 读取绿色最小值
delay(100);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueMin = pulseIn(sensorOut, LOW); // 读取蓝色最小值
delay(100);
Serial.println("next..."); // 接下来,请对准黑色物体
digitalWrite(13, LOW);
delay(2000);
Serial.println("Black"); // 黑色校准
// 设置校准值:
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redMax = pulseIn(sensorOut, LOW); // 读取红色最大值
delay(100);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenMax = pulseIn(sensorOut, LOW); // 读取绿色最大值
delay(100);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueMax = pulseIn(sensorOut, LOW); // 读取蓝色最大值
delay(100);
Serial.println("Done calibrating."); // 校准完成
digitalWrite(13, LOW); // 熄灭内置LED
}

void playMusic() {
// 根据颜色播放音乐
switch (color) {
case 1: // 白色
Serial.println("情绪:纯净");
myDFPlayer.play(1); // whitesong
break;
case 2: // 黑色
Serial.println("情绪:沉默");
myDFPlayer.play(2); // blacksong
break;
case 3: // 黄色
Serial.println("情绪:活力");
myDFPlayer.play(3); // yellowsong
break;
case 4: // 红色
Serial.println("情绪:热情");
myDFPlayer.play(4); // redsong
break;
case 5: // 绿色
Serial.println("情绪:愉悦");
myDFPlayer.play(5); // greensong
break;
case 6: // 蓝色
Serial.println("情绪:平和");
myDFPlayer.play(6); // bluesong
break;
default:
Serial.println("未知颜色");
break;
}
}

UNO board 2

Code:

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

const int stepsPerRevolution = 200;
const int motorPin1 = 2;
const int motorPin2 = 3;
const int motorPin3 = 4;
const int motorPin4 = 5;

Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2, motorPin3, motorPin4);

void setup() {
myStepper.setSpeed(50); // 设置速度为50 RPM
}

void loop() {
myStepper.step(stepsPerRevolution); // 顺时针旋转一圈
}

Overall Wiring Diagram