From bcbcabcba834429159bdeb1e42b2d4022debb506 Mon Sep 17 00:00:00 2001 From: mrcory <33401433+mrcory@users.noreply.github.com> Date: Fri, 14 Feb 2020 12:04:14 -0500 Subject: [PATCH] Added timer for Blynk.run() Imported the timer code I use in my aquirium controller. --- shade_controller/config.h | 3 ++ shade_controller/functions.h | 11 ------- shade_controller/lightcontrol.h | 2 +- shade_controller/shade_controller.ino | 6 ++++ shade_controller/timer.h | 47 +++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 shade_controller/timer.h diff --git a/shade_controller/config.h b/shade_controller/config.h index e9d4d5f..9c4ed4a 100644 --- a/shade_controller/config.h +++ b/shade_controller/config.h @@ -115,3 +115,6 @@ int pulseMax = 250; //Max brightness for pulse mode. Max 250 or it wil //Extra Stuff #define rtcBlynk false #define cmds true //After setting up positions, cmdArduino can be disabled + +//How many ms until Blynk.run is triggered +const unsigned long blynkRefresh = 250; diff --git a/shade_controller/functions.h b/shade_controller/functions.h index db72db2..ed117f8 100644 --- a/shade_controller/functions.h +++ b/shade_controller/functions.h @@ -184,17 +184,6 @@ void motorControl() { } #endif -unsigned long timer = millis(); - -bool timerFunc(int _comp) { - if (millis() - timer >= _comp) { - return true; - timer = millis(); - } else { - return false; - } -} - void checkInvert() { if (invertMotor[0] != invertMotor[1]) { for (int i=1;i<3;i++) { diff --git a/shade_controller/lightcontrol.h b/shade_controller/lightcontrol.h index d6ec050..dfced33 100644 --- a/shade_controller/lightcontrol.h +++ b/shade_controller/lightcontrol.h @@ -13,7 +13,7 @@ void lightControl() { static bool _direction = false; //Flag for fade direction if (pulse == true) { - if (timerFunc(pulseSpeed)) { + if (timer(1,pulseSpeed)) { if (_direction == true) { if (ledBrightness < pulseMax) { ledBrightness+=2; } else {_direction = false;} } diff --git a/shade_controller/shade_controller.ino b/shade_controller/shade_controller.ino index 952a83a..7a36d91 100644 --- a/shade_controller/shade_controller.ino +++ b/shade_controller/shade_controller.ino @@ -24,6 +24,7 @@ long currentDistance = 0; #include #include #include "config.h" +#include "timer.h" //FastLED Setup #include @@ -169,6 +170,7 @@ int pwmBrightnessOut = 0; void setup() { + timerSetup(); analogWrite(PWM_PIN,0); if (lightMode == 1) { //Use FastLED if selected @@ -250,6 +252,10 @@ void loop() { //If not moving, run extra features if (stepper.distanceToGo() == 0) { + if (timer(0,blynkRefresh)) { //Run blyk every other cycle + blynkRun(); //Only run blynk when the stepper is not active + } + if (setHome == true) { resetHold(); //Set current position as home diff --git a/shade_controller/timer.h b/shade_controller/timer.h new file mode 100644 index 0000000..917deb6 --- /dev/null +++ b/shade_controller/timer.h @@ -0,0 +1,47 @@ +//Timing + +/* + * 0 - Blynk Refresh + * 1 - Pulse timer + */ + +#define countDataAmount 2 //Number of timers +unsigned long countData[countDataAmount] = {0}; //Holds count information. (Adjust for numeber of timers needed.) + + +//Used by timer function +unsigned long millisCount(int _mode, int _id) { //_mode: 0-Start 1-Stop | _id Identity number (allow more by editing the length of countData + unsigned long _count; + if (_mode == 0) { + countData[_id]= millis(); + return 0; + } + + if (_mode == 1) { + _count = millis() - countData[_id]; + return _count; + } + +} + +//Will set all variables in timer table to 0. +void timerSetup() { + for (int i=0;i= _interval) { + millisCount(0,_id); + return true; + } else { + return false; + } +} + +void timerReset(byte _id) { //Reset timer start time + countData[_id]= millis(); +}