Ignoring 90% of my code - it works as intended...
But when I implement the Blynk timer - the code stops there. Never gets out of setup.
I also tried with the ESP Ticker - but that dies too...!
//==========================================
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// #include <Ticker.h>
#define topLED 2 // GPIO2 NodeMCU LED - near antenna - input pin is used to select WiFi mode
#define botLED 16 // GPIO16 NodeMCU LED - near USB connector
// used with target_state to reflect the status back to blynk
#define GATE_CLOSED 0
#define GATE_OPEN 1
// motor step count to match open/c.osed positions
#define CLOSED_POSITION 0
#define OPEN_POSITION 1400
#define STEP_FORWARD HIGH
#define STEP_BACKWARD LOW
// Ticker stepTimer;
BlynkTimer timer;
unsigned char target_state = GATE_CLOSED; // assume closed to begin with at reset time
unsigned long currentPosition, targetPosition;
unsigned long prev_stepMS, step_periodMS = 50L;
// EasyDriver Pins
// ESP8266 has very few I/O pins free for user...
const int smEnable = D1; //Driver enable pin
const int smDirection = D2; //Direction pin
const int smStep = D3; //Step pin
unsigned long stepDurationMS = 50L; // millisecs per half-step phase
int pinData;
//==========================================
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "token"; //MC TEST
//==========================================
// Your local WiFi credentials.for the ESP/Blynk server
// Set password to "" for open networks.
char ssid[] = "SSID";
char pass[] = "passphrase";
//==========================================
void setup() {
// pinMode (topLED, OUTPUT);
// pinMode (botLED, OUTPUT);
pinMode(smDirection, OUTPUT);
pinMode(smStep, OUTPUT);
pinMode(smEnable, OUTPUT);
digitalWrite(smEnable, HIGH);
Serial.begin(115200); // <<<<< ==== use this value in serial terminal etc..
delay(100);
Serial.println("\nStarting...\nwait for Wifi & Blynk server");
Blynk.begin(auth, ssid, pass); // can take several seconds
// digitalWrite(botLED, 0); // note the LED is inverted
Serial.println(F("Connected - the Blynk server is alive!"));
prev_stepMS = millis();
// timer.setInterval(stepDurationMS, moveStepper);
// ###########################################################
// We don't know where the motor is parked...
// so all movements will be relative to the initial position
// until some sort of end stop or calibration is available,
// ###########################################################
}
//==========================================
void loop() {
Blynk.run();
// timer.run(); // moveStepper();
}
//==========================================
// take care to accommodate reversing the
// target motor if it is still running
// in the opposite direction
// We could #define the virtual pin numbers to be more readable
// - but they still have to match the vPin assignments in the BLYNK app
//==========================================
BLYNK_WRITE(V0) { //momentary button Widget is writing to pin V0
pinData = param.asInt();
if (pinData) { // pin has been set to '1'
Serial.println(F("CLOSE THE GATES"));
}
// target_state = GATE_CLOSED;
targetPosition = CLOSED_POSITION;
}
//==========================================
BLYNK_WRITE(V1) { //momentary button Widget is writing to pin V1
pinData = param.asInt();
if (pinData) { // pin has been set to '1'
Serial.println(F("OPEN THE GATES"));
}
// target_state = GATE_OPEN;
targetPosition = OPEN_POSITION;
}
//==========================================
BLYNK_WRITE(V2) { // button Widget is writing to pin V2
pinData = param.asInt();
target_state = pinData; // !target_state;
Serial.print(F("TOGGLE GATES TO "));
if (target_state == GATE_OPEN) {
Serial.println(F("OPENING STATE"));
targetPosition = OPEN_POSITION;
} else {
Serial.println(F("CLOSING STATE"));
targetPosition = CLOSED_POSITION;
}
}
//==========================================
void moveStepper() {
static bool stepPhase = 0; // alternates 0/1
if (targetPosition != currentPosition) { // we have somewhere to go...
if (millis() - prev_stepMS > step_periodMS) { // the time is right for a step
// ###########################################################
// until some sort of end-stop or calibration is available,
// the motor & position are driven until targetPosition == currentPosition
// ###########################################################
if (targetPosition > currentPosition) { // choose forward por reverse
digitalWrite(smDirection, STEP_FORWARD);
currentPosition++;
Serial.write('+');
} else {
digitalWrite(smDirection, STEP_BACKWARD);
currentPosition--;
Serial.write('-');
}
//-----------------
stepPhase = !stepPhase; // the step pulse swaps every step_periodMS
digitalWrite(smStep, stepPhase); // toggle the step pulse
prev_stepMS = millis();
}
} else {
if ((targetPosition == OPEN_POSITION) && (target_state != GATE_OPEN)) {
target_state = GATE_OPEN;
Serial.println(F("|open"));
} else if ((targetPosition == CLOSED_POSITION) && (target_state != GATE_CLOSED)) {
target_state = GATE_CLOSED;
Serial.println(F("|closed"));
}
}
}
//==========================================
The offending line is
timer.setInterval(stepDurationMS, moveStepper);
If that's commented out - the code runs fine, but as soon as it's in-line - the setup dies - in that function ?
The other timer lines are commented out - because the above line defines the timer parameters.
Thanks for any thoughts