Added timer for Blynk.run()
Imported the timer code I use in my aquirium controller.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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++) {
|
||||
|
||||
@@ -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;}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ long currentDistance = 0;
|
||||
#include <BlynkSimpleEsp8266.h>
|
||||
#include <Ethernet.h>
|
||||
#include "config.h"
|
||||
#include "timer.h"
|
||||
|
||||
//FastLED Setup
|
||||
#include <FastLED.h>
|
||||
@@ -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
|
||||
|
||||
@@ -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<countDataAmount;i++) {
|
||||
millisCount(0,i);
|
||||
}
|
||||
}
|
||||
|
||||
//Use this to check if set time has passed.
|
||||
//Will return true or false
|
||||
bool timer(unsigned long _interval,int _id) { //_interval in millis, _id in countData
|
||||
if (millisCount(1,_id) >= _interval) {
|
||||
millisCount(0,_id);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void timerReset(byte _id) { //Reset timer start time
|
||||
countData[_id]= millis();
|
||||
}
|
||||
Reference in New Issue
Block a user