greenhouse project

Hi guys,

My friend and I are creating a greenhouse project and would like some help with the coding as we are new to Arduino.

We’re trying to create a code which will toggle screens on an LCD when a push button is pressed. Basically we want the screen to change between Celsius and Fahrenheit by pressing the button ?

Hopefully this makes sense - ill post the code underneath

Thanks in anticipation!

Georgia and Amber :slight_smile:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int buttonPin = 7; // the number of the pushbutton pin
const int lcdPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
//int buttonState = digitalRead(buttonPin);

void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
lcd.print(β€œPLANT ANALYSIS”);
// initialize the LED pin as an output:
pinMode(lcdPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop()
{
int Temperature = analogRead(analogInPin0);
int Humidity = analogRead(analogInPin1);

lcd.setCursor(0, 2);
Serial.println(Temperature); // display Celcius
Serial.println(Humidity);
delay(500);

if (Temperature > 460)
{
lcd.print(β€œ35”);
lcd.print ((char)223); //display degrees symbol
lcd.print("C ");
if (buttonState == HIGH)
{lcd.print(β€œ35+2”);
lcd.print ((char)223); //display degrees symbol
lcd.print("F ");
}
else{}
}
else if (Temperature > 500)
{
lcd.print(β€œ30”);
lcd.print ((char)223); //display degrees symbol
lcd.print("C ");
if (buttonState == HIGH)
{lcd.print(β€œ30+2”);
lcd.print ((char)223); //display degrees symbol
lcd.print("F ");
}
else{}
}
else if (Temperature > 530)
{
lcd.print(β€œ25”);
lcd.print ((char)223); //display degrees symbol
lcd.print("C ");
if (buttonState == HIGH)
{lcd.print(β€œ25+2”);
lcd.print ((char)223); //display degrees symbol
lcd.print("F ");
}
else{}
}
else if (Temperature > 560)
{
lcd.print(β€œ20”);
lcd.print ((char)223); //display degrees symbol
lcd.print("C ");
if (buttonState == HIGH)
{lcd.print(β€œ20+2”);
lcd.print ((char)223); //display degrees symbol
lcd.print("F ");
}
else{}
}
else if (Temperature > 610)
{
lcd.print(β€œ15”);
lcd.print ((char)223); //display degrees symbol
lcd.print("C ");
if (buttonState == HIGH)
{lcd.print(β€œ15+2”);
lcd.print ((char)223); //display degrees symbol
lcd.print("F ");
}
else{}
}
else (Temperature > 630);
{
lcd.print(β€œ10”);
lcd.print ((char)223); //display degrees symbol
lcd.print("C ");
if (buttonState == HIGH)
{lcd.print(β€œ10+2”);
lcd.print ((char)223); //display degrees symbol
lcd.print("F ");
}
else{}
}

// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(lcdPin, HIGH);
}
else {
// turn LED off:
digitalWrite(lcdPin, LOW);
}

lcd.setCursor(6, 2);
if (Humidity > 1023)
{
lcd.print("Very dry ");
}
else if (Humidity > 850)
{
lcd.print("Dry ");
if (buttonState == HIGH)
{lcd.print(β€œ35+2”);
lcd.print ((char)223); //display degrees symbol
lcd.print("F ");
}
else{}
}

else if (Humidity > 370)
{
lcd.print("OK ");
}

else if (Humidity > 290)
{
lcd.print("Wet ");
}

else (Humidity > 206);
{
lcd.print("Very wet ");
}
}

If temperature is bigger than 460 ELSE if temperature is bigger than 500

Do you see a problem with this logic?

haha sorry yeah I think we got the signs the wrong way round ..... can you see if there is anything else wrong with the code ?

Thanks :D

Why do you have else{} It's totally redundant.

Why do you have these repeating blocks of code dealing with each range of 5 degrees as if they're totally different? Wouldn't it be simpler just to calculate the value you want displayed and then display it.

We had 'else' there just so we could write the other variable of the code.

We also tried to use the math.h library to automatically measure the temperature from the thermistor but for some reason the library wasn't on Arduino so we just used the serial monitor readings and converted them using a thermometer instead.

Thanks :)

See how close this gets

int Temperature = analogRead(analogInPin0);
int celcius=( (695-Temperature) * 2) /13;
lcd.print(celcius,DEC);
lcd.print ((char)223); //display degrees symbol
lcd.println("CΒ  Β  ");

that equation unfortunately still doesn’t work with our code :frowning: here’s our new code!
if you can help out with the thermistor we would be grateful

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //stating the pins the lcd is connected to

const int analogInPin0 = A0; // read the Thermistor on analog pin 0:
const int analogInPin1 = A1; // read the Moisture sensor on analog pin 1:
const int buttonPin = 7; // read the push button on digital pin 7:
const int lcdPin = 13; // read the lcd on digital pin 13:
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
// int buttonState = digitalRead(buttonPin);

// the number of the Motor pins
int pot = 0;
int relay = 3;
int motorPin = 6;

// run once, when the sketch starts
void setup()
{
lcd.begin(16,2); // set the lcd screen size
lcd.clear(); // clear the lcd screen
Serial.begin(9600); // initialize serial communication at 9600 bits per second:

// setting inputs and outputs for the motor
pinMode(pot, INPUT);
pinMode(relay, OUTPUT);
pinMode(motorPin, OUTPUT);
}

int getPot() {
int v;
v = analogRead(pot);
v /= 4;
v = max(v, 90);
v = min(v, 255);
return v;
}

int motorFoward() {
analogWrite(motorPin, getPot());
delay(1600);
digitalWrite(motorPin, LOW);
delay(1000);
digitalWrite(relay, HIGH);
// Serial.println(getIR());
delay(1000);
}

int motorFoward1() {
analogWrite(motorPin, getPot());
delay(800);
digitalWrite(motorPin, LOW);
delay(1000);
digitalWrite(relay, HIGH);
// Serial.println(getIR());
delay(2000);
}

// the loop routine runs over and over again forever:
void loop()
{
// set the cursor to correct place on top line
lcd.setCursor (0,0);
// print β€˜Plant Analysis’ to screen
lcd.print(β€œPlant Analysis”);
// reading the thermistor and moisture sensor
int Temperature = analogRead(analogInPin0);
int Moisture = analogRead(analogInPin1);
// set the cursor to the end of the second row
lcd.setCursor(0, 2);
// print temperature and moisture readings in the serial monitor
Serial.println(Temperature);
Serial.println(Moisture);
delay(500); //delay for stability

if (Temperature > 460) // when the thermistor reads less than 460 do the following:
{
lcd.print(β€œ35”); // print β€˜35’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("C "); // print β€˜C’ to lcd
if (buttonState == HIGH) // check if button is pressed
{lcd.print(β€œ95”); // print β€˜95’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("F "); // print β€˜F’ to lcd
}
else{}
motorFoward(); // when the button isnt pressed run the motor sequence
}
else if (Temperature > 500) // when the thermistor reads less than 500 do the following:
{
lcd.print(β€œ30”); // print β€˜35’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("C "); // print β€˜C’ to lcd
if (buttonState == HIGH) // check if button is pressed
{lcd.print(β€œ86”); // print β€˜86’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("F "); // print β€˜F’ to lcd
}
else{}
motorFoward1(); // when the button isnt pressed run the motor sequence
}
else if (Temperature > 530) // when the thermistor reads less than 530 do the following:
{
lcd.print(β€œ25”); // print β€˜25’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("C "); // print β€˜C’ to lcd
if (buttonState == HIGH) // check if button is pressed
{lcd.print(β€œ77”); // print β€˜77’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("F "); // print β€˜F’ to lcd
}
else{}
}
else if (Temperature > 560) // when the thermistor reads less than 560 do the following:
{
lcd.print(β€œ20”); // print β€˜20’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("C "); // print β€˜C’ to lcd
if (buttonState == HIGH) // check if button is pressed
{lcd.print(β€œ68”); // print β€˜68’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("F "); // print β€˜F’ to lcd
}
else{}
}
else if (Temperature > 610) // when the thermistor reads less than 610 do the following:
{
lcd.print(β€œ15”); // print β€˜20’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("C "); // print β€˜C’ to lcd
if (buttonState == HIGH) // check if button is pressed
{lcd.print(β€œ59”); // print β€˜59’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("F "); // print β€˜F’ to lcd
}
else{}
}

else (Temperature > 630); // when the thermistor reads less than 630 do the following:
{
lcd.print(β€œ10”); // print β€˜10’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("C "); // print β€˜C’ to lcd
if (buttonState == HIGH) // check if button is pressed
{lcd.print(β€œ50”); // print β€˜50’ to lcd
lcd.print ((char)223); // display degrees symbol
lcd.print("F "); // print β€˜F’ to lcd
}
else{}
}

// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LCD on:
digitalWrite(lcdPin, HIGH);
}
else {
// turn LCD off:
digitalWrite(lcdPin, LOW);
}

// set sursor to the sixth character on the second row
lcd.setCursor(6, 2);
if (Moisture > 1000) // when the moisture sensor reads more than 1000 do the following:
{
lcd.print("Very dry "); // print β€˜Very dry’ to lcd
}

else if (Moisture > 850) // when the moisture sensor reads more than 850 do the following:
{
lcd.print("Dry "); // print β€˜Dry’ to lcd
}

else if (Moisture > 370) // when the moisture sensor reads more than 370 do the following:
{
lcd.print("OK "); // print β€˜OK’ to lcd
}

else if (Moisture > 290) // when the moisture sensor reads more than 290 do the following:
{
lcd.print("Wet "); // print β€˜Wet’ to lcd
}

else (Moisture > 206); // when the moisture sensor reads more than 206 do the following:
{
lcd.print("Very wet "); // print β€˜Very wet’ to lcd
}
}

Please edit your post and insert code tags (button with β€œ<>” on it).

The posted code has too many problems to list and certainly will not work the way you expect.

You would be MUCH better off fixing the β€œproblem” with KenF’s proposal, than going down the rough road you are currently on.

What goes wrong when you try his code?

else if (Moisture > 850) // when the moisture sensor reads more than 850 do the following: { lcd.print("Dry "); // print 'Dry' to lcd

it is nice to see people clearly add comments to their program. it really takes the confusion out of it.

Like these?

else if (Temperature > 610) // when the thermistor reads less than 610 do the following: { lcd.print("15"); // print '20' to lcd

jremington: else if (Temperature > 610) // when the thermistor reads *_less than *610 do the following: * {* * lcd.print("15"); // *print '20'_* to lcd*

Like these?

Exactly ! I would not have understood their meaning, I was certainly thinking something totally different.