-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundMachine3.ino
231 lines (189 loc) · 5.86 KB
/
SoundMachine3.ino
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
The Sound Machine - based on Roald Dahl short story
Roni Bandini
March 2020
Buenos Aires, Argentina
Twitter/Instagram @RoniBandini
*/
#include "SoftwareSerial.h"
#include <TM1637Display.h>
// Mp3 player rx and tx
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00
# define ACTIVATED LOW
#define sensor A7
#define buttonPin 2
#define CLK 4
#define DIO 5
// Creates display object
TM1637Display display(CLK, DIO);
// All - in display
const uint8_t SEG_START[] = {
SEG_G, // -
SEG_G, // -
SEG_G, // -
SEG_G // -
};
int buttonValue=0;
int humidValue=0;
long randNumber;
void setup(){
Serial.begin(9600);
display.setBrightness(0x0f);
display.setSegments(SEG_START);
// humidity sensor
pinMode(sensor, INPUT);
// button
pinMode(buttonPin,INPUT_PULLUP);
// mp3 player
mySerial.begin(9600);
// nothing being played, just init
playFirst();
Serial.println("The Sound Machine by Roni Bandini");
Serial.println("Based on Roald Dahl short story");
delay (2000);
Serial.println("Ready...");
}
void loop(){
// read button
buttonValue = digitalRead(buttonPin);
if (buttonValue == LOW) {
// button has been pressed, map humidty sensor
humidValue = map(analogRead(sensor), 0, 1023, 100, 0);
// for random sounds in each moisture category
randomSeed(analogRead(0));
Serial.print("Humidity: ");
Serial.print(humidValue);
Serial.println("%");
// send to display
display.showNumberDec(humidValue, false,4);
if (humidValue<40){
randNumber = random(1, 10);
Serial.println("Dry");
switch (randNumber) {
case 1:
execute_CMD(0x0F,0x01,0x01);
break;
case 2:
execute_CMD(0x0F,0x01,0x02);
break;
case 3:
execute_CMD(0x0F,0x01,0x03);
break;
case 4:
execute_CMD(0x0F,0x01,0x04);
break;
case 5:
execute_CMD(0x0F,0x01,0x05);
break;
case 6:
execute_CMD(0x0F,0x01,0x06);
break;
case 7:
execute_CMD(0x0F,0x01,0x07);
break;
case 8:
execute_CMD(0x0F,0x01,0x08);
break;
case 9:
execute_CMD(0x0F,0x01,0x09);
break;
}
}
if (humidValue>39 and humidValue<60){
randNumber = random(10, 13);
Serial.println("Medium");
Serial.println(randNumber);
switch (randNumber) {
case 10:
execute_CMD(0x0F,0x01,0x10);
break;
case 11:
execute_CMD(0x0F,0x01,0x11);
break;
}
}
if (humidValue>61){
randNumber = random(12, 19);
Serial.println("Lot of water");
Serial.println(randNumber);
switch (randNumber) {
case 12:
execute_CMD(0x0F,0x01,0x12);
break;
case 13:
execute_CMD(0x0F,0x01,0x13);
break;
case 14:
execute_CMD(0x0F,0x01,0x14);
break;
case 15:
execute_CMD(0x0F,0x01,0x15);
break;
case 16:
execute_CMD(0x0F,0x01,0x16);
break;
case 17:
execute_CMD(0x0F,0x01,0x17);
break;
case 18:
execute_CMD(0x0F,0x01,0x18);
break;
}
}
delay(1000);
display.setSegments(SEG_START);
} // button pressed
}
void playFirst()
{
execute_CMD(0x3F, 0, 0); // query device
delay(500);
setVolume(25);
delay(500);
//execute_CMD(0x11,0,1); // repeat playback
//delay(500);
}
void pause()
{
execute_CMD(0x0E,0,0);
delay(500);
}
void play()
{
execute_CMD(0x0D,0,1);
delay(500);
}
void playNext()
{
execute_CMD(0x01,0,1);
delay(500);
}
void playPrevious()
{
execute_CMD(0x02,0,1);
delay(500);
}
void setVolume(int volume)
{
execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
delay(2000);
}
void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}