#include <Arduino.h>
#include "Button2.h" //https://github.com/LennartHennigs/Button2
#define BUTTON_PIN 2 // Hardware pin for main button
#define POWER_LED_PIN 3 // Pin for soft power LED
#define VIBE_PIN 10 // Pin for vibration motor
#define BATTERY_PIN A0// Pin for monitoring battery voltage. Connect with voltage divider resistor network
#define SECONDS_HOUR 3600
#define MS_MINUTE 60000
#define MS_HOUR 3600000
#define TEST_TARGET 3000
#define ANALOG_RES 12
#define BATTERY_MAX 4.2
#define ANALOG_MAX 4095
Button2 button; // Creates event driven button library instance
bool counting_state = false; // Holds counting state
bool power_state = false; // Holds soft power state
bool debug = false; // When true more verbose debug data is shown
static unsigned long lastCountMillis = 0; // Holds the counter start millis
// Fired on all button releases
void handleTap(Button2& b) {
if (debug)
Serial.println("(b.wasPressedFor(): " + String(b.wasPressedFor()));
}
//Stops and resets running counter
void handleDoubleClick(Button2& b) {
Serial.println("handleDoubleClick");
counting_state = false;
// Battery level code
int val = analogRead(BATTERY_PIN);
analogWrite(VIBE_PIN, val); // Testing analog output
Serial.println("Val: " + String(val));
float VBAT = 3.30f * float(analogRead(BATTERY_PIN)) / 4096.0f; // LiPo battery
Serial.print("Battery Voltage = "); Serial.print(VBAT, 2); Serial.println(" V");
}
// Soft on/off handler. Long press toggles on\off state.
void handleLongClick(Button2& b) {
Serial.println("handleLongClick");
if (power_state == true) {
Serial.println("-- Powering off");
digitalWrite(POWER_LED_PIN, 0); // Turn off Power LED
power_state = false;
} else {
Serial.println("-- Powering on");
digitalWrite(POWER_LED_PIN, 1); // Turn on Power LED
power_state = true;
}
}
// If device is not already counting, a single button press starts the counting.
void handleSingleClick(Button2& b) {
Serial.println("handleSingleClick");
if (power_state == false) {
Serial.println("!! Cant start. Soft power is off.");
return;
}
if (counting_state == true) {
Serial.println("!! Cant start. Already counting.");
return;
}
if (counting_state == false) {
unsigned long start_ms = millis();
Serial.println("-- Starting millis: " + String(start_ms));
lastCountMillis = start_ms; // Set initial time
counting_state = true;
}
}
void setup() {
pinMode(POWER_LED_PIN, OUTPUT);
pinMode(VIBE_PIN, OUTPUT);
pinMode(BATTERY_PIN, INPUT);
digitalWrite(POWER_LED_PIN, power_state); // Will be off until long press
Serial.begin(115200);
Serial.println("-- Hello, Sankalpa!");
button.begin(BUTTON_PIN);
button.setTapHandler(handleTap);
button.setDoubleClickHandler(handleDoubleClick);
button.setLongClickDetectedHandler(handleLongClick);
button.setClickHandler(handleSingleClick);
button.setLongClickTime(500);
// Configure ADC pin for reading battery voltage
analogReadResolution(ANALOG_RES);
//analogSetAttenuation(ADC_11db); // Default is ADC_11db
analogWriteResolution(ANALOG_RES);
}
// Counts to target time
void countingService() {
if (counting_state == false) {
return;
}
if (millis() - lastCountMillis < TEST_TARGET) {
return;
}
Serial.println("-- Done Counting millis: " + String(millis()));
lastCountMillis = millis();
counting_state = false;
}
void powerService() {
}
void loop() {
button.loop();
countingService();
delay(10); // this speeds up the simulation
}