I setup a push button (D40) on the app to turn on a vent fan in my greenhouse. When I push the button the fan comes on for just a sec and then turns off. I am assuming it is because of the if statements in my void loop that turns the fan on based on temp. Im guessing when the temp is below 75 it turns off the button. I am brand new to arduino and still trying to wrap my head around how if statements work . Im sure this is simple to do but I cant seen to figure it out.
this is the part of the code I think is turning off the button
//***********Cooling Code***************
if (fahrenheit >= 85)
{ digitalWrite (Relay_1, RELAY_ON);}//Greenhouse Temp Control Fan ON Above 85 Degrees
if (fahrenheit <= 75)
{ digitalWrite (Relay_1, RELAY_OFF);} //Greenhouse Temp Control coolingFan OFF Below 75 Degrees
if (fahrenheit >= 95)
{Blynk.notify("GREENHOUSE OVERHEATING!!!");}
Original Code
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
You’ll need:
- Blynk App (download from AppStore or Google Play)
- Arduino Ethernet board
- Decide how to connect to Blynk
(USB, Ethernet, Wi-Fi, Bluetooth, ...)
There is a bunch of great example sketches included to show you how to get
started. Think of them as LEGO bricks and combine them as you wish.
For example, take the Ethernet Shield sketch and combine it with the
Servo example, or choose a USB sketch and add a code from SendData
example.
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
OneWire ds(7); // on pin 7 (a 4.7K resistor is necessary) make sure you change this from the original pin 10 to an unused pin.
RTC_DS3231 rtc;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***************";
#define W5100_CS 10
#define SDCARD_CS 4
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1 40 // Arduino Digital I/O pin number
#define Relay_2 41
//#define Relay_3 0
//#define Relay_4 1
int sensorPin = 0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
void setup()
{
//lcd.init(); //initialize the lcd
//lcd.backlight(); //open the backlight
// Debug console
Serial.begin(9600);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
//Blynk.begin(auth);
// You can also specify server:
Blynk.begin(auth, "blynk-cloud.com", 8442);
//Blynk.begin(auth, IPAddress(192,168,10,222), 8442);
// Send e-mail when your hardware gets connected to Blynk Server
// Just put the recepient's "e-mail address", "Subject" and the "message body"
Blynk.email("***************", "Subject", "Greenhouse is online.");
//#ifndef ESP8266
//while (!Serial); // for Leonardo/Micro/Zero
//#endif
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on LCD Backlight
// Uncomment to set time to PC time
//rtc.begin();
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
//Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust (DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// Adjust Time on line Below
//rtc.adjust(DateTime(2017, 8, 23, 10, 36, 0));
}
Serial.begin(9600);
//---( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
//pinMode(Relay_3, OUTPUT);
//pinMode(Relay_4, OUTPUT);
//delay(1000); //Check that all relays are inactive at Reset
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
//digitalWrite(Relay_3, RELAY_OFF);
//digitalWrite(Relay_4, RELAY_OFF);
}
void loop()
{
Blynk.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
DateTime now = rtc.now();
char dateBuffer[12];
sprintf(dateBuffer,"%02u-%02u-%04u ",now.month(),now.day(),now.year());
Serial.print(dateBuffer);
sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second());
Serial.println(dateBuffer);
// Watering Schedule
if (now.hour() == 8 & now.minute() == 30 & now.second() <= 10)
{ Blynk.notify("Plants Just Wattered!");
Blynk.email("***************", "Subject: Greenhouse", "Plants Just Watered!");
lcd.clear ();
lcd.print(" Arduino is" );
lcd.setCursor(0, 1);
lcd.print("Watering Plants" );
digitalWrite(Relay_2, RELAY_ON);// Turn the Watering Relay ON
delay(300000); // Water for 5 Minutes
digitalWrite(Relay_2, RELAY_OFF);// Turn the Watering Relay OFF
}
if (now.hour() == 17 & now.minute() == 30 & now.second() <= 10 )
{ Blynk.notify("Plants Just Watered!");
Blynk.email("***************", "Subject: Greenhouse", "Plants Just Watered!");
lcd.clear ();
lcd.print(" Arduino is" );
lcd.setCursor(0, 1);
lcd.print("Watering Plants" );
digitalWrite(Relay_2, RELAY_ON);// Turn the Watering Relay ON
delay(150000); // Water for 5 Minutes
digitalWrite(Relay_2, RELAY_OFF);// Turn the Watering Relay OFF
}
//Serial.print(now.month(), DEC);
//Serial.print('/');
//Serial.print(now.day(), DEC);
//Serial.print('/');
//Serial.print(now.year(), DEC);
//Serial.print(" (");
//Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
//Serial.print(") ");
//Serial.print (now.hour(), DEC);
//Serial.print(':');
//Serial.print(now.minute(), DEC);
//Serial.print(':');
//Serial.print(now.second(), DEC);
//Serial.println();
//Remember to avoid delay() function!
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}
Serial.print("ROM =");
for ( i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
Serial.print(" Data = ");
Serial.print(present, HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
Blynk.virtualWrite(V5, fahrenheit);
//***********Cooling Code***************
if (fahrenheit >= 85)
{ digitalWrite (Relay_1, RELAY_ON);}//Greenhouse Temp Control Fan ON Above 85 Degrees
if (fahrenheit <= 75)
{ digitalWrite (Relay_1, RELAY_OFF);} //Greenhouse Temp Control coolingFan OFF Below 75 Degrees
if (fahrenheit >= 95)
{Blynk.notify("GREENHOUSE OVERHEATING!!!");}
//*************Heating Code****************
//if (fahrenheit >= 55.00)
//{ digitalWrite (Relay_2, RELAY_OFF);} //Greenhouse Temp Control Heater Fan ON Above 85 Degrees
//if (fahrenheit <= 45.00)
//{ digitalWrite (Relay_2, RELAY_ON);} //Greenhouse Temp Control Heater Fan OFF Below 80 Degrees
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
delay(1000);
Serial.print("sensor = " );
Serial.println(sensorValue);
// set up the LCD's number of columns and rows:
// Print a message to the LCD.
lcd.clear ();
lcd.setCursor(0, 0);
lcd.print ("Windy Ridge Farm");
delay(3000);
lcd.clear ();
lcd.print("Greenhouse Temp:" );
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(fahrenheit );
lcd.print((char)223);
lcd.print ("F");
delay (3000);
//Print Second Line
lcd.clear ();
lcd.setCursor(0, 0);
lcd.print ("Eastern Time:");
lcd.setCursor(0, 1);
sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
lcd.print(dateBuffer);
//lcd.print(now.hour(), DEC);
//lcd.print(':');
//lcd.print(now.minute(), DEC);
//lcd.print(':');
//lcd.print(now.second(), DEC);
Serial.println();
//delay (3000);
Blynk.virtualWrite(V1, dateBuffer);
}
Updated Code
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
You’ll need:
- Blynk App (download from AppStore or Google Play)
- Arduino Ethernet board
- Decide how to connect to Blynk
(USB, Ethernet, Wi-Fi, Bluetooth, ...)
There is a bunch of great example sketches included to show you how to get
started. Think of them as LEGO bricks and combine them as you wish.
For example, take the Ethernet Shield sketch and combine it with the
Servo example, or choose a USB sketch and add a code from SendData
example.
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define ONE_WIRE_BUS 7 //Arduino Mega Digital Pin 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
BlynkTimer timer;
RTC_DS3231 rtc;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***************";
#define W5100_CS 10
#define SDCARD_CS 4
/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1 40 // Arduino Digital I/O pin number
#define Relay_2 41
#define Relay_3 42
#define Relay_4 43
int displayState = 0;
int GreenhouseTemp; // Greenhouse temperature in F
unsigned long previousMillis1 = 0; // will store last time Display was updated
long interval = 3000; // m
//int timeState = LOW;
unsigned long previousMillis2 = 0; // will store last time Time was updated
long OnTime = 100; // milliseconds of on-time
long OffTime = 200; // milliseconds of off-time
int sensorPin = 0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
BLYNK_WRITE(V2)
{
int CFanBttn = param[1].asInt(); // assigning incoming value from pin V2 to a variable
if (CFanBttn == 0)
{
digitalWrite(Relay_1, CFanBttn);
}
else
{
digitalWrite(Relay_1, HIGH);
}
}
BLYNK_WRITE(V3)
{
int WtrBttn = param[2].asInt(); // assigning incoming value from pin V3 to a variable
if (WtrBttn == 0)
{
digitalWrite(Relay_2, WtrBttn);
}
else
{
digitalWrite (Relay_2, HIGH);
}
}
BLYNK_WRITE(V4)
{
int HFanBttn = param[3].asInt(); // assigning incoming value from pin V4 to a variable
if (HFanBttn == 0)
{
digitalWrite(Relay_3, HFanBttn);
}
else
{
digitalWrite(Relay_3, HIGH);
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
//Blynk.begin(auth);
// You can also specify server:
Blynk.begin(auth, "blynk-cloud.com", 8442);
//Blynk.begin(auth, IPAddress(192,168,10,222), 8442);
// Send e-mail when your hardware gets connected to Blynk Server
// Just put the recepient's "e-mail address", "Subject" and the "message body"
Blynk.email("***************", "Subject", "Greenhouse is online.");
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on LCD Backlight
// Uncomment to set time to PC time
//rtc.begin();
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
//Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust (DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// Adjust Time on line Below
//rtc.adjust(DateTime(2017, 8, 23, 10, 36, 0));
}
Serial.begin(9600);
//---( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
//delay(1000); //Check that all relays are inactive at Reset
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
timer.setInterval(1000L, Date_Time );
timer.setInterval(1000L, sendTemps );
timer.setInterval(3000L, LCDdisplay );
timer.setInterval(1000L, Temp );
timer.setInterval(1000L, sendTime );
}
void loop()
{
Blynk.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
timer.run();
}
void sendTemps()
{
sensors.requestTemperatures(); // Polls the sensors.
GreenhouseTemp = sensors.getTempFByIndex(0); // Stores temperature. Change to getTempCByIndex(0) for celcius.
Blynk.virtualWrite(V5, GreenhouseTemp); // Send temperature to Blynk app virtual pin 1.
}
void sendTime()
{
DateTime now = rtc.now();
char dateBuffer[12];
sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
Serial.println(dateBuffer);
Blynk.virtualWrite(V1,dateBuffer);
}
void LCDdisplay()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((displayState == 0) && (currentMillis - previousMillis1 >= interval))
{
displayState = 1; // Turn it off
// set up the LCD's number of columns and rows:
// Print a message to the LCD.
lcd.clear ();
lcd.setCursor(0, 0);
lcd.print ("Windy Ridge Farm");
}
else if ((displayState == 1) && (currentMillis - previousMillis1 >= interval))
{
displayState = 2; // turn it on
previousMillis1 = currentMillis; // Remember the time
lcd.clear ();
lcd.print("Greenhouse Temp:" );
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(GreenhouseTemp);
lcd.print((char)223);
lcd.print ("F");
}
else if((displayState == 2) && (currentMillis - previousMillis1 >= interval))
{
displayState = 0; // Turn it off
DateTime now = rtc.now();
char dateBuffer[12];
//Print Second Line
lcd.clear ();
lcd.setCursor(0, 0);
lcd.print ("Eastern Time:");
lcd.setCursor(0, 1);
sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
lcd.print(dateBuffer);
}
}
void Date_Time ()
{
DateTime now = rtc.now();
char dateBuffer[12];
sprintf(dateBuffer,"%02u-%02u-%04u ",now.month(),now.day(),now.year());
Serial.print(dateBuffer);
sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second());
Serial.println(dateBuffer);
// ********Watering Schedule***********
int SchOn1 = (now.hour() == 8 & now.minute() == 30 & now.second() <= 10);
int SchOff1= (now.hour() == 8 & now.minute() == 33 & now.second() <= 10);
int SchOn2 = (now.hour() == 17 & now.minute() == 30 & now.second() <= 10);
int SchOff2= (now.hour() == 17 & now.minute() == 33 & now.second() <= 10);
unsigned long currentMillis = millis();
if((SchOn1) && (currentMillis - previousMillis2 >= OnTime))
{
Blynk.notify("Plants Just Watered!");
Blynk.email("***************", "Subject: Greenhouse", "Plants Just Watered!");
digitalWrite(Relay_2, RELAY_ON);// Turn the Watering Relay ON
Blynk.virtualWrite(V3, RELAY_ON);
}
else if ((SchOff1) && (currentMillis - previousMillis2 >= OffTime))
{
previousMillis2 = currentMillis; // Remember the time
digitalWrite(Relay_2, RELAY_OFF);// Turn the Watering Relay OFF
Blynk.virtualWrite(V3, RELAY_OFF);
}
if((SchOn2) && (currentMillis - previousMillis2 >= OnTime))
{
Blynk.notify("Plants Just Watered!");
Blynk.email("***************", "Subject: Greenhouse", "Plants Just Watered!");
digitalWrite(Relay_2, RELAY_ON);// Turn the Watering Relay ON
Blynk.virtualWrite(V3, RELAY_ON);
}
else if ((SchOff2) && (currentMillis - previousMillis1 >= OffTime))
{
previousMillis2 = currentMillis; // Remember the time
digitalWrite(Relay_2, RELAY_OFF);// Turn the Watering Relay OFF
Blynk.virtualWrite(V3, RELAY_OFF);
}
}
void Temp()
{
//***********Cooling Code***************
if (GreenhouseTemp >= 85)
{ digitalWrite (Relay_1, RELAY_ON);//Greenhouse Temp Control Fan ON Above 85 Degrees
Blynk.virtualWrite(V2, RELAY_ON);}
else if ((GreenhouseTemp > 73) && (GreenhouseTemp< 75))
{ digitalWrite (Relay_1, RELAY_OFF); //Greenhouse Temp Control coolingFan OFF Below 75 Degrees
Blynk.virtualWrite(V2, RELAY_OFF);
}
bool tempflag = true;
if ((GreenhouseTemp >= 95) && (tempflag == true))
{
Blynk.notify("GREENHOUSE OVERHEATING!!!");
tempflag = false;
}
if (GreenhouseTemp < 95)
{
tempflag = true;
}
//*************Heating Code****************
if ((GreenhouseTemp > 55) && (GreenhouseTemp< 57))
{ digitalWrite (Relay_3, RELAY_OFF); //Greenhouse Temp Control Heater Fan OFF Above 55 Degrees F
Blynk.virtualWrite(V4, RELAY_OFF);
}
if (GreenhouseTemp <= 45)
{ digitalWrite (Relay_3, RELAY_ON); //Greenhouse Temp Control Heater Fan ON Below 45 Degrees F
Blynk.virtualWrite(V4, RELAY_ON);
}
}