-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlayChristmasMelodyUSDistance.ino
216 lines (189 loc) · 6.97 KB
/
PlayChristmasMelodyUSDistance.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
/*
* PlayChristmasMelodyUSDistance.cpp
*
* Plays a random Christmas melody if US sensor value is in a defined range.
* If TALKIE_FEEDBACK is defined, the current distance is spelled by Talkie
*
* Copyright (C) 2019-2023 Armin Joachimsmeyer
* armin.joachimsmeyer@gmail.com
*
* This file is part of PlayRttl https://github.com/ArminJo/PlayRtttl.
*
* PlayRttl is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
*/
#include <Arduino.h>
//#define USE_NO_RTX_EXTENSIONS // Disables RTX format definitions `'s'` (style) and `'l'` (loop). Saves up to 332 bytes program memory
#include <PlayRtttl.hpp>
#include "HCSR04.hpp"
#include "BlinkLed.h"
#define TALKIE_FEEDBACK
#if defined(TALKIE_FEEDBACK)
/*
* You need to install "Talkie" and "EasyButtonAtInt01" libraries under "Tools -> Manage Libraries..." or "Ctrl+Shift+I" -> use the names as filter string
*/
#include <TalkieUtils.h>
#include <Vocab_US_Large.h>
Talkie Voice;
#define USE_BUTTON_0
#include <EasyButtonAtInt01.hpp>
EasyButton Button0AtPin2; // Pin2 on
#endif
/*
* The range for the melody to start
*/
#define MINIMUM_DISTANCE_CENTIMETER 40
#define MAXIMUM_DISTANCE_CENTIMETER 120
#define NUMBER_OF_CONSECUTIVE_IN_RANGE_READINGS 5
#define DELAY_MILLIS_FOR_IN_RANGE_READING 200
#define NUMBER_OF_CONSECUTIVE_OUT_RANGE_READINGS 5
#define DELAY_MILLIS_FOR_OUT_RANGE_READING 1000
#define PIN_BUZZER 3
#define PIN_ECHO_IN 4
#define PIN_TRIGGER_OUT 5
#define PIN_GREEN_LED A0
#define PIN_YELLOW_LED A2
#define PIN_RED_LED A4
BlinkLed RedLed(PIN_RED_LED);
BlinkLed YellowLed(PIN_YELLOW_LED);
BlinkLed GreenLed(PIN_GREEN_LED);
void playRandomSongAndBlink();
//The setup function is called once at startup of the sketch
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) /*stm32duino*/|| defined(USBCON) /*STM32_stm32*/ \
|| defined(SERIALUSB_PID) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_attiny3217)
delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
#endif
// Just to know which program is running on my Arduino
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_PLAY_RTTTL));
Serial.print(F("Button pin="));
Serial.println(INT0_PIN);
initUSDistancePins(PIN_TRIGGER_OUT, PIN_ECHO_IN);
delay(500); // to avoid sound directly at power up
uint16_t tDistance = getUSDistance();
if (tDistance < US_DISTANCE_DEFAULT_TIMEOUT_CENTIMETER) {
randomSeed(tDistance);
/*
* Play first song
*/
playRandomSongAndBlink();
}
RedLed.off(); // switch it manually off here
}
void loop() {
static uint8_t sInRangeCounter = 0;
static uint16_t tRandomSeed;
unsigned int tCentimeter = getUSDistanceAsCentimeterWithCentimeterTimeout(300);
Serial.print("Distance=");
Serial.print(tCentimeter);
Serial.println("cm.");
#if defined(TALKIE_FEEDBACK)
if (Button0AtPin2.ButtonToggleState) {
/*
* Output distance with talkie
*/
if (tCentimeter == 0) {
Voice.sayQ(sp2_TIME);
Voice.sayQ(sp2_OUT);
} else {
sayQNumber(&Voice, tCentimeter);
}
Voice.wait();
}
#endif
if (tCentimeter > MINIMUM_DISTANCE_CENTIMETER && tCentimeter < MAXIMUM_DISTANCE_CENTIMETER) {
sInRangeCounter++;
tRandomSeed += tCentimeter;
if (sInRangeCounter >= NUMBER_OF_CONSECUTIVE_IN_RANGE_READINGS) {
/*
* Now an object is for a longer time in the right range.
* Play one song and wait for the object to leave the range
* As long as the object is in range, the red LED is active
*/
randomSeed(tRandomSeed);
playRandomSongAndBlink();
sInRangeCounter = 0;
// wait for distance to be out of range for NUMBER_OF_CONSECUTIVE_OUT_RANGE_READINGS consecutive readings
uint8_t tCounter = 0;
while (tCounter < NUMBER_OF_CONSECUTIVE_OUT_RANGE_READINGS) {
tCentimeter = getUSDistanceAsCentimeter();
Serial.print("Distance=");
Serial.print(tCentimeter);
Serial.print("cm.");
#if defined(TALKIE_FEEDBACK)
if (Button0AtPin2.ButtonToggleState) {
/*
* Output distance with talkie
*/
if (tCentimeter > 0) {
sayQNumber(&Voice, tCentimeter);
} else {
Voice.sayQ(sp2_TIME);
Voice.sayQ(sp2_OUT);
}
Voice.wait();
}
#endif
if (tCentimeter < MINIMUM_DISTANCE_CENTIMETER || tCentimeter > MAXIMUM_DISTANCE_CENTIMETER) {
tCounter++;
} else {
tCounter = 0; // reset to start condition
Serial.print(" Still in range.");
}
Serial.print(" Wait for ");
Serial.print(NUMBER_OF_CONSECUTIVE_OUT_RANGE_READINGS - tCounter);
Serial.println(" distances out of range.");
delay(DELAY_MILLIS_FOR_OUT_RANGE_READING);
}
} else {
GreenLed.on();
delay(5);
GreenLed.off();
}
} else {
sInRangeCounter = 0;
}
RedLed.off();
delay(DELAY_MILLIS_FOR_IN_RANGE_READING);
}
/*
* Leaves red LED on
*/
void playRandomSongAndBlink() {
char StringBuffer[16];
Serial.println();
Serial.print("Now playing: ");
startPlayRandomRtttlFromArrayPGM(PIN_BUZZER, RTTTLChristmasMelodies, ARRAY_SIZE_CHRISTMAS_MELODIES, StringBuffer,
sizeof(StringBuffer));
Serial.println(StringBuffer);
/*
* Start LEDs blinking
*/
RedLed.startWithOnOffTime(300, 600);
YellowLed.startWithOnOffTime(800, 400);
GreenLed.startWithOnOffTime(1000, 1500);
// wait for the song to end
while (updatePlayRtttl()) {
RedLed.update();
YellowLed.update();
GreenLed.update();
delay(1);
}
// switch off only 2 LEDs, the red one will be on until the "object in the right distance" is gone
YellowLed.off();
GreenLed.off();
RedLed.on();
delay(2000);
}