typestar

greenhouse_monitor.ino in Arduino

The whole greenhouse: two hysteresis loops and a CSV report line.

// 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);
  }
}

How it works

  1. Soil and temperature each get a two-threshold band.
  2. readCelsius converts the TMP36 the standard way.
  3. Every five seconds one CSV row logs state for the plotter.

Keywords and builtins used here

The run, in numbers

Lines
50
Characters to type
1295
Tokens
322
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 322 seconds.

Type this snippet

Step 2 of 3 in Encore, step 26 of 27 in Language basics.

← Previous Next →