Example - UniQHW/NudeAudio_Move-S_Readable-BT-Controller GitHub Wiki
Source File : examples/MoveSTest.ino
The example has been written for Arduino boards, it should however run and function on devices that meet the Microcontroller Boards Requirement List with the exception of the embedded/internal LED (LED_BUILTIN
) which may not be present on all devices. Shall this be the case, one may assign LED_BUILTIN
to a pin controlling an external LED.
The provided example file tests the libraries core functionality with nothing but serial logging and the embedded LED (LED_BUILTIN
)
While paired, the following information is sprinted to the serial console :
Device has been paired
Device disconnected
The incoming speaker volume is then polled for readings below 10, being loud and usually high pitched noises. If values below 10 have been detected, the internal board LED (LED_BUILTIN
) remains ON.
During pairing mode, while intervaling/flashing the Bluetooth LED, the following information is printed to the serial console :
Awaiting device
The Arduino then polls for further changes
During pairing mode, with the Bluetooth LED off (aka in sleep mode), the following information is printed to the serial console :
Entered sleep mode
Awaiting device
The Arduino then polls for further changes
We start out by taking a look at the headers :
#include <move_s_r.h>
As previously mentioned in section Project Setup, the mov_s_r.h header is packed with the libraries device structures. More about those under section move_s_r.
Following the header inclusion, we get to see the programms global declarations :
move_s_r *mvs_r;
move_s_bt_mode mode;
The first decleration defines a read-only Move S device pointer that is assigned in setup().
The second decleration defines a enumerated variable that buffers the devices bluetooth mode. Once the bluetooth mode has been read, it is evaluated. A list of bluetooth modes are defined here.
Next up is the setup function, executed on device boot.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
/* Initialize with BT LED on pin 2 and speaker on pin a5 */
mvs_r = new move_s_r(2, A5);
}
At first, we're setting the embedded LED pin to out, so that we can control it.
Next up we're assigning a device object to our device pointer. The device object is initialized with the BT_LED input pin set to D2, and the speaker input pin to A5. Pin modes are defined on initialization.
Finally, we enter the loop function, which, as the name already indicates, is constantly looped by the device :
void loop() {
/* Evaluate device mode */
switch(mvs_r->mode()) {
/* Device is paired */
case paired_mode :
Serial.println("Device has been paired");
while(mvs_r->mode() == paired_mode) {
/* If volume signal reaches 850+, receive high signal */
if(mvs_r->spk->sig(SPK_NOISE_LEVEL_HIGH)) {
digitalWrite(LED_BUILTIN, HIGH);
while(mvs_r->spk->sig(SPK_NOISE_LEVEL_HIGH));
digitalWrite(LED_BUILTIN, LOW);
}
}
Serial.println("Device disconnected");
break;
/* Device is in pairing mode and is blinking */
case pair_blink_mode :
Serial.println("Awaiting device");
while(mvs_r->mode() == pair_blink_mode);
break;
/* Device is in pairing mode, but isn't blinking */
case pair_sleep_mode :
Serial.println("Entered sleep mode");
Serial.println("Awaiting device");
while(mvs_r->mode() == pair_sleep_mode);
}
}
The loop function essentially just consists of one large switch statement, that evaluates the device current bluetooth mode using the device structures mode()
function.
/* Device is paired */
case paired_mode :
Serial.println("Device has been paired");
while(mvs_r->mode() == paired_mode) {
/* If volume signal reaches below 10, receive high signal */
if(mvs_r->spk->sig(SPK_NOISE_LEVEL_HIGH)) {
digitalWrite(LED_BUILTIN, HIGH);
while(mvs_r->spk->sig(SPK_NOISE_LEVEL_HIGH));
digitalWrite(LED_BUILTIN, LOW);
}
}
Serial.println("Device disconnected");
break;
The first switch case checks whether the device is paired. Shall this be true, it will begin polling the audio input volume. If the input exceeds a value lower than 10, the embedded LED remains on until a value above 10 has been received/detected.
If the device has been unpaired, it prints "Device Disconnect" following a break of the case
/* Device is in pairing mode and is blinking */
case pair_blink_mode :
Serial.println("Awaiting device");
while(mvs_r->mode() == pair_blink_mode);
break;
The 2nd switch case executes if the device is set to pairing mode with an blinking Bluetooth LED. What is enumerated as pair_blink_mode
is a state that applies to the first 3 minutes after the device has switched into pairing mode. During those 3 minutes, the devices Bluetooth LED switches state a total of 880 times with each state taking about 0.2 seconds.
A simple "Awaiting device" is printed to the serial console, following a while loop that waits for the device to exit pair_blink_mode.
/* Device is in pairing mode, but isn't blinking */
case pair_sleep_mode :
Serial.println("Entered sleep mode");
Serial.println("Awaiting device");
while(mvs_r->mode() == pair_sleep_mode);
The 3rd switch case executes if the device is set to pairing mode with the Bluetooth LED off, or what is here considered as pair_sleep_mode
. This takes place after pair_blink_mode
expires. In other words, this switch case takes place after 3 minutes in pairing mode.
The switch case simply informs that the device has entered sleep mode, and just as in the previously discussed switch case, prints "Awaiting Device". The device than awaits a change of state.