Momentary buttons and wifi

I'm trying to make an automatic curtain closer mechanism.
I'm using alexa to open and close the curtains, but want a 2 momentary button to move the stepper motor either way until I release the button.
I have found to separate bits of code that work perfectly independently but when i add the button code to the MQTT code the button code doesn't work?

Help and advice appreciated.

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define WIFI_SSID "BT-********"
#define WIFI_PASS "**********"

#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME ""
#define MQTT_PASS "
**"

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);

// I named my adafruit feed "openclose"
Adafruit_MQTT_Subscribe openclose = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/openclose");

boolean curtainOpen = false;

#define SLEEP 16 //D0(gpio16)
#define STEP 13 //D7(gpio13)
#define DIR 0 //D3(gpio0)
#define CW 9 //SD2(gpio9)
#define CCW 10 //SD3(gpio10)
#define STEPS_PER_ROTATION 1200

void setup()
{
Serial.begin(9600);

//Connect to WiFi
Serial.print("\n\nConnecting Wifi... ");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}

Serial.println("Connected");

//Subscribe to the openclose topic
mqtt.subscribe(&openclose);

pinMode(SLEEP, OUTPUT);
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(CW, INPUT);
pinMode(CCW, INPUT);

}

void loop()
{

// Button ----------------

{
if(digitalRead(CCW))
digitalWrite(DIR, HIGH);
else if(digitalRead(CW))
digitalWrite(DIR, LOW);
else

// These four lines result in 1 step:
if (digitalRead(CCW) || digitalRead(CW))
{
digitalWrite(STEP, HIGH);
delayMicroseconds(500);
digitalWrite(STEP, LOW);
delayMicroseconds(500);
}
}

// End of Button ----------

//Connect/Reconnect to MQTT
MQTT_connect();

//Read from our subscription queue until we run out, or
//wait up to 5 seconds for subscription to update
Adafruit_MQTT_Subscribe * subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
  //If we're in here, a subscription updated...
  if (subscription == &openclose)
  {
    //Print the new value to the serial monitor
    Serial.print("openclose: ");
    Serial.println((char*) openclose.lastread);

    //If the switch is triggered and the curtain is closed, open it
    //Otherwise, if the curtain is open, close the curtain.
    if (!strcmp((char*) openclose.lastread, "trigger") && !curtainOpen)
    {
      openCurtain();
      curtainOpen = true;
    }
    else if (!strcmp((char*) openclose.lastread, "trigger") && curtainOpen)
    {
      closeCurtain();
      curtainOpen = false;
    }
  }
}

// ping the server to keep the mqtt connection alive
if (!mqtt.ping()) {
  mqtt.disconnect();
}

}

void openCurtain() {
Serial.print("Opening the curtain...");

Serial.println(F("dir HIGH"));
digitalWrite(DIR, HIGH);
moveCurtain();
Serial.println(F("SLEEP LOW"));
digitalWrite(SLEEP, LOW);
Serial.print("Done opening curtain.");

//delay(1000);                  // waits for a second

}

void closeCurtain() {
Serial.print("Closing the curtain...");

Serial.println(F("dir LOW"));
digitalWrite(DIR, LOW);
moveCurtain();
Serial.println(F("SLEEP LOW"));
digitalWrite(SLEEP, LOW);
Serial.print("Done closing curtain");

//delay(1000);                  // waits for a second

}

// for the motor -------------

void stepNow(int totalSteps) {
Serial.print(totalSteps);
Serial.println(F(" steps."));

int i;
for (i = 0; i < totalSteps; ++i) {
  digitalWrite(STEP, HIGH);
  delay(1);
  digitalWrite(STEP, LOW);
  delay(1);
}

}

//----------------------------

void moveCurtain() {
delay(5);
stepNow(STEPS_PER_ROTATION);
}

void MQTT_connect()
{
int8_t ret;
// Stop if already connected
if (mqtt.connected())
{
return;
}

Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected
{
  Serial.println(mqtt.connectErrorString(ret));
  Serial.println("Retrying MQTT connection in 5 seconds...");
  mqtt.disconnect();
  delay(5);  // wait 5 seconds
  retries--;
  if (retries == 0)
  {
    // basically die and wait for WDT to reset me
    while (1);
  }
}
Serial.println("MQTT Connected!");

}

How are the buttons wired? Do You use pull up or pull down resistors?

    if (digitalRead(CCW))
      digitalWrite(DIR, HIGH);
    else if (digitalRead(CW))
      digitalWrite(DIR, LOW);
    else


I suggest dropping that last else. Else CW rotation will not work.

You are using D0 for the stepper. That collides with the Serial channel...

I'm using D0 for the enable pin on the stepper driver

Don't do that. It interferes with the Serial channel, Serial.print, downloading code....
Select another pin for that purpose.

In the code it says DIR pin...

changed it to D2/gpio4 now and it doesnt seem to have done anything?

Can You read the reply #2 and reply to it?

In the very beginning of loop Your code steps the motor.
Later, down in the code moveCurtain etc. is also stepping the motor.
This looks wrong to me.

Sorry for being vague, I'm very new to all this.

I borrowed the code from other projects on the internet and I'm trying to make it work for my application, it's a steep learning curve.

The resistors are 10K are wired to GND along with the input pin on the board, the other leg of the button goes to Vin on the board (I'm not sure if that is pull up or pull down) .

The button portion of the code worked when I uploaded it to the board itself, as did the other.

Thanks

Fine.
That's called "pull down" and properly done.
Can You take on the second part of #2? Then #6 and #9.

Removed the 'else' that wasn't needed.
Changed a few pins around, not sure what you mean where it steps the motor (I cant find the code)

when i pres one of the buttons the stepper appears to try and step?

What the h....? What pins?
Do You want me doing my best to help You or do You want to go on on Your own? You introduce more uncertainties doing like this. I have to spend more efforts finding out what project I'm looking at.

// These four lines result in 1 step:
if (digitalRead(CCW) || digitalRead(CW))
{
digitalWrite(STEP, HIGH);
delayMicroseconds(500);
digitalWrite(STEP, LOW);
delayMicroseconds(500);
}
}

// End of Button ----------

I suggest You comment them out. They interfere with the other stepping functions, as I mentioned in #9.

Please read every detail in the replies. Some time, some day, the reply might contain the solution. What if You jump that? I will not sit here replying for good.

Sorry, I moved DIR to pin D4, and STEP to pin D3, as you said the DIR would conflict with the serial

You told moving dir in #7. Moving step was waisted time as far as I can se.

Reading Your question again it looks like You want to move the blinds both from MQTT world and local buttons . Right?
What is the MQTT button?
You will have to find out how the MQTT way works and how to use extra parallell buttons. I don't know the MQTT stuff.

I do appreciate your time and assistance!

I added the button coding to the original code, is there a better way to do this as earlier this afternoon I had It working (with a different button code), but I would really like to have the buttons momentary rather than timed as it used the curtainclose / curtainopen code already in the main code.

You have to explore the MQTT system and find out how it works.
Suppose You step the curtain using the new buttons. How will the MQTT system act upon that? I don't know and I will not try to find out.

Could You disconnect the MQTT system some way and just use Your local buttons?

I think I have bitten off more than I can chew for a beginner!

I think I need to start from scratch work it out in the real world and then make it controllable over Alexa etc

Thanks for your help

Making steppers run as You describe it, step CW when the CW button is pressed, or CCW when the CCW button is pressed, should work quickly.
Make a temporary stepper sketch for that. There's not much needed to do that.
Bedtime here. Tomorrow?