A
A
Al Sm2016-04-14 16:55:21
Arduino
Al Sm, 2016-04-14 16:55:21

Why doesn't the relay work?

Hello. We plan to leave for a couple of weeks, I want to do watering for flowers. But for some reason the program does not work. The humidity sensor displays some insane readings on the display (in a completely dry state 1023, in a wet napkin about 1800, although sometimes it goes off scale to 9980), maybe it's the wrong readings that I'm guided by?
Humidity sensor: FC-28

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // (RS, E, DB4, DB5, DB6, DB7)

int val = 0;
const int relayPin = 9;

void setup(){ 
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);  // установить порт как исходящий
}

void loop(){

  if (val > 0)
{
  digitalWrite(relayPin, HIGH);
  }
else
{
  digitalWrite(relayPin, LOW);
  }
  val = analogRead(0);
  lcd.setCursor(0, 0); // Устанавливаем курсор в начало 1 строки
  lcd.print("Vlaghnost:"); // Выводим текст
  lcd.setCursor(0, 1); // Устанавливаем курсор в начало 2 строки
  lcd.print(val);
}

Please tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Al Sm, 2016-04-14
@radio_mus

The issue was resolved by switching the moisture meter to a digital input ("0" for wet soil and "1" for dry soil).

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // (RS, E, DB4, DB5, DB6, DB7)

int val = 0;
const int relayPin = 9;

void setup(){ 
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT); 
}

void loop(){

  if (val != 0)
{
  digitalWrite(relayPin, HIGH);
  }
else
{
  digitalWrite(relayPin, LOW);
  }
  val = digitalRead(8);
  lcd.setCursor(0, 0); 
  lcd.print("Vlaghnost:");
  lcd.setCursor(0, 1); 
  lcd.print(val);
}

D
Dmitry, 2016-04-16
@Tomasina

val = analogRead(A0);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question