You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Applied PLC's Technique, Concept, and Function to Microcontroller is named "Applied Microcontroller Programming" and shared my Libraries "MicroBeaut" with you.
1. Timer
Debounce
TimerOn (Timer On)
TimerOff (Timer Off)
TimePulse (Time Pulse)
Blink
Trigger
2. Bistable
SR (Set Dominant)
RS (Reset Dominant)
Toggle
3. Edge
Rising (Rising Edge)
Falling (Falling Edge)
4. Schedule
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.
Timer
Debounce
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.
#include"MicroBeaut.h"#defineinputPin 8 // Define Input PinMicroBeaut_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}
TimerOn (Timer On)
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.
#include"MicroBeaut.h"#defineinputPin 8 // Define Input PinMicroBeaut_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}
TimerOff (Timer Off)
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.
#include"MicroBeaut.h"#defineinputPin 8 // Define Input PinMicroBeaut_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}
TimePulse (Time Pulse)
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.
#include"MicroBeaut.h"#defineinputPin 8 // Define Input PinMicroBeaut_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}
Blink
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.
#include"MicroBeaut.h"#defineinputPin 8 // Define Input PinMicroBeaut_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}
Trigger
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.
#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin#defineresetPin 9 // Define Input PinMicroBeaut_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.");}}
Bistable
SR (Set Dominant)
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.
#include"MicroBeaut.h"#definesetPin 8 // Define Input Pin#defineresetPin 9 // Define Input PinMicroBeaut_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}
RS (Reset Dominant)
The RS function is a latch with the reset input dominant over the set input. Thereset input resets output to FALSE. The set input sets output to TRUE if reset is false. If reset is FALSE andS is FASE, then Q1 does not change.
#include"MicroBeaut.h"#definesetPin 8 // Define Input Pin#defineresetPin 9 // Define Input PinMicroBeaut_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}
Toggle
The rising edge of input "Input" toggles readStatus. Input "Reset" resets readStatus (to FALSE).
#include"MicroBeaut.h"#defineinputPin 8 // Define Input Pin#defineresetPin 9 // Define Input PinMicroBeaut_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}
#include"MicroBeaut.h"#defineinputPin 8 // Define Input PinMicroBeaut_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}
Schedule
TimeSchedule (Schedule execution at specified time intervals)
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.
#include"MicroBeaut.h"#defineinputPin 8 // Define Input PinMicroBeaut_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;}
ScanSchedule (Schedules execution after a certain number of scans)
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.
#include"MicroBeaut.h"#defineinputPin 8 // Define Input PinMicroBeaut_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;}