To my project i use stepper motor JK42HS34-0404 and driver L298N.
This is a wiring diagram.
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 ");
}
}
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