-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
183 lines (177 loc) · 5.25 KB
/
main.cpp
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
#include <Arduino.h>
#include "pitches.h"
#define BUZZZER_PIN 18
int listentime;
unsigned long stoptime;
unsigned long starttime;
unsigned long nowtime;
int menuChoice;
int SongOne[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
int DurationOne[] = {
1000 / 4, 1000 / 8, 1000 / 8, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4};
int SongTwo[] = {
NOTE_D3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_F3, NOTE_E3, NOTE_D3, NOTE_C3, NOTE_B3, NOTE_B3,
NOTE_C3, NOTE_D3, NOTE_D3, NOTE_C3, NOTE_C3};
int DurationTwo[] = {
1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4,
1000 / 4, 1000 / 4, 1000 / 4 + 1000 / 8, 1000 / 8, 1000 / 2};
int SongThree[] = {
NOTE_D3, NOTE_DS3, NOTE_F3, NOTE_D3, NOTE_DS3, NOTE_F3};
int DurationThree[] = {
1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4, 1000 / 4};
int SongFour[] = {
NOTE_D3, NOTE_F3, NOTE_D4, NOTE_D3, NOTE_F3, NOTE_D4};
int DurationFour[] = {
1000 / 16, 1000 / 16, 1000 / 4, 1000 / 16, 1000 / 16, 1000 / 4};
void setup()
{
Serial.begin(9600);
delay(500);
}
int StrToHex(char str[])
{
return (int)strtol(str, 0, 16);
}
void loop()
{
Serial.println("");
Serial.println("program started");
Serial.println("which song do you want to listen ? ");
while (Serial.available())
{
Serial.read();
}
while (Serial.available() == 0)
{
}
menuChoice = Serial.parseInt();
Serial.print("you shoose to listen song ");
Serial.println(menuChoice);
Serial.println("How long do you want to listen? (/s)");
while (Serial.available())
{
Serial.read();
}
while (Serial.available() == 0)
{
}
listentime = Serial.parseInt();
Serial.print("you want to listen ");
Serial.print(listentime);
Serial.println(" s");
delay(1000);
while (Serial.available())
{
Serial.read();
}
switch (menuChoice)
{
case 1:
starttime = millis();
stoptime = listentime * 1000 + starttime;
for (int thisNote = 0; thisNote < 8; thisNote++)
{
tone(BUZZZER_PIN, SongOne[thisNote], DurationOne[thisNote]);
for (nowtime = millis(); nowtime > stoptime;)
{
goto nemo;
}
int pauseBetweenNotes = DurationOne[thisNote] * 1.30;
delay(pauseBetweenNotes);
for (; thisNote == 8;)
{
thisNote = 0;
}
}
// stop the tone playing:
noTone(BUZZZER_PIN);
break;
case 2:
// humidity sensor code goes here
Serial.println("You choose 2");
Serial.println("Then I will play song 2");
starttime = millis();
stoptime = listentime * 1000 + starttime;
for (int thisNote2 = 0; thisNote2 < 16; thisNote2++)
{
for (nowtime = millis(); nowtime > stoptime;)
{
goto nemo;
}
// to calculate the note duration, take one second divided by the note type.
// e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
tone(BUZZZER_PIN, SongTwo[thisNote2], DurationTwo[thisNote2]);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = DurationTwo[thisNote2] * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(BUZZZER_PIN);
for (; thisNote2 == 8;)
{
thisNote2 = 0;
}
}
break;
case 3:
// pressure sensor code goes here
Serial.println("You choose 3");
Serial.println("Then I will play song 3");
starttime = millis();
stoptime = listentime * 1000 + starttime;
for (int thisNote3 = 0; thisNote3 < 6; thisNote3++)
{
for (nowtime = millis(); nowtime > stoptime;)
{
goto nemo;
}
// to calculate the note duration, take one second divided by the note type.
// e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
tone(BUZZZER_PIN, SongThree[thisNote3], DurationThree[thisNote3]);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = DurationThree[thisNote3] * 1.30;
delay(pauseBetweenNotes);
for (; thisNote3 == 6;)
{
thisNote3 = 0;
}
// stop the tone playing:
noTone(BUZZZER_PIN);
}
break;
case 4:
// humidity sensor code goes here
Serial.println("You choose 4");
Serial.println("Then I will play song 4");
starttime = millis();
stoptime = listentime * 1000 + starttime;
for (int thisNote4 = 0; thisNote4 < 6; thisNote4++)
{
for (nowtime = millis(); nowtime > stoptime;)
{
goto nemo;
}
// to calculate the note duration, take one second divided by the note type.
// e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
tone(BUZZZER_PIN, SongFour[thisNote4], DurationFour[thisNote4]);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = DurationFour[thisNote4] * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(BUZZZER_PIN);
for (; thisNote4 == 6;)
{
thisNote4 = 0;
}
}
break;
default:
Serial.println("Please choose a valid selection");
break;
}
nemo:
noTone(BUZZZER_PIN);
}