#include <Arduino.h>
#include <SmartButton.h> //https://github.com/marcinbor85/SmartButton/tree/master
#include <Ramp.h> //https://github.com/siteswapjuggler/RAMP
using namespace smartbutton;
constexpr int BUTTON_PIN = 8;
constexpr int LED_PIN = 3;
constexpr int BUZZER_PIN = 4;
constexpr unsigned long BLINKING_TURBO = 50UL;
constexpr unsigned long BLINKING_FAST = 1000UL;
constexpr unsigned long BLINKING_SLOW = 5000UL;
constexpr unsigned long BLINKING_LONG = 10000UL;
const long second = 1000;
const long minute = second * 60;
const long hour = minute * 60;
int buzzMax = 255;
int buzzTime = 2500;
int val;
int lastValue; // global variable
rampInt myRamp; // new int ramp object
static bool blinking = true;
static unsigned long blinkingPeriod = BLINKING_SLOW;
static bool counting = false;
const long countTarget = hour;
static long count;
SmartButton button(BUTTON_PIN, SmartButton::InputType::NORMAL_HIGH);
static void beepBuzz() {
analogWrite(BUZZER_PIN, 255);
delay(100);
analogWrite(BUZZER_PIN, 0);
}
static void printDebug(int casenum = 0) {
Serial.println("Blinking: " + String(blinking) + " | Ramp: " + String(val) +
" | Counting: " + String(counting) + " | Event: " + String(casenum));
}
void eventCallback(SmartButton *button, SmartButton::Event event, int clickCounter)
{
if (event == SmartButton::Event::CLICK) { // Click event handler
switch (clickCounter) {
case 1:
blinkingPeriod = BLINKING_FAST; // Single click will turn fast led blinking
printDebug(clickCounter);
break;
case 2:
blinkingPeriod = BLINKING_SLOW; // Double click will turn slow led blinking
printDebug(clickCounter);
break;
case 3:
blinkingPeriod = BLINKING_TURBO; // Double click will turn slow led blinking
printDebug(clickCounter);
break;
default:
printDebug();
break;
}
} else if (event == SmartButton::Event::HOLD) { // Hold press event handler
blinking = !blinking; // Turning on/off blinking service
counting = !counting;
printDebug();
beepBuzz();
}
}
void setup()
{
pinMode(LED_PIN, OUTPUT); // Digital output for led
pinMode(BUTTON_PIN, INPUT_PULLUP); // Digital input with pull-up resistors (normal high)
Serial.begin(115200);
Serial.println("ˁ˚ᴥ˚ˀ Started Sancalpa ˁ˚ᴥ˚ˀ");
Serial.println("Target ms[sec](min): " + String(countTarget) + "[" + countTarget / 1000 + "](" + + ")");
printDebug();
button.begin(eventCallback); // Initialize and register smart button
myRamp.setGrain(1); // set grain to 1 ms
//myRamp.go(255); // set value directly
myRamp.go(buzzMax, buzzTime, LINEAR, LOOPFORWARD); // go to 0 in n ms
}
static void motorRampService()
{
val = myRamp.update(); // store updated value
if (val != lastValue) { // print updated value and completion percentage
analogWrite(BUZZER_PIN, val);
}
lastValue = val;
}
static void countService()
{
static unsigned long lastMillis = 0;
if (!counting)
return;
if (millis() - lastMillis < countTarget)
return;
Serial.println("Done Counting");
lastMillis = millis();
counting = !counting;
}
static void blinkService()
{
static unsigned long lastMillis = 0;
static bool blinkingState = false;
if (!blinking)
return;
if (millis() - lastMillis < blinkingPeriod)
return;
lastMillis = millis();
digitalWrite(LED_PIN, blinkingState);
blinkingState = !blinkingState;
}
void loop()
{
SmartButton::service(); // Asynchronous service routine, should be called periodically
blinkService(); // Asynchronous led blinking service
motorRampService();
countService();
}