I’m not quite sure what’s wrong with my code, but I have 3 Blynk sliders, a humidity/temp sensor and a CO2 sensor. I’m using the slider values as the settings- if the real values go below those settings, I want my relays to turn on (with a little bit of delay so the motors don’t burn out). For some reason, I can only get the humidity relay to turn on with this code. Can anyone see some issues? And I’m also not quite sure where the best place to put my sensorReadings (for example, float h = myClimate.readHumidity() to save the most space on the project.
Any thoughts?
#include <Bridge.h>
#include <BlynkSimpleYun.h>
#include <Wire.h>
#include "SparkFunHTU21D.h"
#include "kSeries.h"
HTU21D myClimate;
kSeries K_30(8, 9); //CO2 Read
unsigned long currentMillis=millis();
unsigned long interval=5000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
int tempSlider;
int co2Slider;
int humdSlider;
int lightSlider;
float t;
float h;
float co2;
int tempRelay = 10;
int co2Relay = 7;
int lightRelay = 4;
int humdRelay = 5;
//Blynk token
char auth[] = "b828e244f8b94392af33e13c346a4244";
void setup()
{
Blynk.begin(auth);
myClimate.begin();
pinMode(tempRelay,OUTPUT);
pinMode(co2Relay,OUTPUT);
pinMode(lightRelay,OUTPUT);
digitalWrite(lightRelay,LOW);
pinMode(humdRelay,OUTPUT);
}
void loop() {
Blynk.run();
}
//Climate Display..........................................................
//Display CO2 Values on V Pin 5
BLYNK_READ(5) {
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
const long interval=2000;
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
float setting = K_30.getCO2('p');
co2 = setting;
Blynk.virtualWrite(5,co2);
}
}
//Display HTU Temperature Values on V Pin 5
BLYNK_READ(0) { // temp display
float celsiusTemp = myClimate.readTemperature();
celsiusTemp = (celsiusTemp * 9.0)/ 5.0 + 32.0; //convert to fahrenheit
t = celsiusTemp;
Blynk.virtualWrite(0,t);
}
//Display HTU Humidity Values on V Pin 1
BLYNK_READ(1) { //Humidity Display
float h = myClimate.readHumidity();
Blynk.virtualWrite(1,h);
}
//Climate Slider Settings....................................
//Temperature Setting in Blynk on Virtual Pin 2
BLYNK_WRITE(2) {
int tempSetting = param.asInt();
tempSlider = tempSetting;
unsigned long currentMillis=millis();
unsigned long interval=5000;
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (t < tempSlider) {
digitalWrite(tempRelay, HIGH);
}
}
if (t >= tempSlider) {
digitalWrite(tempRelay,LOW);
}
}
//Humidity Setting in Blynk on Virtual Pin 3
BLYNK_WRITE(3) {
int humdSetting = param.asInt();
humdSlider = humdSetting;
unsigned long currentMillis=millis();
unsigned long interval=5000;
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (h < humdSlider) {
digitalWrite(humdRelay, HIGH);
}
}
if (h >= humdSlider) {
digitalWrite(humdRelay,LOW);
}
}
//Co2 Setting in Blynk on Virtual Pin 4
BLYNK_WRITE(4) {
int co2Setting = param.asInt();
co2Slider = co2Setting;
unsigned long currentMillis=millis();
unsigned long interval=5000;
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (co2 < co2Slider) {
digitalWrite(co2Relay, HIGH);
}
}
if (co2 >= co2Slider) {
digitalWrite(co2Relay,LOW);
}
}