Hello everyone,
On to a new project, I have a DC motor with an encoder that I would like to control from OpenHAB, would appreciate some help in directing me to the right place, as I have not been able to find any solutions on my own yet
Basically I need to send a custom degree value from OpenHAB to the Node, and the motor should move that many degrees, store the value and display "current" position in degrees would be ideal. (the value I sent last)
I was able to move the motor using the code below, if placed within void receive(const MyMessage &message) however it would not stop the motor at the right degree... so clearly I was doing something wrong
Right now the bellow code will move the motor to "target" which is set at 360 on the sketch
// 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>
#define CHILD_ID 6
MyMessage message(CHILD_ID, S_CUSTOM);
#define total 7560 //x4 pulses per rotation.
#define target 360
#define motorSpeed 125 //Change speed of the motor. 125 to 135 is optimal speed for perfect stops
long degree;
long pulses; //Output pulses.
int encoderA = 3;
int encoderB = 2;
const int pwm = 5; //Power of motor.
const int dir = 6;
const int dir2 = 7; //Direction of the motor.
int pulsesChanged = 5;
#define degree pulses/21 // total / 360 = total ticks per degree of shaft rotation
void setup()
{
Serial.begin(115200);
pinMode(pwm, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(dir2, OUTPUT);
analogWrite(pwm, 0); //Make sure the motor in reset mode.
digitalWrite(dir, HIGH);
digitalWrite(dir2, HIGH);
pinMode(encoderA, INPUT);
pinMode(encoderB, INPUT);
delay(50);
attachInterrupt(0, A_CHANGE, CHANGE);
attachInterrupt(1, B_CHANGE, CHANGE);
}
void presentation()
{
sendSketchInfo("Program", "1.0");
present(CHILD_ID, S_CUSTOM);
}
void loop()
{
if(degree==target){
analogWrite(pwm,0);
digitalWrite(dir,HIGH);
digitalWrite(dir2,HIGH);
delay(500);
//saveState(message.sensor, message.getBool());
//request(CHILD_ID, V_VAR1);
return;
}
else{
if (degree>target){
analogWrite(pwm,motorSpeed);
digitalWrite(dir,LOW);
digitalWrite(dir2,HIGH);
//saveState(message.sensor, message.getBool());
//request(CHILD_ID, V_VAR1);
}
else if(degree<target){
analogWrite(pwm,motorSpeed);
digitalWrite(dir,HIGH);
digitalWrite(dir2,LOW);
//saveState(message.sensor, message.getBool());
//request(CHILD_ID, V_VAR1);
}
}
if (pulsesChanged != 0) {
pulsesChanged = 0;
Serial.println(degree);
}
}
void receive(const MyMessage &message) {
saveState(message.sensor, message.getBool());
request(CHILD_ID, V_VAR1);
}
void A_CHANGE(){
if( digitalRead(encoderB) == 0 ) {
if ( digitalRead(encoderA) == 0 ) {
// A fell, B is low
pulses--; // moving reverse
} else {
// A rose, B is low
pulses++; // moving forward
}
}else {
if ( digitalRead(encoderA) == 0 ) {
// B fell, A is high
pulses++; // moving reverse
} else {
// B rose, A is high
pulses--; // moving forward
}
}
pulsesChanged = 1;
}
void B_CHANGE(){
if ( digitalRead(encoderA) == 0 ) {
if ( digitalRead(encoderB) == 0 ) {
// B fell, A is low
pulses++; // moving forward
} else {
// B rose, A is low
pulses--; // moving reverse
}
} else {
if ( digitalRead(encoderB) == 0 ) {
// B fell, A is high
pulses--; // moving reverse
} else {
// B rose, A is high
pulses++; // moving forward
}
}
pulsesChanged = 1;
}```