So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By lladoog
#67286 I have had the code below working on an arduino mini, and want to migrate it onto the Wemos D1 Mini so I can take advantage of the WIFI features.

The code controls a window blind open and closed. This works fine on arduino, but I get "soft WDT Reset Error" on the ESP8266.

I think it is to do with the delay / yield functions, but my limited programming skills have drawn a blank.

Any ideas:

Code: Select all/*
autoBLIND WITH SINGLE PUSH BUTTON OPERATION:
   -> POWER ON, DO NOTHING
   -> PRESS ONCE - BLIND CLOSE CYLCE (onboard LED on during cycle) 
   -> PRESS AGAIN - BLIND OPEN CYCLE (onboard LED on during cycle)

  version:      0.9.1
  Date:         15/06/2017
  Changelog:    FIXED - error outputs not going into low state after close
__________________________________________________________________________________*/


/*-----( Import needed libraries )-----*/
#include <Stepper.h>

// This will store the last known state of the button
const int buttonPin = D2;   // change as per your button attached.
const int ledPin = LED_BUILTIN;
int x = 0;

// variables will change:
int buttonState = 0;         
int oldButtonState = LOW;

/*---( Number of steps per revolution of INTERNAL motor in 4-step mode )*/
#define STEPS_PER_MOTOR_REVOLUTION 32
/*---( Steps per OUTPUT SHAFT of gear reduction -> 32steps motor x 64 Gear Ratio x 5 turn blind wand )---*/
#define STEPS_PER_OUTPUT_REVOLUTION 32*64*5 //  10240 steps
/*-----( Declare Stepper & Pins )-----*/
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, D5, D7, D6, D8);
/*-----( Declare Variables )-----*/
int  Steps2Take;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(LED_BUILTIN, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // Get the current state of the button
  int newButtonState = digitalRead(buttonPin);

  // Has the button gone high since we last read it?
  if (newButtonState == HIGH && oldButtonState == LOW) {

    while (x == 0) {
      // Toggle on
      digitalWrite(ledPin, HIGH);
      Serial.println("Close_Cycle_Initiated");
      Steps2Take  =  STEPS_PER_OUTPUT_REVOLUTION ;  // Rotate closed
      small_stepper.setSpeed(500);   
      small_stepper.step(Steps2Take);
      yield();
      digitalWrite(D5, LOW);
      digitalWrite(D6, LOW);
      digitalWrite(D7, LOW);
      digitalWrite(D8, LOW);
      yield();
      digitalWrite(ledPin, LOW);
      x = 1;
      Serial.println("Close_Cycle_Complete");
      }
   
    while (x ==1) {
      // Toggle off
      digitalWrite(ledPin, HIGH);
      Serial.println("Open_Cycle_Initiated");
      Steps2Take  =  - STEPS_PER_OUTPUT_REVOLUTION;  // Rotate open 
      small_stepper.setSpeed(500);  // 700 a good max speed??
      small_stepper.step(Steps2Take);
      yield();
      digitalWrite(D5, LOW);
      digitalWrite(D6, LOW);
      digitalWrite(D7, LOW);
      digitalWrite(D8, LOW);
      digitalWrite(ledPin, LOW);
      Serial.println("Open_Cycle_Complete");
      x = 0;
    }
  }
  // Store the button's state so we can tell if it's changed next time round
  oldButtonState = newButtonState;
}
User avatar
By lladoog
#67498 Thanks philbowles, that what i figured out... I have managed to get the wdt timer resetting under control by removing the While loop as your suggestion. The code hase been changed a fair bit since I am now using the Blynk platform for the control of the stepper motor routine. The code now looks like this:

Code: Select all/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Stepper.h>

/*--------------SETUP WIFI------------------------*/
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
/*-----------------------------------------------*/

//setup  virtual led & LCD
WidgetLCD lcd1(V0);

//setup gpio and the like....
int appPin1 = V2;     //Blynk virtual button
int appPin5 = V5;     //for graph test
//setup real button
int realPin1 = D2;   //real life button (paired with V1)
int motorSpeed = 500;

//VARIABLES WILL CHANGE....
int x = 0;
int buttonState = 0;         // current state of the button

/*--<MOTOR SETUP>-----------------------------------------------------------*/
/*---( Number of steps per revolution of INTERNAL motor in 4-step mode )*/
#define STEPS_PER_MOTOR_REVOLUTION 32
/*---( Steps per OUTPUT SHAFT of gear reduction -> 32steps motor x 64 Gear Ratio x 5 turn blind wand )---*/
#define STEPS_PER_OUTPUT_REVOLUTION 2048 // 1 revolution of shaft/wand   
/*-----( Declare Stepper & Pins )-----*/
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, D5, D7, D6, D8);
int  Steps2Take;
/*--<MOTOR SETUP END>--------------------------------------------------------*/

BLYNK_WRITE(V2)
{
  yield();
  {
    int pinValue = param.asInt();
    // read the pushbutton input pin:
    buttonState = digitalRead(pinValue);

    // compare the buttonState to its previous state
    if (buttonState == HIGH && x == 0)
    {
      Serial.println("close");
      lcd1.clear();
      lcd1.print(1, 0, "Closing");
      Serial.println("  ->  Close_Cycle_Initiated");

      //start closing sequence
      for (int p = 0; p < 6; ++p) {
        ESP.wdtDisable();
        small_stepper.setSpeed(motorSpeed);
        Steps2Take  =  -STEPS_PER_OUTPUT_REVOLUTION;
        small_stepper.step(Steps2Take);
        ESP.wdtEnable(100);
        Serial.println(p);
        lcd1.print(9, 0, 5 - p);  //prints a countdown of shaft revolutions from 5 to 0 on virtual lcd in blynk app
      }

      //once complete force all outputs low
      digitalWrite(D5, LOW);
      digitalWrite(D6, LOW);
      digitalWrite(D7, LOW);
      digitalWrite(D8, LOW);

      //clear LCD and ready LCD text for next input status
      lcd1.clear();
      lcd1.print(3, 0, "autoBLINDS");
      lcd1.print(2, 1, "Press To Open");

      //change value of x to 1
      x = 1;
      Serial.println("  ->  Stepper Complete");
    }

    else if (buttonState == HIGH && x == 1)
    {
      Serial.println("Open");
      lcd1.clear();
      lcd1.print(0, 0, "Opening");
      Serial.println("  ->  Open_Cycle_Initiated");

      //start Opening
      for (int p = 0; p < 6; ++p) {
        ESP.wdtDisable();
        small_stepper.setSpeed(motorSpeed);
        Steps2Take  =  -STEPS_PER_OUTPUT_REVOLUTION;
        small_stepper.step(Steps2Take);
        ESP.wdtEnable(100);
        Serial.println(p);
        lcd1.print(8, 0, (5 - p));    //prints a countdown of shaft revolutions from 5 to 0 on virtual lcd in blynk app
      }

      //once complete force all outputs low
      digitalWrite(D5, LOW);
      digitalWrite(D6, LOW);
      digitalWrite(D7, LOW);
      digitalWrite(D8, LOW);

      //clear LCD and ready LCD text for next input status
      lcd1.clear();
      lcd1.print(3, 0, "autoBLINDS");
      lcd1.print(1, 1, "Press To Close");

      //change value of x to 0
      x = 0;

      Serial.println("  ->  Stepper Complete!");
    }
  }
}

void setup()
{
  //SET PINS----
  pinMode(appPin1, INPUT);
  pinMode(appPin5, INPUT);
  pinMode(realPin1, INPUT);
  // initialize serial communication:
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  //lcd setup
  Blynk.setProperty(V0, "color", "#1b1b1c");
  Blynk.setProperty(V0, "label", "LCD");
  lcd1.clear();
  lcd1.print(1, 0, "WIFI Connected");
  lcd1.print(1, 1, "Ready....");
  delay(500);
  lcd1.clear();
  lcd1.print(3, 0, "autoBLINDS");
  lcd1.print(1, 1, "Press To Close");
  //button setup
  Blynk.setProperty(V2, "color", "#23C48E");
  Blynk.setProperty(V2, "offLabel", "Blind Ctrl");
  Blynk.setProperty(V2, "onLabel", "Go!");
}

void loop()
{
  Blynk.run();
  //timer.run();
}


The only issue I am left with is a 'Heartbeat timeout' error once the stepper has completed its routine open or closed. this is the connection between the Blynk server and the device. I guess the stepper motor takes up too much time and is code blocking, causing the timeout.

Many Thanks