I write program with AccelStepper library
/ Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE
#include <SPI.h>
#include <MySensors.h>
#include <AccelStepper.h> //import biblioteki AccelStepper
#define HALFSTEP 8
#define CURTAIN_CLOSED 2000 // wartosc gdy kurtyna zamknieta
#define CURTAIN_OPEN 0 // wartosc gdy kurtyna otwarta
#define CHILD_ID 1
// definicje MySensors
MyMessage message(CHILD_ID, S_COVER);
// Definicja pinow silnika
#define IN1 3 // IN1 - zielony
#define IN2 4 // IN2 - czarny
#define IN3 5 // IN3 - niebieski
#define IN4 6 // IN4 - czerwony
AccelStepper stepper1(HALFSTEP, IN1, IN2, IN3, IN4);
void setup()
{
stepper1.setMaxSpeed(200.0);
stepper1.setAcceleration(1000.0);
stepper1.setSpeed(200);
stepper1.moveTo(CURTAIN_OPEN);
}
void presentation()
{
// Wyslanie informacji o wersji programu
sendSketchInfo("Program kurtyna", "1.0");
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID, S_COVER); // Window Cover sub-type, commands: V_UP, V_DOWN, V_STOP
}
void loop()
{
stepper1.run(); //Start
}
void receive(const MyMessage &message)
{
// if message = V_UP start moving until closed
if (message.type==V_UP) {
if (stepper1.distanceToGo() == 0){
if (stepper1.currentPosition() == CURTAIN_OPEN){
stepper1.moveTo(CURTAIN_CLOSED);
// Store state in eeprom
saveState(message.sensor, message.getBool());
request(CHILD_ID, V_UP, 0); // request new values from controller
}
}
}
if (message.type==V_DOWN) {
stepper1.moveTo(CURTAIN_OPEN);
// Store state in eeprom
saveState(message.sensor, message.getBool());
request(CHILD_ID, V_DOWN, 0); // request new values from controller
}
if (message.type==V_STOP) {
stepper1.setCurrentPosition(0);
// Store state in eeprom
saveState(message.sensor, message.getBool());
request(CHILD_ID, V_STOP, 0); // request new values from controller
}
}
I use MySensors library 2.0
Now i working.