mysensors_registre_airchaud/registre_airchaud.ino

121 lines
3.0 KiB
Arduino
Raw Normal View History

#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_RF24
// #define MY_RF24_PA_LEVEL RF24_PA_MIN
// #define MY_RF24_PA_LEVEL RF24_PA_LOW
#define MY_RF24_PA_LEVEL RF24_PA_HIGH
//#define MY_RF24_PA_LEVEL RF24_PA_MAX
#define MY_RF24_CHANNEL 14
#define MY_REPEATER_FEATURE
#include <SPI.h>
#include <MySensors.h>
#define ROTATION_CHILD_ID 0
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
/*
control a 28BYJ-48 stepper motor with ULN2003 driver board
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:
// Pin 3 to IN1
// Pin 4 to IN2
// Pin 5 to IN3
// Pin 6 to IN4
// Create stepper object called 'myStepper', note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 3, 5, 4, 6);
int prevDimValue=0;
int newDimValue=0;
char convBuffer[10];
void presentation()
{
#ifdef MY_DEBUG
Serial.println("- - - - - presentation - - - - -");
#endif
sendSketchInfo("Registre Air Chaud", "0.1");
present(ROTATION_CHILD_ID, S_DIMMER, "rotation");
}
void setup() {
#ifdef MY_DEBUG
Serial.println("- - - - - setup - - - - -");
#endif
myStepper.setSpeed(5); // Set the speed to 5 rpm:
// todo make a homing of stepper motor OR find a way to sensorize where it is
send(dimmerMsg.set(prevDimValue));
send(lightMsg.set(1));
}
void loop() {
#ifdef MY_DEBUG
// Serial.println("- - - - - loop - - - - -");
#endif
delay(5000);
}
// Handle incoming messages
void receive(const MyMessage &message) {
#ifdef MY_DEBUG
Serial.println("- - - - - receive - - - - -");
Serial.print("Last reading for sensor: ");
#endif
if (message.type == V_LIGHT) {
#ifdef MY_DEBUG
Serial.println("- - - - - receive V_LIGHT - - - - -");
#endif
} else if (message.type == V_DIMMER) {
#ifdef MY_DEBUG
Serial.println("- - - - - receive V_DIMMER - - - - -");
#endif
newDimValue = atoi(message.getString(convBuffer));
#ifdef MY_DEBUG
Serial.print("prevDimValue: ");
Serial.println(prevDimValue);
Serial.print("newDimValue: ");
Serial.println(newDimValue);
#endif
int quart = stepsPerRevolution / 4; // how many rotation do we want max (here 45° so from 0 to 512 steps)
int diff = prevDimValue - newDimValue; // get the difference between the previous and the new dimm value
prevDimValue = newDimValue; // record new dimm as previous dimm
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
}
}