Page 1 of 1

Stepper motor with MySensors

Posted: Friday 09 September 2016 20:05
by romeok01
Hello.
To my project i use stepper motor JK42HS34-0404 and driver L298N.

This is a wiring diagram.
Image

This is a program code that I use:

Code: Select all

#include <MySensor.h>  
#include <SPI.h>
#include <AccelStepper.h>
#define HALFSTEP 4  // Stepper uses "Halfstep" mode
#define CURTAIN_CLOSED 1000 // value when closed
#define CURTAIN_OPEN 0 // value when open
#define CHILD_ID 1   // Id of the sensor child
// MySensors definitions
MySensor gw;
// Initialize message
MyMessage msg(CHILD_ID, V_TRIPPED);


// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin2, motorPin3, motorPin4);

unsigned long current_position ; // remembers current position, need to be saved in EEPROM?

void setup() {
  // MySensors
  gw.begin(); // fixed node 13
  gw.sendSketchInfo("Curtain control 13", "1.0"); // Send the sketch version information to the gateway and Controller

  //pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
  // Register all sensors to gw (they will be created as child devices)
  gw.present(CHILD_ID, S_COVER); // Window Cover sub-type, commands: V_UP, V_DOWN, V_STOP
  
  stepper1.setMaxSpeed(500.0);
  stepper1.setAcceleration(700.0);
  stepper1.moveTo(CURTAIN_OPEN);
  
  current_position = CURTAIN_OPEN ;
  stepper1.moveTo(CURTAIN_OPEN);
}//--(end setup )---

void loop() {
  // if message = V_UP start moving until closed
  // if message = V_DOWN start moving back until open
  // if message = V_STOP stop moving
  // Test: Change direction when the stepper reaches the target position
  gw.process(); // check if message from controller
  if (stepper1.distanceToGo() == 0){
    if (stepper1.currentPosition() == CURTAIN_OPEN){
      stepper1.moveTo(CURTAIN_CLOSED);
      Serial.println("Curtain Open, now closing");
      gw.request(CHILD_ID, V_UP, 0); // request new values from controller  
    }
    else{
      stepper1.moveTo(CURTAIN_OPEN);
      Serial.println("Curtain Closed, now opening");
      gw.request(CHILD_ID, V_DOWN, 0); // request new values from controller   
    }
  }
  
  stepper1.run();
}

// This is called when a message is received 
void incomingMessage(const MyMessage &message) {
  // We only expect few types of messages from controller, check which
  stepper1.moveTo(message.getInt() * CURTAIN_CLOSED/100);
  Serial.print("Message - valid: ");
  Serial.print(message.sensor);
  Serial.print(" , value: % ");
  Serial.println(message.getInt());
  switch (message.getInt()) {
  case 100:
    // Curtain should be opened
     Serial.print("Message - valid: ");
     Serial.print(message.sensor);
     Serial.print(", Message UP ");
     stepper1.moveTo(CURTAIN_OPEN);
     break;
  case 0:
    // Curtain should be closed
     Serial.print("Message - valid: ");
     Serial.print(message.sensor);
     Serial.print(", Message DOWN ");
     stepper1.moveTo(CURTAIN_CLOSED);
    break;
  case 50:
    // Curtain action should be stopped
     Serial.print("Message - valid: ");
     Serial.print(message.sensor);
     Serial.print(", Message STOP ");
    break;
  default: 
    // not recognizable message
     Serial.print("Message - valid: ");
     Serial.print(message.sensor);
     Serial.print(", Unrecognized ");

  }
 
}
When the program I have uploaded to my mysensors, the engine still turns, one left and one to the right and did not work the button blinds in Domoticz.

I cant turn off my stepper motor in Domoticz.

In the video you can see what happens
<iframe width="640" height="360" src="https://www.youtube.com/embed/1Wc1p_9YDQQ" frameborder="0" allowfullscreen></iframe>

This is log from Domoticz:

Code: Select all

2016-09-09 20:02:00.504 MySensors: Node: 7, Sketch Name: Curtain control 13
2016-09-09 20:02:00.516 MySensors: Node: 7, Sketch Version: 1.0
2016-09-09 20:02:07.527 User: Admin initiated a switch command
2016-09-09 20:02:07.628 (MySensors) Blinds (Blinds/Window)
2016-09-09 20:02:08.812 1-Wire: Searching devices...
2016-09-09 20:02:08.813 (DS18B20) Temp (Zewnątrz)
2016-09-09 20:02:08.824 (DS18B20) Temp (Pokój)
2016-09-09 20:02:10.579 User: Admin initiated a switch command
2016-09-09 20:02:10.680 (MySensors) Blinds (Blinds/Window)
2016-09-09 20:02:13.715 User: Admin initiated a switch command
2016-09-09 20:02:13.816 (MySensors) Blinds (Blinds/Window)
2016-09-09 20:02:15.362 User: Admin initiated a switch command
Please help me.

Re: Stepper motor with MySensors

Posted: Friday 09 September 2016 23:36
by romeok01
I was able to write a program on library Stepper

Program code:

Code: Select all

// 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 <Stepper.h>

#define CURTAIN_CLOSED 1000  // wartosc gdy kurtyna zamknieta
#define CURTAIN_OPEN -1000         // wartosc gdy kurtyna otwarta 
#define CHILD_ID 1

// definicje MySensors 

MyMessage message(CHILD_ID, S_COVER);

int in1Pin = 3;
int in2Pin = 4;
int in3Pin = 5;
int in4Pin = 6;
int lastState;
 
// liczba kroków na jeden obrót
#define stepsPerRevolution 200

//ustawienie szybkości silnika 
int motorSpeed = 40;

Stepper myStepper(stepsPerRevolution, in1Pin, in2Pin, in3Pin, in4Pin);

void setup() {
  
// ustawienie pinów jako wyjście
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
// ustawienie szybkosci silnika
  myStepper.setSpeed(motorSpeed);
}

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 receive(const MyMessage &message) {
  // if message = V_UP start moving until closed
  if (message.type==V_UP) {
     myStepper.step(CURTAIN_OPEN);
     // 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) {
     myStepper.step(CURTAIN_CLOSED);
     // Store state in eeprom
     saveState(message.sensor, message.getBool());
     request(CHILD_ID, V_DOWN, 0); // request new values from controller
        } 
}
I use MySensors Library 2.0

Youtube film -> https://youtu.be/gm0r2kDXrt4

Re: Stepper motor with MySensors

Posted: Tuesday 13 September 2016 11:50
by romeok01
I write program with AccelStepper library

Code: Select all

// 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.

Re: Stepper motor with MySensors

Posted: Thursday 06 October 2016 8:21
by tonrijnaard
Romeok01,

Will you please send the wiring diagram.

Thanks Ton

Re: Stepper motor with MySensors

Posted: Thursday 06 October 2016 14:12
by romeok01