Applied PLC's Technique, Concept, and Function to Microcontroller is named "Applied Microcontroller Programming" and shared my Libraries "MicroBeaut" with you.
- Debounce
- TimerOn (Timer On)
- TimerOff (Timer Off)
- TimePulse (Time Pulse)
- Blink
- Trigger
- SR (Set Dominant)
- RS (Reset Dominant)
- Toggle
- Rising (Rising Edge)
- Falling (Falling Edge)
- TimeSchedule (Schedule execution at specified time intervals)
- ScanSchedule (Schedules execution after a certain number of scans)
All of them are generic functions that can help you do Microcontroller Programming efficiently.
Timer Group used "micros()" of Arduino function for calculating elapsed time. As I call " Continuous" tasks, you can use them for multitasking programs. Still, don't forget a delay() function; you may sometimes require to use it as an initiative of some process.
Bistable and Edge Group are essential functions to detect or turn bit On / Off. Without thinking about managing their program, you can use these libraries in your program.
The Schedule is a choice for your to execute your subroutine by Time or Scans.
I hope you will enjoy Microcontroller Programming and hope these libraries may help you manage your program easily.
The debounce function delays TimeDebounce from the rising edge and falling edge of input to the output's rising edge and falling edge.
The rising edge of the input starts the timer. While the input is TRUE, after a delay of duration TimeDebounce, the timer changes the output to TRUE. The falling edge of the input starts the timer. While the input is FALSE, after a delay of duration TimeDebounce, the timer changes the output to FALSE.
MicroBeaut_DebouncefunctionVariable;functionVariable.setTimeDebounce(uint16TimeDebounceInMillisec);
boolReturnValue=functionVariable.readInput(boolVariableInput);`
boolReturnOutput=functionVariable.readStatus();uint16ReturnTimeInMillisec=functionVariable.getTimeDebounce();uint16ReturnTimeInMillisec=functionVariable.getElapsedTime();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_DebouncedebFunction; // Debounce VariableboolinputState; // Input StateboolprevInput; // Previous Input StatebooloutputState; // Output Stateconstuint16_tmsTimeDebounce=10; // Time Debounce = 10 millisecondsvoidsetup() {
pinMode(inputPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output ModedebFunction.setTimeDebounce(msTimeDebounce); // Set Time Debounce
}
voidloop() {
prevInput=inputState; // Previous Input = Current InputinputState=debFunction.readInput(digitalRead(inputPin)); // Read Input and DebounceoutputState ^= (inputState& !prevInput); // Toggle OutputdigitalWrite(LED_BUILTIN, outputState); // Write Output
}The timer on function delays TimeDelay from the input's rising edge to the output's rising edge.
The rising edge of the input starts the timer. While the input is TRUE, after a delay of duration TimeDelay, the timer changes the output to TRUE. If the input changes to FALSE at any time, the timer resets, reversing the result to FALSE and elapsed time to zero.
MicroBeaut_TimeOnfunctionVariable;functionVariable.setTimeDelay(msTimeDelay);
boolReturnValue=functionVariable.readInput(boolVariableInput);`
boolReturnOutput=functionVariable.readStatus();uint16ReturnTimeInMillisec=functionVariable.getTimeDelay();uint16ReturnTimeInMillisec=functionVariable.getElapsedTime();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_TimerOntonFunction; // Timer On VariableboolinputState; // Input StatebooloutputState; // Output Stateconstuint16_tmsTimeDelay=1000; // Time Delay = 1 Secondvoidsetup() {
pinMode(inputPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output ModetonFunction.setTimeDelay(msTimeDelay); // Set Time Delay
}
voidloop() {
inputState=digitalRead(inputPin); // Read InputoutputState=tonFunction.readInput(inputState); // Timer On Function with InputdigitalWrite(LED_BUILTIN, outputState); // Write Output
}The timer off function delays TimeDelay from the input's falling edge to the output's falling edge.
The falling edge of the input starts the timer. While the input is FALSE, after a delay of duration TimeDelay, the timer changes the output to FALSE. If the input changes to TRUE at any time, the timer resets, reversing the result to TRUE and elapsed time to zero.
MicroBeaut_TimeOnfunctionVariable;functionVariable.setTimeDelay(msTimeDelay);
boolReturnValue=functionVariable.readInput(boolVariableInput);`
boolReturnOutput=functionVariable.readStatus();uint16ReturnTimeInMillisec=functionVariable.getTimeDelay();uint16ReturnTimeInMillisec=functionVariable.getElapsedTime();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_TimerOfftofFunction; // Timer Off VariableboolinputState; // Input StatebooloutputState; // Output Stateconstuint16_tmsTimeDelay=1000; // Time Delay = 1 Secondvoidsetup() {
pinMode(inputPin, INPUT_PULLUP); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output ModetofFunction.setTimeDelay(msTimeDelay); // Set Time Delay
}
voidloop() {
inputState=digitalRead(inputPin); // Read InputoutputState=tofFunction.readInput(inputState); // Timer Off Function with InputdigitalWrite(LED_BUILTIN, outputState); // Write Output
}The time pulse function generates a pulse of duration TimeDelay starting on the rising edge of the input.
The rising edge of the input starts the timer, setting output to TRUE. While the output is TRUE, the timer ignores the input.
MicroBeaut_TimePulsefunctionVariable;functionVariable.setTimeDelay(msTimeDelay);
boolReturnValue=functionVariable.readInput(boolVariableInput);`
boolReturnOutput=functionVariable.readStatus();uint16ReturnTimeInMillisec=functionVariable.getTimeDelay();uint16ReturnTimeInMillisec=functionVariable.getElapsedTime();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_TimePulsetpFunction; // Time Pulse VariableboolinputState; // Input StatebooloutputState; // Output Stateconstuint16_tmsTimeDelay=1000; // Time Pulse = 1 Secondvoidsetup() {
pinMode(inputPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output ModetpFunction.setTimeDelay(msTimeDelay); // Set Time Pulse
}
voidloop() {
inputState=digitalRead(inputPin); // Read InputoutputState=tpFunction.readInput(inputState); // Time Pulse Function with InputdigitalWrite(LED_BUILTIN, outputState); // Write Output
}The input enables blinking. The blink function generates a pulse output that is supposed to repeatedly turn the result on and off. The reset input resets the output to FALSE and initiates the pulse-off period. TimeDelayOn specifies how long the pulse is on; TimeDelayOff specifies how long the pulse is off.
MicroBeaut_BlinkfunctionVariable;functionVariable.setTimeDelay(msTimeDelayOff, msTimeDelayOn);
boolReturnValue=functionVariable.readInput(boolVariableInput= true);`
boolReturnOutput=functionVariable.readStatus();uint16ReturnTimeInMillisec=functionVariable.getTimeDelay();uint16ReturnTimeInMillisec=functionVariable.getElapsedTime();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_BlinkblinkFunction; // Blink VariableboolenableState; // Enable State (0 = Disable, 1=Enable)booloutputState; // Output Stateconstuint16_tmsTimeDelayOff=250; // OFF Delay = 0.25 secondconstuint16_tmsTimeDelayOn=500; // ON Delay = 0.5 secondvoidsetup() {
pinMode(inputPin, INPUT_PULLUP); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output ModeblinkFunction.setTimeDelay(msTimeDelayOff, msTimeDelayOn); // Set Blink
}
voidloop() {
enableState=digitalRead(inputPin); // Read Input (0 = Disable, 1 = Enable)outputState=blinkFunction.readInput(enableState); // Blink Function with Enable InputdigitalWrite(LED_BUILTIN, outputState); // Write Output
}The trigger function sets the output only one scan.
The rising edge of the input starts the timer. While the input is TRUE, after a delay of duration TimeDelay, the timer changes the output to TRUE and restarts the timer. If the input changes to FASE at any time, the timer holds the last value.
The reset input resets the output to FALSE and resets the timer.
MicroBeaut_TriggerfunctionVariable;functionVariable.setTimeDelay(msTimeDelay);
boolReturnValue=functionVariable.readInput(boolVariableInput= true, boolVariableReset= false);`
boolReturnOutput=functionVariable.readStatus();uint16ReturnTimeInMillisec=functionVariable.getTimeDelay();uint16ReturnTimeInMillisec=functionVariable.getElapsedTime();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
#defineresetPin 9 // Define Input Pin
MicroBeaut_TriggertrigFunction; // Trigger VariableboolinputState; // Input StateboolresetState; // Input StatebooloutputState; // Output StatebooltrigState; // Trigger Stateconstuint16_tmsTimeDelay=1000; // Trigger Delay = 1 Secondvoidsetup() {
Serial.begin(115200);
pinMode(inputPin, INPUT_PULLUP); // Set Pin as an Input ModepinMode(resetPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output ModetrigFunction.setTimeDelay(msTimeDelay); // Set Trigger Delay
}
voidloop() {
inputState=digitalRead(inputPin); // Read InputresetState=digitalRead(resetPin); // Read InputoutputState=trigFunction.readInput(inputState, resetState); // Trigger Function with InputdigitalWrite(LED_BUILTIN, outputState); // Write Outputif(trigFunction.readStatus()) {
Serial.println("Applying MicroBeaut to your program will make it easier to manage your Multitasking Programming.");
}
}The RS function block is a latch with the reset input dominant over the set input. The reset input resets output to false. The set input sets the output to TRUE if reset is FALSE. If reset is FALSE and set is FALSE, the output does not change.
MicroBeaut_SRfunctionVariable;boolReturnValue=functionVariable.readInput(boolVariableSet, boolVariableReset);`
boolReturnOutput=functionVariable.readStatus();#include"MicroBeaut.h"#definesetPin 8 // Define Input Pin
#defineresetPin 9 // Define Input Pin
MicroBeaut_SRsrFunction; // SR VariableboolsetState; // Input StateboolresetState; // Input StatebooloutputState; // Output Statevoidsetup() {
pinMode(setPin, INPUT_PULLUP); // Set Pin as an Input ModepinMode(resetPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output Mode
}
voidloop() {
setState=digitalRead(setPin); // Read InputresetState=digitalRead(resetPin); // Read InputoutputState=srFunction.readInput(setState, resetState); // SR Function with Set and ResetdigitalWrite(LED_BUILTIN, outputState); // Write Output
}The RS function is a latch with the reset input dominant over the set input. The reset input resets output to FALSE. The set input sets output to TRUE if reset is false. If reset is FALSE and S is FASE, then Q1 does not change.
MicroBeaut_RSfunctionVariable;boolReturnValue=functionVariable.readInput(boolVariableSet, boolVariableReset);`
boolReturnOutput=functionVariable.readStatus();#include"MicroBeaut.h"#definesetPin 8 // Define Input Pin
#defineresetPin 9 // Define Input Pin
MicroBeaut_RSrsFunction; // RS VariableboolsetState; // Input StateboolresetState; // Input StatebooloutputState; // Output Statevoidsetup() {
pinMode(setPin, INPUT_PULLUP); // Set Pin as an Input ModepinMode(resetPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output Mode
}
voidloop() {
setState=digitalRead(setPin); // Read InputresetState=digitalRead(resetPin); // Read InputoutputState=rsFunction.readInput(setState, resetState); // RS Function with Set and ResetdigitalWrite(LED_BUILTIN, outputState); // Write Output
}The rising edge of input "Input" toggles readStatus. Input "Reset" resets readStatus (to FALSE).
MicroBeaut_TogglefunctionVariable;boolReturnValue=functionVariable.readInput(boolVariableInput, boolVariableReset= false);`
boolReturnOutput=functionVariable.readStatus();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
#defineresetPin 9 // Define Input Pin
MicroBeaut_ToggletogFunction; // RS VariableboolinputState; // Input StateboolresetState; // Input StatebooloutputState; // Output Statevoidsetup() {
pinMode(inputPin, INPUT); // Set Pin as an Input ModepinMode(resetPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output Mode
}
voidloop() {
inputState=digitalRead(inputPin); // Read InputresetState=digitalRead(resetPin); // Read InputoutputState=togFunction.readInput(inputState, resetState); // Toggle Function with Input and ResetdigitalWrite(LED_BUILTIN, outputState); // Write Output
}Set readStatus on the rising edge of Input.
MicroBeaut_RisingfunctionVariable;boolReturnValue=functionVariable.readInput(boolVariableInput);`
boolReturnOutput=functionVariable.readStatus();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_RisingreFunction; // Rising Edge VariableboolinputState; // Input StateboolprevInput; // Previous Input StatebooloutputState; // Output Statevoidsetup() {
pinMode(inputPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output Mode
}
voidloop() {
prevInput=inputState; // Previous Input = Current InputinputState=reFunction.readInput(digitalRead(inputPin)); // Read Input and Rising Edge DetectoroutputState ^= (inputState& !prevInput); // Toggle OutputdigitalWrite(LED_BUILTIN, outputState); // Write Output
}Set readStatus on the falling edge of Input.
MicroBeaut_FallingfunctionVariable;boolReturnValue=functionVariable.readInput(boolVariableInput);`
boolReturnOutput=functionVariable.readStatus();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_FallingfeFunction; // Falling Edge VariableboolinputState; // Input StateboolprevInput; // Previous Input StatebooloutputState; // Output Statevoidsetup() {
pinMode(inputPin, INPUT); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output Mode
}
voidloop() {
prevInput=inputState; // Previous Input = Current InputinputState=feFunction.readInput(digitalRead(inputPin)); // Read Input and Falling Edge DetectoroutputState ^= (inputState& !prevInput); // Toggle OutputdigitalWrite(LED_BUILTIN, outputState); // Write Output
}The TimeSchedule function is used to schedule a selected subroutine's execution after the specified period of time has passed and the output is TRUE for one scan. Otherwise, the output is FALSE. Input enables the function.
MicroBeaut_TimeSchedulefunctionVariable;functionVariable.setTimeSchedule(uint16TimeScheduleInSecond, CallbackFunction);
boolReturnValue=functionVariable.readInput(boolVariableEnable= true);#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_TimeScheduletsFunction; // Time Schedule VariableboolenableState; // Input StatebooloutputState; // Output Stateconstuint16_ttimeSchedule=1000; // Time Schedule = 1 Secondvoidsetup() {
pinMode(inputPin, INPUT_PULLUP); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output ModetsFunction.setTimeSchedule(timeSchedule, ToggleStateRoutine); // Set Time Schedule and Callback Function
}
voidloop() {
enableState=digitalRead(inputPin); // Read InputtsFunction.readInput(enableState); // Time Schedule Function with EnabledigitalWrite(LED_BUILTIN, outputState); // Write Output
}
voidToggleStateRoutine() {
outputState= !outputState;
}The ScanSchedule function is used to schedule a selected subroutine's execution after a specified number of scans occur. Input enables the function. readStatus is TRUE after the specified number of scans occur and holds TRUE for one scan.
MicroBeaut_ScanSchedulefunctionVariable;functionVariable.setScanSchedule(uIntNumberOfScan, CallbackFunction);
boolReturnValue=functionVariable.readInput(boolVariableEnable);`
boolReturnOutput=functionVariable.readStatus();uint16ReturnActualTimeInMillisec=functionVariable.Actual();#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin
MicroBeaut_ScanSchedulessFunction; // Scan Schedule VariableboolenableState; // Enable StatebooloutputState; // Output Stateconstunsigned longnumberOfScan=17450; // Number of scansvoidsetup() {
pinMode(inputPin, INPUT_PULLUP); // Set Pin as an Input ModepinMode(LED_BUILTIN, OUTPUT); // Set Pin as an Output ModessFunction.setScanSchedule(numberOfScan, ToggleStateRoutine); // Set Scan Schedule and Callback Function
}
voidloop() {
enableState=digitalRead(inputPin); // Read InputssFunction.readInput(enableState); // Scan Schedule Function with EnabledigitalWrite(LED_BUILTIN, outputState); // Write Output
}
voidToggleStateRoutine() {
outputState= !outputState;
}
