#include <DHT.h>
// --------------------
// DHT22
// --------------------
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// --------------------
// A4988
// --------------------
#define STEP_PIN 8
#define DIR_PIN 9
// --------------------
// Potenciometro
// --------------------
#define POT_PIN A0
// --------------------
// Pulsadores
// --------------------
#define BTN_START 4 // Boton de encendido / marcha
#define BTN_STOP 5 // Boton de paro
#define BTN_CW 6 // Sentido horario
#define BTN_CCW 7 // Sentido antihorario
// --------------------
// Motor paso a paso
// --------------------
#define PASOS_VUELTA 200
bool motorActivo = false;
bool sentidoHorario = true;
unsigned long ultimoMensaje = 0;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(BTN_START, INPUT_PULLUP);
pinMode(BTN_STOP, INPUT_PULLUP);
pinMode(BTN_CW, INPUT_PULLUP);
pinMode(BTN_CCW, INPUT_PULLUP);
Serial.begin(115200);
dht.begin();
digitalWrite(DIR_PIN, HIGH);
Serial.println("Sistema Iniciado");
}
void loop() {
// --------------------------------------------------
// Cambio de sentido permitido solo con motor detenido
// --------------------------------------------------
if (!motorActivo) {
if (digitalRead(BTN_CW) == LOW) {
sentidoHorario = true;
digitalWrite(DIR_PIN, HIGH);
delay(250);
}
if (digitalRead(BTN_CCW) == LOW) {
sentidoHorario = false;
digitalWrite(DIR_PIN, LOW);
delay(250);
}
}
// --------------------------------------------------
// Boton START: enciende el motor
// --------------------------------------------------
if (digitalRead(BTN_START) == LOW) {
motorActivo = true;
delay(250);
}
// --------------------------------------------------
// Boton STOP: apaga el motor
// --------------------------------------------------
if (digitalRead(BTN_STOP) == LOW) {
motorActivo = false;
delay(250);
}
// --------------------------------------------------
// Control de velocidad con potenciometro
// --------------------------------------------------
int valorPot = analogRead(POT_PIN);
int delayStep = map(valorPot, 0, 1023, 3000, 200);
// --------------------------------------------------
// Generacion de pulsos STEP para el A4988
// --------------------------------------------------
if (motorActivo) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(delayStep);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(delayStep);
}
// --------------------------------------------------
// Calculo aproximado de RPM
// Formula:
// RPM = 60,000,000 / (2 * delayStep * pasosPorVuelta)
// --------------------------------------------------
float rpm = 0;
if (motorActivo) {
rpm = 60000000.0 / (2.0 * delayStep * PASOS_VUELTA);
}
// --------------------------------------------------
// Envio de datos formateados para HMI VB.NET
// Formato:
// TEMP=25.4;HUM=60.0;RPM=125.5;POT=680;ESTADO=MARCHA;SENTIDO=HORARIO
// --------------------------------------------------
if (millis() - ultimoMensaje >= 1000) {
ultimoMensaje = millis();
float temperatura = dht.readTemperature();
float humedad = dht.readHumidity();
Serial.print("TEMP=");
if (isnan(temperatura)) {
Serial.print("ERROR");
} else {
Serial.print(temperatura, 1);
}
Serial.print(";HUM=");
if (isnan(humedad)) {
Serial.print("ERROR");
} else {
Serial.print(humedad, 1);
}
Serial.print(";RPM=");
Serial.print(rpm, 1);
Serial.print(";POT=");
Serial.print(valorPot);
Serial.print(";ESTADO=");
if (motorActivo) {
Serial.print("MARCHA");
} else {
Serial.print("PARO");
}
Serial.print(";SENTIDO=");
if (sentidoHorario) {
Serial.println("HORARIO");
} else {
Serial.println("ANTIHORARIO");
}
}
}