/*
Sketch: AuCP_Blink.ino
Created: 28-Feb-2023
Author: MicroBeaut
GitHub: https://github.com/MicroBeaut/
Youtube: https://www.youtube.com/channel/UCAztmS6ogRMH5spdH0bSmEg
*/
/*
Solution For:
Topics: LED either turns on or off after each run
Category: LEDs and Multiplexing
Link: https://forum.arduino.cc/t/led-either-turns-on-or-off-after-each-run/1095268
*/
#define ledPin 7
#define startPin A0
// ************************************
// T = 1 / F
// = 1 / 1 Hz
// = 1 second(S) = 1,000 milliseconds(mS) = 1,000,000 microseconds(uS)
//
// Duty Cycle = 50 % @T= 1 S
// _______ ____
// | |- T/2 -|
// | TON | TOFF |
// ____|- T/2 -|_______|
//
// TON = 500 mS = 500,000 uS
// TOF = 500 mS = 500,000 uS
//
// *** Time Interval (Ti) For Toggle ***
// Ti = 0.5 / F
// = 500,000 uS / F
// #define F2Ti(Frequency) (500,000 uS/Frequency)
//
// ************************************
#define F2Ti(frequency) (500000UL/frequency)
const uint16_t frequency = 8; // <- Set your frequency (Hz)
const unsigned long interval = F2Ti(frequency); // Frequency to Time Interval
const bool firstState = HIGH; // FIRST state for LED
bool blink = false; // Variable for blink state
unsigned long startInterval = 0UL; // Reset start time
unsigned long count = 0;
#define MILLISEC(time) (time * 1000UL) // Define calculator for time in second
const unsigned long duration = MILLISEC(5000); // <- Set your duration n milliseconds
unsigned long startTime = 0UL;
bool startState = false; // Start state
bool pressState = false; // Press state
bool buttonState = false; // Button state
bool prevButtonState = false; // previous Button state
void InitialRoutine();
void StartStopRoutine();
void TimerRoutine();
void BlinkRoutine();
void setup() {
Serial.begin(115200);
pinMode(startPin, INPUT_PULLUP); // button pin mode
pinMode(ledPin, OUTPUT); // LED pin mode
InitialRoutine();
Serial.println("Please press start...\n");
}
void loop() {
prevButtonState = buttonState;
buttonState = digitalRead(startPin); // Read button state
pressState = !buttonState & prevButtonState; // Rising edge detector
StartStopRoutine();
TimerRoutine();
BlinkRoutine();
}
void InitialRoutine() {
startState = false; // Stop blink
digitalWrite(ledPin, LOW); // Write low state to LED
}
void StartStopRoutine() {
if (pressState) {
startState = !startState; // Invert start state
if (startState) {
startInterval = micros(); // Load blink time
blink = firstState; // Set blink with default first state
startTime = startInterval; // Load start time
count = 0; // Reset counter
digitalWrite(ledPin, firstState); // Write state to LED
Serial.print("Start Blink, ");
Serial.print("Frequency:= " + String(frequency) + " Hz, ");
Serial.println("Duration:= " + String(duration / 1000000.0f, 2) + " Second(s)...");
} else {
InitialRoutine();
Serial.println("Manual Stop Blink...\n");
}
}
}
void TimerRoutine() {
if (startState) {
unsigned long elapsedTime = micros() - startTime; // Elapsed Time
if (elapsedTime >= duration) {
InitialRoutine(); // Stop blink
Serial.println("Elapsed Time:= " + String(elapsedTime / 1000000.0f, 2) + " Second(s)");
Serial.println("Count:= " + (String(count)));
Serial.println("Auto Stop Blink...\n");
}
}
}
void BlinkRoutine() {
if (startState) {
unsigned long currInterval = micros();
if (currInterval - startInterval >= interval) {
startInterval = currInterval;
if (blink) count++;
blink = !blink; // Toggle blink state
digitalWrite(ledPin, blink); // Write state to LED
}
}
}