typestar

greenhouse_monitor.ino en Arduino

El invernadero entero: dos lazos de histéresis y una línea CSV.

// The whole greenhouse: soil pump, heat lamp, and a serial report.

const int SOIL_PIN = A0;
const int TEMP_PIN = A2;
const int PUMP_PIN = 7;
const int HEAT_PIN = 4;

const int SOIL_DRY = 700;
const int SOIL_WET = 500;
const float TEMP_LOW_C = 16.0;
const float TEMP_HIGH_C = 20.0;

bool pumping = false;
bool heating = false;
unsigned long lastReport = 0;

float readCelsius() {
  float volts = analogRead(TEMP_PIN) * (5.0 / 1023.0);
  return (volts - 0.5) * 100.0;
}

void setup() {
  pinMode(PUMP_PIN, OUTPUT);
  pinMode(HEAT_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("time_s,soil,temp_c,pump,heat");
}

void loop() {
  int soil = analogRead(SOIL_PIN);
  float tempC = readCelsius();

  // Hysteresis on both actuators keeps the relays calm.
  if (!pumping && soil > SOIL_DRY) pumping = true;
  if (pumping && soil < SOIL_WET) pumping = false;
  if (!heating && tempC < TEMP_LOW_C) heating = true;
  if (heating && tempC > TEMP_HIGH_C) heating = false;

  digitalWrite(PUMP_PIN, pumping ? HIGH : LOW);
  digitalWrite(HEAT_PIN, heating ? HIGH : LOW);

  if (millis() - lastReport >= 5000) {
    lastReport = millis();
    Serial.print(millis() / 1000); Serial.print(',');
    Serial.print(soil); Serial.print(',');
    Serial.print(tempC); Serial.print(',');
    Serial.print(pumping); Serial.print(',');
    Serial.println(heating);
  }
}

Cómo funciona

  1. Suelo y temperatura reciben cada uno su banda de dos umbrales.
  2. readCelsius convierte el TMP36 de la manera estándar.
  3. Cada cinco segundos una fila CSV registra el estado para el plotter.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
50
Caracteres a escribir
1295
Tokens
322
Ritmo de tres estrellas
60 tpm

Al ritmo de tres estrellas de 60 tokens por minuto, este intento toma unos 322 segundos.

Escribe este fragmento

Paso 2 de 3 en Bis; paso 26 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →