First time writing code, am I doing it right?

This is the first time Ive written code. I'm making a greenhouse with humidity control, temperature control, and I want both readings to display on the LCD. The humidity will be controlled by a usb bottle humidifier and temperature is just a fan. Is this what my code will actually do? Lol! Thanks!

Greenhouse_Sketch.ino (1.12 KB)

Is this what my code will actually do? Lol! Thanks!

No it won't do it.

Ok could you help me out?

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int humiditySensor=A0; // sets a pin for the humidity sensor
int temperatureFan=A1; // sets a pin for the temperature fan

void setup() { // inital command on startup
  lcd.begin(16, 2); // sets up the screens number of columns and rows:
  Serial.begin(9600); // lets the Arduino talk to the computer
    delay(100); // wait .1 sec
    lcd.clear(); // clear the screen
    while (Serial.available() > 0) { // read all the available characters
      lcd.write(Serial.read()); // display each character to the screen
    }
}    



void loop() { //repeats forever
  if (humiditySensor >= 0) { // if the humidity reads "0" then...
     analogWrite (A0,HIGH); // apply 5v to A0
}
else { // if not, then...
 analogWrite (A0, LOW); // apply ground to A0
 delay (1000); // wait 1 sec
}  
{
  if (temperatureFan >= 0) { // if the temperature reads "0" then...
    analogWrite (A1,HIGH); // apply 5v to A1
}
else { // if not then...
  analogWrite (A1, LOW); // apply ground to A1
  delay (1000); // wait 1 sec
}
}
}

OP's code so people can see it.

analogWrite() gives PWM when used on PWM-capable pins. The "analog pins" are not capable of PWM/analogWrite - they are called "analog pins" because they support analogRead().
However, it looks like what you actually want is to just write the pin high or low, so you want digitalWrite(). You can freely use digitalWrite() on analog pins A0 through A5 (A6 and A7 on boards with the '328p chip can only be used for analogRead() )

You also seem to be writing to the same pin that you're reading an analog value from, which doesn't really make sense, and about half of your comments contradict your code...

There is very little that will do anything like what you seem to want it to in that code. I think you need to get back to the basics, and also read over the documentation for the builtin functions.

DrAzzy:
There is very little that will do anything like what you seem to want it to in that code. I think you need to get back to the basics, and also read over the documentation for the builtin functions.

yep... hence my short comment

Ok so I've been told that I did the code completely wrong.. I'm very new to the programing area of Arduino. Could someone help walk me through this? I have a small humidifier that I want to be turned on when humidity drops below a certain level. I also have a temperature sensor, I want a fan to be turned on when the temp reaches a certain point. Also I would like to have both temp and humidity be displayed on the lcd. Thanks!

#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int humiditySensor=A0; 
int temperatureFan=A1;

void setup() {
  lcd.begin(16, 2); 
  Serial.begin(9600);
    delay(100);
    lcd.clear();
    while (Serial.available() > 0) {
      lcd.write(Serial.read()); 
    }
}    



void loop() {
  if (humiditySensor >= x) { 
     analogWrite (A0,HIGH); 
}
else { 
 analogWrite (A0, LOW); 
 delay (1000);
}  
{
  if (temperatureFan >= x) { 
    analogWrite (A1,HIGH);
}
else { 
  analogWrite (A1, LOW); 
  delay (1000); 
}
}
}

You were told that if (humiditySensor >= x) { won't get you too far...

use AnalogRead to read the value of the pin. then compare that value with your threshold. same for temperature...

you were told that you need to look at the pins you use wether or not you want Analog or digital...

show us your work

Threads merged.

Im sorry, also new to the website, please forgive me. Ok so Im going to fix the code to the best of my ability based on what you said.

#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int humiditySensor=A0; 
int temperatureFan=A1;
int sensorValue1;
int sensorValue2;

void setup() { 
  lcd.begin(16, 2);
  Serial.begin(9600); 
    delay(100); 
    lcd.clear(); 
    while (Serial.available() > 0) { 
      lcd.write(Serial.read());
    }
}    



void loop() { 
  sensorValue1 = analogRead(humiditySensor);
  if (humiditySensor >= 0) { 
     digitalWrite (A0,HIGH); 
}
else { 
 digitalWrite (A0, LOW);
 delay (1000);
}  
{
  sensorValue2 = analogRead(temperatureFan);
  if (temperatureFan >= 0) { 
    digitalWrite (A1,HIGH); 
}
else {
  digitalWrite (A1, LOW);
  delay (1000);
}
}
}

Thank you for working with me here. Also I just want to add that I am 15, so I really am trying.

  sensorValue1 = analogRead(humiditySensor);
  if (humiditySensor >= 0) {

So, you read the value from the humidity sensor. Then what are you going to do with it next? Why not use that value in the very next line?

Just as a style note: if you have numbers in your variable names then you're labelling them incorrectly. Why not use HumidityValue and TemperatureValue instead of sensorvalue1 and sensorValue2?

In general, a pin is either an input or an output. So you either read it or you write it. You however read a pin and next write that same pin.

So you need to declare two other pins for the outputs. For now we assume that you will control using relays.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// input and output pins
const int humiditySensor = A0;
const int temperatureFan = A1;
const int relayHumidifier = 12;
const int relayHeater = 11;

// humidity and temperature
int sensorValue1;
int sensorValue2;

Note the use of the const keyword; the compiler will warn you if you try to assign a value to them by accident.

Although sensorValue1 and sensorValue2 are acceptable names, why don't you give them a name that really reflects what is stored in them? So the above could change to

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// input and output pins
const int humiditySensor = A0;
const int temperatureFan = A1;
const int relayHumidifier = 12;
const int relayHeater = 11;

// humidity and temperature
int humidity;
int temperature;

The result of an analogRead() is always a positive number from 0 to 1023. So your 'intended' test will always be true (and the test that you implemented will also always be true because himiditySensor equals A0 which is the value 14).

Let's assume that a humidity value of 512 equals 50% and you want to switch the humidifier on when the humidity drops below 50%.

void loop()
{
  // read humidity
  humidity = analogRead(humiditySensor);
  // if less than 50%
  if (humidity < 512)
  {
    // humidifier on
    digitalWrite(relayHumidifier, HIGH);
  }
  else
  {
    // humidifier off
    digitalWrite(relayHumidifier, LOW);
  }

  // rest of code
  ...
  ...
}

This leaves us with the setup() function.

void setup()
{
  // output pins
  pinMode(relayHumidifier, OUTPUT);
  pinMode(relayTemperature, OUTPUT);

  // input pins
  // analog pins don't require specific setup.
}

terminator15:
The humidity will be controlled by a usb bottle humidifier

Please explain what an usb bottle humidifier is. The word 'usb' in there worries me a bit.

Ok. Each loop you need to do some stuff.

Read the humidity.
Read the temperature.

If the humidity is different from what is currently on the lcd screen, then 
  update the lcd humidity ;
  and  make a note of  it (for the next time you loop;

If the temperature is different from what is currently on the lcd screen, then
  update the lcd temperature ;
  and make a note of  it (for the next time you loop);

Work out if the spray bottle should be on, given the current temp/humidity

If the spray bottle should be on and it currently is off, 
  turn it on;
If the spray bottle should be off and it currently is on, 
  turn it off;

Work out if the fan should be on, given the current temp/humidity

If the fan should be on and it currently is off, 
  turn it on;
If the fan should be off and it currently is on, 
  turn it off;

It gets a bit more complicated if you want to spray the spray bottle for a short period and wait a bit before checking the humidity again.

terminator15:
am I doing it right?

No! This isn't the correct way to write code. It's especially not how a beginner should write code. All these problem you have in your code show clearly that you never took the time to break your project down into individual parts and get each part working correctly. Write a simple program for each individual part and get it working perfectly and make sure you understand what the code is doing before moving to the next part:

  • Read from your humidity sensor and prints the result to the serial monitor.
  • Read from your temperature sensor and prints the result to the serial monitor.
  • Print a message to the LCD.
  • Turn the humidifier on and off.
  • Turn the fan on and off

Next you can start combining the parts, one at a time and thoroughly testing as you go along:

  • Read from the humidity sensor and print the value to the LCD.
  • Read from the humidity and temperature sensors and print the value to the LCD.
  • Control the humidifier based on the reading from the humidity sensor.
  • Control the fan based on the reading from the temperature sensor.

OK. I see I have a lot to work on. Im going to do some more research today and I will hopefully come back with a better version. Oh and the humidifier is just a 5v system. I wanted to use that instead of dealing with relays and electrical sockets. I would takes the leading coming from the usb and plug them in the Arduino like anything else. https://www.amazon.com/gp/product/B015KREV1G/ref=ox_sc_act_image_1?ie=UTF8&psc=1&smid=A1YJ6TIWTV7DBJ

I know this is far from usable, but I think its an improvement. I decided to remove the LCD for now and deal with it later. I think I need a set time for the fan/humidifier to stay on after triggered, how would I go about that? Did I set the analogRead and digitalWrite commands correctly?

const int humiditySensor=A0;
const int humidifier=8;
const int temperatureSensor=A1;
const int temperatureFan=13;

int humidityValue = 0;
int tempValue = 0; 

 void setup() {
  Serial.begin(9600);
 pinMode(temperatureFan, OUTPUT);
  pinMode(humidifier, OUTPUT);
}


void loop() { 
  humidityValue = analogRead(humiditySensor);
  if (humidityValue <512) 
  
{  
     digitalWrite (humidifier,HIGH);
}
else 
{ 
 digitalWrite (humidifier, LOW); 
}  
{
  tempValue = analogRead(temperatureSensor);
  if (temperatureFan < 512) 
{ 
    digitalWrite (temperatureFan,HIGH);    
}
else { 
  digitalWrite (temperatureFan, LOW);
}
}
Serial.println(humidityValue,tempValue);
}

There is a chance that your humidifier will draw to much current to be directly connected to an Arduino. You will have to check that.

Your temperature code will not work; look at it again.

Why do you intend to compare the temperature with the value 512? What does 512 represent? 10 degree Celsius or 10 degree Fahrenheit or 100 degree Kelvin or .....

For the time, use millis().

When you switch e.g. the fan on, store the time in a variable. Every iteration of loop you check the current time (millis() ) against the stored time till a duration has lapsed.

Some comments

In general you should make the type of variables as small as possible to save memory. For instance, will the temperatureFan pin ever have a values larger than 255 ? If, then why is it an int rather than a byte ?

You have superfluous curly brackets in you code. Not a real problem, but silly.

You might like to consider introducing some hysteresis into the sensor values used to switch the outputs to avoid too much switching on/off

 Serial.println(humidityValue, tempValue);

This is almost certainly not doing what you want.

tempValue = analogRead(temperatureSensor);
if (temperatureFan < 512)

You aren't comparing the temperature value to 512. temperatureFan appears to be an Arduino pin number but it's not clear because there's no comments at the top, like "//fan connected to this pin - write HIGH to turn on".