/*
-- ========================================================
-- Subject: Applied Microcontroller Programming (AuCP)
-- Author: Montree Hamarn
-- GitHub: https://github.com/MicroBeaut
-- YouTube: What Did You Learn Today
-- ========================================================
/*
/*
-- =======================================================
-- HEADER
-- =======================================================
*/
#include "MicroBeaut.h"
#define pinSW1 A0
#define pinSW2 A1
#define pinSW3 A2
#define pinSW4 A3
#define pinLED1 6
#define pinLED2 5
#define pinLED3 4
#define pinLED4 3
#define pinLED5 2
bool stateSW1;
bool stateSW2;
bool stateSW3;
bool stateSW4;
bool stateLED1;
bool stateLED2;
bool stateLED3;
bool stateLED4;
bool stateLED5;
// ************************************
const float triggerTime = 0.5;
MicroBeaut_Trigger trigOutput;
MicroBeaut_Edge reOutput;
MicroBeaut_Edge feOutput;
MicroBeaut_TimerOn tonInitial;
unsigned long lineNumber;
String strFunctionName = "DEBOUNCE FUNCTION";
String strMessage;
bool stateFunction;
bool stateOutput;
// ************************************
/*
===================================
DATE: 16-FEB-2022
BY: Montree Hamarn
RESULT: PASSED/FAILED
===================================
*/
/*
-- =======================================================
-- DEBOUNCE FUNCTON
-- =======================================================
/*
DECLARE: MicroBeaut_Debounce variableName
OPTION 1:
void SetTimeDebounce(float TimeDebounce); // Time Debounce in second
bool Debounce(bool Input);
OPTION2:
bool Debounce(bool Input, float TimeDebounce); // Time Debounce in second
*/
/*
-- DECLARE VARIABLE FOR FUNCTION TEST
*/
MicroBeaut_Bistable toggleFunc; // Toggle Output
MicroBeaut_TimerOn tonFunction;
MicroBeaut_TimerOff tofFunction;
MicroBeaut_Blink blinkFunction;
MicroBeaut_TimePulse tpFunction;
MicroBeaut_Trigger triggerFunction;
MicroBeaut_Debounce debounceFunction;
MicroBeaut_Edge edgeFunction;
MicroBeaut_Bistable bistableFunction;
MicroBeaut_Debounce debounceSet;
MicroBeaut_Debounce debounceReset;
MicroBeaut_Bistable toggleSet;
MicroBeaut_Bistable toggleReset;
/*
/*
-- =======================================================
-- SETUP
-- =======================================================
*/
void setup() {
// AuCP Message
Serial.begin(115200);
Serial.println(msgMicroBeart);
Serial.println(strFunctionName);
while (!tonInitial.TimerOn(true,2));
Serial.print("READY GO!!!");
//TODO YOUR SETUP
pinMode(pinSW1, INPUT);
pinMode(pinSW2, INPUT);
pinMode(pinSW3, INPUT);
pinMode(pinSW4, INPUT);
pinMode(pinLED1, OUTPUT);
pinMode(pinLED2, OUTPUT);
pinMode(pinLED3, OUTPUT);
pinMode(pinLED4, OUTPUT);
pinMode(pinLED5, OUTPUT);
}
/*
-- =======================================================
-- MAIN PROGRAM (LOOP)
-- =======================================================
*/
void loop(){
// READ PUSH BUTTON
stateSW1 = digitalRead(pinSW1);
stateSW2 = digitalRead(pinSW2);
stateSW3 = digitalRead(pinSW3);
stateSW4 = digitalRead(pinSW4);
// ************************************
// TODO YOU FUNCTION TEST
// ************************************
/*
OPTION 1:
OPTION 1:
void SetTimeDebounce(float TimeDebounce);
bool Debounce(bool Input);
Format:
variableName.SetTimeDebounce(timeDebounce);
retrurnBool = variableName.Debounce(inputVariable);
Example:
MicroBeaut_Debounce debounceFunc;
debounceFunc.SetTimeDebounce(1.0); // Time Debounce = 1.0 second
retrurnBool = debounceFunc.Debounce(inputVariable); // Call Debounce Function
/*
-- OPTION SELECTION
*/
#define OPTION1
#if defined (OPTION1)
/*
-- Example OPTION1
*/
stateSW1 = toggleSet.Toggle(debounceSet.Debounce(stateSW1));
stateSW2 = toggleReset.Toggle(debounceReset.Debounce(stateSW2));
//stateOutput = bistableFunction.RS(stateSW1,stateSW2);
stateOutput = toggleFunc.Toggle(stateSW1,stateSW2);
//strMessage = "Time Delay: " + String(debounceFunction.GetTimeDebounce(),6);
//strMessage += " Elapsed Time: " + String(debounceFunction.GetElapsedTime(),6);
/*
OPTION2:
bool Debounce(bool Input, float TimeDebounce);
Format:
retrurnBool = variableName.Debounce(inputVariable, timeDebounce);
Example:
MicroBeaut_Debounce debounceFunc;
retrurnBool = debounceFunc.Debounce(inputVariable, timeDebounce); // Call Debounce Function with Time Debounce
*/
#elif defined (OPTION2)
/*
-- Example OPTION2
*/
stateFunc = debounceFunc.Debounce(stateSW1, 10.0); // Call Debounce Function with Input and TD = 1.0 second
stateOutput = toggleFunc.Toggle(stateFunc);
//strMessage = "Elapsed Time: " + String(edgeFunction.GetElapsedTime(),6);
#else
static bool _input;
static bool _prevInput;
static unsigned long _timeZero;
static unsigned long _timeDebounce = 0.01 * 1000000UL;
unsigned long _elapsedTime;
bool _output;
// INPUT
_input = stateSW1;
if (_input != _prevInput) {
_timeZero = micros();
}
if (_input != _output) {
_elapsedTime = micros() - _timeZero;
if (0UL >= _timeDebounce || _elapsedTime >= _timeDebounce) {
_output = _input;
}
}
_prevInput = _input;
// OUTPUT
stateFunc = _output;
stateOutput = toggleFunc.Toggle(stateFunc);
#endif
/*
OUTPUT:
---
*/
//stateOutput = tpFunction.TimePulse(stateOutput,1.0);
// OUTPUT -> stateLED5 = ;
// *********** END
// ************************************
stateLED1 = stateSW1;
stateLED2 = stateSW2;
stateLED3 = stateSW3;
stateLED4 = stateSW4;
stateLED5 = stateOutput;
// OUTPUT //
// ************************************
if((reOutput.Rising(stateOutput) || feOutput.Falling(stateOutput)) || (trigOutput.Trigger(true,triggerTime) & strMessage !="")) {
lineNumber++;
Serial.println(msgMicroBeart);
Serial.println(strFunctionName);
Serial.println( "OUTPUT STATE: " + String(stateOutput) + " " + strMessage);
}
// WRITE LED
digitalWrite(pinLED1,stateLED1);
digitalWrite(pinLED2,stateLED2);
digitalWrite(pinLED3,stateLED3);
digitalWrite(pinLED4,stateLED4);
digitalWrite(pinLED5,stateLED5);
}