#include "SimpleTimer.h"
/*
// Pin for button
const int BTN_PIN = 2;
const int LED_PIN = 23;
// Button state variables
bool lastButtonState = false;
bool buttonState = false;
// Time variables
unsigned long lastTime = 0;
unsigned long timeCounter = 0;
unsigned long totalTime = 0;
int threshold = 3000; // in milliseconds
int continues = 0;
int minute = 0;
// Timer variables
SimpleTimer timer;
int interval = 60000; // in milliseconds
void setup() {
Serial.begin(9600);
Serial.println(".");
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
lastButtonState = digitalRead(BTN_PIN);
timer.setInterval(interval, printTotalTime);
timer.setInterval(25, mainLoop);
Serial.println("..");
//blinkCycle(int duration, int subCycles) blinkCycle(5,5); = 5 blinks in 5 seconds
blinkCycle(1,5);
}
void loop() {
timer.run();
}
///////////////////////////////////////
void mainLoop() {
// Update button state
buttonState = digitalRead(BTN_PIN);
blinkCycle(1,2);
// Check if button is pushed
if (buttonState == LOW && lastButtonState == HIGH) {
// Button is pushed down
Serial.println("btn is down");
blinkCycle(1,2);
if (millis() - lastTime <= threshold) {
// Button is pushed continuously within threshold
timeCounter += millis() - lastTime;
continues++;
}
lastTime = millis();
} else if (buttonState == HIGH && lastButtonState == LOW) {
// Button is released
Serial.println("btn is up");
blinkCycle(1,4);
if (timeCounter > 0) {
// Total time is updated only if button was pushed down before
totalTime += timeCounter;
timeCounter = 0;
continues = 0;
}
}
lastButtonState = buttonState;
}
void printTotalTime() {
minute++;
float percentage = 100.0 * (float)totalTime / (float)interval;
Serial.print(String(minute/10) + String(minute%10) + ":");
Serial.print(percentage, 0);
Serial.print(".....|");
Serial.print(totalTime / 1000);
Serial.println("s");
// Reset variables for next minute
totalTime = 0;
continues = 0;
minute = 0;
}
void blinkCycle( int duration, int subCycles) {
for (int i = 0; i < subCycles; i++) {
digitalWrite(LED_PIN, HIGH);
delay((duration*1000)/subCycles);
digitalWrite(LED_PIN, LOW);
delay((duration*1000)/subCycles);
}
delay(duration*1000);
}
*/
/*
const int LED_PIN = 23;
const int btnA = 2; // Pin for button
const int threshold = 3; // Threshold for consecutive button presses (in seconds)
const int maxTime = 15 * 60 * 1000; // Maximum time for logging (in milliseconds)
int totalTime = 0; // Total time for button presses (in milliseconds)
int startTime = 0; // Start time for button presses (in milliseconds)
int lastState = HIGH; // Last state of the button
SimpleTimer timer; // Timer object for updating the display
void setup() {
Serial.begin(9600);
Serial.println(".");
pinMode(btnA, INPUT_PULLUP);
timer.setInterval(1000, updateDisplay); // Update the display every second
timer.setInterval(25, mainLoop); // Update the display every second
pinMode(LED_PIN, OUTPUT);
blinkCycle(1,5);
Serial.println("..");
}
void loop() {
timer.run(); // Update the timer
}
void mainLoop() {
int state = digitalRead(btnA);
if (state == LOW && lastState == HIGH) {
// Button has just been pressed
if (millis() - startTime > threshold * 1000) {
// New button press sequence has started
startTime = millis();
}
totalTime += millis() - startTime;
startTime = millis();
}
lastState = state;
if (millis() > maxTime) {
// Time limit reached, stop logging button presses
timer.disable(timer.getTimer(0)); // Disable the timer
}
}
void updateDisplay() {
int elapsed = millis() % (60 * 1000); // Elapsed time in the current minute (in milliseconds)
int percentage = map(totalTime % (60 * 1000), 0, 60 * 1000, 0, 10); // Percentage of the current minute
Serial.print(String(elapsed / 1000 / 60).padLeft(2, '0') + ":" + String(elapsed / 1000 % 60).padLeft(2, '0'));
for (int i = 0; i < 10; i++) {
if (i < percentage) {
Serial.print(".");
} else {
Serial.print(" ");
}
}
Serial.print("|" + String(totalTime / 1000) + "s\n");
}
void blinkCycle( int duration, int subCycles) {
for (int i = 0; i < subCycles; i++) {
digitalWrite(LED_PIN, HIGH);
delay((duration*1000)/subCycles);
digitalWrite(LED_PIN, LOW);
delay((duration*1000)/subCycles);
}
delay(duration*1000);
}
*/
/*
#define LED_PIN 23
#define BUTTON_PIN 2
SimpleTimer timer;
int threshold = 3; // threshold in seconds for continuous button presses
int buttonState = HIGH;
unsigned long lastButtonChange = 0;
unsigned long lastMinuteChange = 0;
unsigned long totalButtonPressDuration = 0;
int totalButtonPresses = 0;
void setup() {
Serial.begin(9600);
Serial.println(".");
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.print("BUTTON_PIN is ");
Serial.println(digitalRead(BUTTON_PIN));
timer.setInterval(5, checkButton); // check button state every 25ms
timer.setInterval(5000, printMinuteStats); // print stats every minute
}
void loop() {
timer.run();
}
void checkButton() {
int currentState = digitalRead(BUTTON_PIN);
if (currentState != buttonState) {
unsigned long currentMillis = millis();
unsigned long buttonDuration = currentMillis - lastButtonChange;
lastButtonChange = currentMillis;
if (currentState == LOW) {
// button pressed
Serial.print("BUTTON_PIN is ");
Serial.println(digitalRead(BUTTON_PIN));
totalButtonPresses++;
if (buttonDuration < threshold * 1000) {
// continuous button press
totalButtonPressDuration += buttonDuration;
}
digitalWrite(LED_PIN, HIGH);
} else {
// button released
Serial.print("BUTTON_PIN is ");
Serial.println(digitalRead(BUTTON_PIN));
digitalWrite(LED_PIN, LOW);
}
}
}
void printMinuteStats() {
unsigned long currentMillis = millis();
unsigned long minuteDuration = currentMillis - lastMinuteChange;
lastMinuteChange = currentMillis;
int dotCount = map(totalButtonPressDuration, 0, minuteDuration, 0, 10);
Serial.print(String((minuteDuration / 1000) / 60) + ":" + String((minuteDuration / 1000) % 60, 2) + "___" + String(totalButtonPressDuration / 1000) + "s ");
for (int i = 0; i < dotCount; i++) {
Serial.print(".");
}
Serial.println();
totalButtonPressDuration = 0;
totalButtonPresses = 0;
}
*/
#define BTN_PIN 2
#define LED_PIN 23
SimpleTimer timer;
int btnState = HIGH;
int lastBtnState = HIGH;
unsigned long lastBtnTime = 0;
unsigned long lastPrintTime = 0;
int dotThreshold = 10; // number of dots in the visual representation
int secThreshold = 3; // number of seconds to consider a continue button push
int totalTime = 0; // total time of button presses and holds in the current minute
int cycleCount = 0; // count of button press cycles in the current minute
int minuteCount = 0; // count of minutes since boot
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("..");
}
void loop() {
// check button state
btnState = digitalRead(BTN_PIN);
// check for button press
if (btnState == LOW && lastBtnState == HIGH) {
digitalWrite(LED_PIN, HIGH);
timer.setTimeout(25, ledOff); // turn off LED after 25ms
cycleCount++;
lastBtnTime = millis();
}
// check for button release
if (btnState == HIGH && lastBtnState == LOW) {
totalTime += (millis() - lastBtnTime) / 1000; // add time to total
}
// check for continue button push
if (btnState == LOW && lastBtnState == LOW && (millis() - lastBtnTime) >= (secThreshold * 1000)) {
cycleCount++;
lastBtnTime = millis();
}
// check if a minute has passed
if ((millis() - lastPrintTime) >= 10000) {
printMinute();
lastPrintTime = millis();
}
lastBtnState = btnState;
timer.run();
}
void ledOff() {
digitalWrite(LED_PIN, LOW);
}
void printMinute() {
minuteCount++;
Serial.print("0");
Serial.print(minuteCount);
Serial.print(":");
// print visual representation of button press time
int dots = (totalTime * dotThreshold) / 60;
for (int i = 0; i < dotThreshold; i++) {
if (i < dots) {
Serial.print(".");
} else {
Serial.print(" ");
}
}
Serial.print(totalTime);
Serial.println("s");
totalTime = 0;
cycleCount = 0;
}