Buongiorno, sto facendo una porta per pollaio automatica in modo tale che si apra e si chiuda in automatico da alexa con un motore stepper e una vite senza fine. Sto utilizzando una scheda esp8266. ho impostato che all'accensione da app della scheda, il motore faccia 8300 step (ovvero scorra su tutta la lunghezza della vite), però finchè fa questa azione rimane bloccato il resto del programma e di conseguenza ad alexa risulta che la scheda non risponda. come posso fare per poter far girare il motore intanto che va avanti il resto del codice?
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
// define the GPIO connected with Relays
#define RelayPin1 5 //D1
// prototypes
boolean connectWifi();
//callback functions
void firstLightChanged(uint8_t brightness);
// WiFi Credentials
const char* ssid = "CipollinoNet_2MHz";
const char* password = "calinetwork201";
// device names
String Device_1_Name = "Pollaio Cuz";
boolean wifiConnected = false;
Espalexa espalexa;
#include <Stepper.h>
const int stepsPerRevolution = 1000; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 14, 12, 13, 15);
int stepCount = 0;
void setup()
{
Serial.begin(115200);
pinMode(RelayPin1, OUTPUT);
// Initialise wifi connection
wifiConnected = connectWifi();
if (wifiConnected)
{
// Define your devices here.
espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
espalexa.begin();
}
else
{
while (1)
{
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
delay(2500);
}
}
}
void loop()
{
espalexa.loop();
delay(1);
}
//our callback functions
void firstLightChanged(uint8_t brightness)
{
//Control the device
if (brightness == 255)
{
digitalWrite(RelayPin1, HIGH);
for(int n = 0; n<8300; n++)
{myStepper.step(1);
delay(2);}
Serial.println("Device1 ON");
}
else
{
digitalWrite(RelayPin1, LOW);
for(int n = 0; n<8300; n++)
{myStepper.step(-1);
delay(2);}
Serial.println("Device1 OFF");
}
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi()
{
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 20) {
state = false; break;
}
i++;
}
Serial.println("");
if (state) {
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("Connection failed.");
}
return state;
}