accelStepper instead of stepper library, temperature every 1min

This commit is contained in:
Bachir Soussi Chiadmi 2023-12-10 17:19:23 +01:00
parent 3eb2a581bb
commit 3d525d6a3a
1 changed files with 49 additions and 38 deletions

View File

@ -36,27 +36,27 @@ MyMessage dimmerMsg(ROTATION_CHILD_ID, V_DIMMER);
MyMessage lightMsg(ROTATION_CHILD_ID, V_LIGHT); // we need init light message to make the dimmer appear in HA MyMessage lightMsg(ROTATION_CHILD_ID, V_LIGHT); // we need init light message to make the dimmer appear in HA
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
/* // https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/
control a 28BYJ-48 stepper motor with ULN2003 driver board #include "AccelStepper.h"
https://www.makerguides.com/28byj-48-stepper-motor-arduino-tutorial/
*/
#include "Stepper.h"
// Define number of steps per rotation:
const int stepsPerRevolution = 2048; // can be 4096
// ULN2003 Wiring: // ULN2003 Wiring:
// Pin 3 to IN1 const int In1 = 3;
// Pin 4 to IN2 const int In2 = 4;
// Pin 5 to IN3 const int In3 = 5;
// Pin 6 to IN4 const int In4 = 6;
// Create stepper object called 'myStepper', note the pin order: // Create stepper object called 'myStepper', note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 3, 5, 4, 6); AccelStepper myStepper(4, In1, In3, In2, In4);
int prevDimValue=0; int prevDimValue=0;
int newDimValue=0; int newDimValue=0;
char convBuffer[10]; char convBuffer[10];
int pos=0;
unsigned long previousMillis = 0;
const long tempInterval = 60000;
float DTemp; float DTemp;
float old_DTemp; float old_DTemp;
@ -67,7 +67,7 @@ void presentation()
Serial.println("- - - - - presentation - - - - -"); Serial.println("- - - - - presentation - - - - -");
#endif #endif
sendSketchInfo("Registre Air Chaud", "0.2"); sendSketchInfo("Registre Air Chaud", "0.3");
present(ROTATION_CHILD_ID, S_DIMMER, "rotation"); present(ROTATION_CHILD_ID, S_DIMMER, "rotation");
present(CHILD_ID_TEMP, S_TEMP, "Temperature"); present(CHILD_ID_TEMP, S_TEMP, "Temperature");
@ -78,10 +78,13 @@ void setup() {
Serial.println("- - - - - setup - - - - -"); Serial.println("- - - - - setup - - - - -");
#endif #endif
myStepper.setSpeed(5); // Set the speed to 5 rpm: myStepper.setMaxSpeed(1000.0);
myStepper.setAcceleration(50.0);
myStepper.setSpeed(200);
// myStepper.moveTo(2038);
// todo make a homing of stepper motor OR find a way to sensorize where it is // todo make a homing of stepper motor OR find a way to sensorize where it is
myStepper.setCurrentPosition(0);
send(dimmerMsg.set(prevDimValue)); send(dimmerMsg.set(prevDimValue));
send(lightMsg.set(1)); send(lightMsg.set(1));
@ -93,16 +96,37 @@ void loop() {
// Serial.println("- - - - - loop - - - - -"); // Serial.println("- - - - - loop - - - - -");
#endif #endif
sensors.requestTemperatures(); unsigned long currentMillis = millis();
old_DTemp = DTemp; // do measurement every one minute
DTemp = sensors.getTempCByIndex(0); if (currentMillis - previousMillis >= tempInterval)
if (DTemp != old_DTemp && DTemp != -127)
{ {
send(msgTemp.set(DTemp, 1)); previousMillis = currentMillis;
sensors.requestTemperatures();
old_DTemp = DTemp; old_DTemp = DTemp;
DTemp = sensors.getTempCByIndex(0);
if (DTemp != old_DTemp && DTemp != -127)
{
send(msgTemp.set(DTemp, 1));
old_DTemp = DTemp;
}
} }
delay(5000); // myStepper.moveTo(myStepper.currentPosition() + 2048);
// delay(5000);
// Change direction once the motor reaches target position
// if (myStepper.distanceToGo() == 0)
// myStepper.moveTo(-myStepper.currentPosition());
// Move the motor one step
if(!myStepper.run()){
// release the stepper motor to free power consumption
digitalWrite(In1, 0);
digitalWrite(In2, 0);
digitalWrite(In3, 0);
digitalWrite(In4, 0);
}
} }
// Handle incoming messages // Handle incoming messages
@ -134,21 +158,8 @@ void receive(const MyMessage &message) {
Serial.println(newDimValue); Serial.println(newDimValue);
#endif #endif
int quart = stepsPerRevolution / 4; // how many rotation do we want max (here 45° so from 0 to 512 steps) // todo instead of 1 revolution (2038) define how much revolution we want to do for full opening (100% of the dimmer in HA)
int diff = prevDimValue - newDimValue; // get the difference between the previous and the new dimm value pos = (2038 / 100) * newDimValue;
prevDimValue = newDimValue; // record new dimm as previous dimm myStepper.moveTo(pos); // move the stepper
int steps = (quart / 100) * diff; // convert diff on percent to steps
#ifdef MY_DEBUG
Serial.print("quart: ");
Serial.println(quart);
Serial.print("diff: ");
Serial.println(diff);
Serial.print("steps: ");
Serial.println(steps);
#endif
myStepper.step(steps); // move the stepper
// todo release the stepper motor to free power consumption
} }
} }