homing ok, debattement ok

This commit is contained in:
Bachir Soussi Chiadmi 2023-12-16 21:34:37 +01:00
parent 3d525d6a3a
commit 852721ab34
1 changed files with 46 additions and 7 deletions

View File

@ -52,8 +52,12 @@ AccelStepper myStepper(4, In1, In3, In2, In4);
int prevDimValue=0;
int newDimValue=0;
char convBuffer[10];
const int debattement = 2038 * 5;
int pos=0;
bool homed=false;
// atmega328p pin 26 -> pc3 -> arduino A3;
const int homing_pin = A3;
int stopCourseBtnState = 0;
unsigned long previousMillis = 0;
const long tempInterval = 60000;
@ -83,10 +87,13 @@ void setup() {
myStepper.setSpeed(200);
// myStepper.moveTo(2038);
// todo make a homing of stepper motor OR find a way to sensorize where it is
myStepper.setCurrentPosition(0);
// myStepper.setCurrentPosition(0);
send(dimmerMsg.set(prevDimValue));
send(lightMsg.set(1));
pinMode(homing_pin, INPUT);
digitalWrite(homing_pin, HIGH);
// send(dimmerMsg.set(prevDimValue));
// send(lightMsg.set(1));
sensors.begin();
}
@ -119,6 +126,32 @@ void loop() {
// if (myStepper.distanceToGo() == 0)
// myStepper.moveTo(-myStepper.currentPosition());
// stopCourseBtnState = digitalRead(homing_pin);
// if (stopCourseBtnState == HIGH){
// Serial.print('stopCourseBtnState HIGH');
// }
if (!homed)
{
myStepper.setSpeed(200);
if (myStepper.distanceToGo() == 0) {
myStepper.moveTo(myStepper.currentPosition() + 2038); // turn clockwise to go up
}
stopCourseBtnState = digitalRead(homing_pin);
if (stopCourseBtnState == HIGH){
Serial.print("stopCourseBtnState is HIGH");
// stepper
myStepper.stop();
myStepper.setCurrentPosition(0);
// HA
send(dimmerMsg.set(prevDimValue));
send(lightMsg.set(1));
homed=true;
}
}
// Move the motor one step
if(!myStepper.run()){
// release the stepper motor to free power consumption
@ -158,8 +191,14 @@ void receive(const MyMessage &message) {
Serial.println(newDimValue);
#endif
// todo instead of 1 revolution (2038) define how much revolution we want to do for full opening (100% of the dimmer in HA)
pos = (2038 / 100) * newDimValue;
myStepper.moveTo(pos); // move the stepper
if (homed)
{
// todo instead of 1 revolution (2038) define how much revolution we want to do for full opening (100% of the dimmer in HA)
myStepper.setSpeed(400);
int pos = (debattement / 100) * newDimValue * -1; // turn counter clock wise to go done
myStepper.moveTo(pos); // move the stepper
}
}
}