2. (Decibels per milliwatts)
pinMode(ACTIONbutton,INPUT); // Set the ACTION button as an input
(GPIO4)
pinMode(speaker,OUTPUT); // Set the speaker output (GPIO5)
startUp();
}
// Code below runs forever
void loop()
{
analogON_OFF = analogRead(monitBatt);
voltageON_OFF = 3.5*analogON_OFF/1023;
// For arduino testing purposes ONLY
if (voltageON_OFF <= 3.25){
Serial.print("Low Battery Warning: Please recharge the battery!");
else if (voltageON_OFF > 3.25){
Serial.print("/nBattery is healthy!");
}
// At low battery, there will be two quick beeps and a long delay
// Bluetooth will also be turned off
while (voltageON_OFF < 3.25){
RFduinoBLE.end();
lowBattWarn();
delay(500);
}
buttonVal = digitalRead(ACTIONbutton);
// Test for button pressed and store the down time
if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnUpTime) >
long(debounce)){
btnDnTime = millis(); // Store the down time
}
// Test for button release and store the up time
if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnDnTime) >
long(debounce)){
if (ignoreUp == false){ // "FALSE" only if the ACTION button is tapped once
stateEvent = 1;
if (stateEvent == 1){
stateEvent1();
}
3. delay(150);
if (stateEvent == 0){
stateEvent0();
}
}
else{
ignoreUp == false;
}
btnUpTime = millis(); // Store the up time
}
// Test for button held down for longer than the hold time
if (buttonVal == HIGH && (millis() - btnDnTime) > long(holdTime)){
stateEvent = 2;
while (stateEvent == 2){
stateEvent2();
}
ignoreUp = true; // "TRUE" only if the ACTION button is held
btnDnTime = millis(); // Store down time
}
buttonLast = buttonVal;
}
// After initiating bluetooth connection, send an INT value of 0 through BLE
void RFduinoBLE_onConnect(){
Serial.println("Bluetooth has been connected!");
RFduinoBLE.sendByte(0);
}
// Tell the user when the device loses bluetooth connection
void RFduinoBLE_onDisconnect(){
Serial.println("Bluetooth has been disconnected!");
}
// 'Home' state event
void stateEvent0(){
RFduinoBLE.sendByte(0);
}
// First ACTION state event
void stateEvent1(){
4. RFduinoBLE.sendByte(1);
tone(5,60); // Generate a tone after first ACTION event
delay(100); // Delay used for repeating beeps
stateEvent = 0;
}
// Second ACTION state event
void stateEvent2(){
noTone(5); // Turn off tone after second ACTION event
RFduinoBLE.sendByte(2);
}
// Two quick beeps at POWER ON
void startUp(){
for (int i=0; i <= 2; i++){
digitalWrite(speaker,HIGH);
delay(100);
digitalWrite(speaker,LOW);
}
}
// Two quick beeps then a long delay at low battery
void lowBattWarn(){
for (int j=0; j<=2; j++){
digitalWrite(speaker,HIGH);
delay(100);
digitalWrite(speaker,LOW);
}
}