Menu
CoddyTech

식물 모니터: 보고서

Coddy Arduino 여정의 프로젝트 섹션에 포함된 레슨. 24개 중 20번째.

마지막 단계에서는 1초마다 수분량과 상태 단어 DRY, OK 또는 WET을 보고합니다. 이 정보는 식물 옆에 있는 사람이 볼 수 있도록 LCD에 표시되고, 컴퓨터를 위해 Serial 모니터에도 출력됩니다.

LCD는 일반적인 방식으로 연결되어 있습니다. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 그리고 setup()에서 lcd.begin(16, 2)를 사용합니다. 윗줄에는 Moisture: 42%가 표시되고, 아랫줄에는 상태 단어가 표시됩니다.

Serial 모니터에서는 수분량과 상태 단어가 같은 줄에 출력되므로, 수분량을 출력할 때 println()이 아니라 Serial.print()를 사용하세요. 그런 다음 상태 표시등의 세 가지 범위에 따라 단어를 선택해 두 곳 모두에 출력하세요.

if (moisture < 30) {
  lcd.print("DRY");
  Serial.println("DRY");
} else if (moisture <= 70) {
  lcd.print("OK ");
  Serial.println("OK");
} else {
  lcd.print("WET");
  Serial.println("WET");
}

이전에 표시된 문자에 유의하세요. OK는 DRY보다 짧으므로, 공백을 포함해 "OK "를 출력하세요. 그렇지 않으면 화면에 OKY가 표시됩니다. 100%가 5%로 바뀌는 경우도 마찬가지입니다. % 뒤에 공백을 두 개 정도 출력하세요.

challenge icon

챌린지

중급

초당 한 번씩 보고하도록 작성하세요:

  • setup()에서 LCD(핀 12, 11, 5, 4, 3, 2)를 설정하세요
  • 맨 윗줄: Moisture: 42% 뒤에 공백을 넣으세요
  • 맨 아랫줄: 30% 미만이면 DRY, 30%부터 70%까지는 OK, 70% 초과이면 WET를 표시하고 공백으로 채우세요
  • 시리얼 모니터: Moisture: 42% OK와 같은 한 줄을 표시하세요

상태 표시등과 알람은 그대로 두세요. 실행을 누른 다음 확인을 누르세요.

직접 해보기

// 노브가 토양 수분 센서 역할을 합니다
const int sensorPin = A0;
// 상태 표시등: RGB LED
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 6;
// 경보 장치
const int buzzerPin = 8;

bool wasDry = false;         // 마지막 측정 때 토양이 건조했나요?
unsigned long dryStart = 0;  // 토양이 건조해진 시점

// 수분을 백분율로 표시: 0은 바싹 마른 상태, 100은 흠뻑 젖은 상태입니다.
int readMoisture() {
  int reading = analogRead(sensorPin);
  return map(reading, 0, 1023, 0, 100);
}

// 건조하면 빨간색, 적정하면 초록색, 젖어 있으면 파란색입니다.
void showStatus(int moisture) {
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);
  if (moisture < 30) {
    digitalWrite(redPin, HIGH);
  } else if (moisture <= 70) {
    digitalWrite(greenPin, HIGH);
  } else {
    digitalWrite(bluePin, HIGH);
  }
}

// 토양이 5초 넘게 건조한 상태이면 1초마다 한 번씩 경고음을 냅니다.
void checkAlarm(int moisture) {
  if (moisture < 30) {
    if (wasDry == false) {
      wasDry = true;
      dryStart = millis();
    }
    if (millis() - dryStart > 5000) {
      tone(buzzerPin, 880, 200);
    }
  } else {
    wasDry = false;
  }
}

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int moisture = readMoisture();
  Serial.print("Moisture: ");
  Serial.print(moisture);
  Serial.println("%");
  showStatus(moisture);
  checkAlarm(moisture);
  delay(1000);
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

프로젝트의 모든 레슨

직접 연습해 보세요: Arduino 플레이그라운드