I need a reading of T&RH by 3 SHT20 sensors and use the output parameters in the loop(), but it seems the problem is when I assign the tcaselect() command.
The code I tried is below (there are also some functions for lcd menu and set clock that which should be used as is so did not upload them here):
#include <Wire.h>
#include <LiquidCrystal.h>
extern "C" {
#include "utility/twi.h" // from Wire library, so we can do bus scanning
}
#include "DFRobot_SHT20.h"
DFRobot_SHT20 sht20;
#define TCAADDR 0x70
void tcaselect (uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
//Variables and setpoints
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
#define HiTEMP 24.85
#define LoTEMP 24.2
#define Compen 0.2 // the temperature defference which is supposed to be Compensated by heating pad
#define HumSETPOINT 50 // These values are based on trial and error for acheiving the best humidity conditions
#define HumRESOLUTION 0.5
// A 1602 SainSmart LCD Keypad module has been used in this setup
LiquidCrystal lcd (8, 9, 4, 5, 6, 7); // Initialize the library with the numbers of the interface pins
// Units SETUP *************************************************
int fan = 1; // The pin number that fan is connected to
int HumFan = 13;
int HumFanSpeed = 22;
int SolHum = 2;
int SolMain = 3;
int SolEx =11;
int WB = 53;
int Hpad = 52;
#define HUMIDIFIER 12 //setting humidifier to pin 12
#define ON false //turns ON the Relay Board Contact
#define OFF true //turn OFF the Relay Board Contact
#define SensorHum 40 //For the water level switch in the humidifier
#define SensorMain 42
#define SensorEx 44
//*********************** LCD Initials ************************
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
#define MILLIS_OVERFLOW 34359738
// To control solenoid valve a time control is required
// **************** Clock Variables **********************
unsigned long currentMillis, previousMillis, elapsedMillis;
int seconds, minutes, hours;
boolean start= true;
boolean a = false;
int tcaI = 0;
void setup() {
Wire.begin(); //Initialize I2C Harware
Serial.begin(115200);
//*************INITIALIZING SENSORs*****
tcaselect(4);
sht20.initSHT20();
Serial.println("SHT20 Blue!");
sht20.checkSHT20();
tcaselect(6);
sht20.initSHT20();
Serial.println("SHT20 Green!");
sht20.checkSHT20();
tcaselect(7);
sht20.initSHT20();
Serial.println("SHT20 Yellow!");
sht20.checkSHT20();
//**********************************
lcd.begin(16, 2);
lcd.clear();
pinMode(fan, OUTPUT);
pinMode(HUMIDIFIER, OUTPUT);
digitalWrite(HUMIDIFIER, OFF);
pinMode(HumFan, OUTPUT);
digitalWrite(HumFan, OFF);
pinMode(SolMain, OUTPUT);
pinMode(SolHum, OUTPUT);
pinMode(SolEx, OUTPUT);
pinMode(WB, OUTPUT);
digitalWrite(WB, OFF);
pinMode(Hpad, OUTPUT);
digitalWrite(Hpad, OFF);
pinMode (SensorHum, INPUT_PULLUP); //For the water level switch in the humidifier
pinMode (SensorMain, INPUT_PULLUP); //For the water level switch in the sink
pinMode (SensorEx, INPUT_PULLUP); //For the water level switch in the external waterbath
//FAN Initialization *************************************************
analogWrite(fan, 255); // Fan needs to work very slowly and this is just to initialize the fan
delay(500);
analogWrite(fan, 35);
}
void loop() {
digitalWrite(SolMain, OFF); // After reseting the setup each time Select Bottun should be pressed to reset the time for Solenoid Valve Control
digitalWrite(SolHum, OFF);
digitalWrite(SolEx, OFF);
if (lcd_key == btnSELECT || start == false ) {
setClock();
start = false;
}
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
//READING Humidity and Temperature***********************
float Temp = 0;
float HUM = 0;
for (int i=1;i<5;i++){
if(i=2){ //skiping sht10 for now
i++;
}
else
{
tcaselect(i+3);
Temp =+ sht20.readTemperature();
HUM =+ sht20.readHumidity();
delay(200);
}
}
delay(100);
float hum = HUM/3;
float temp = temp/3;
Serial.println(hum);
// LCD Display and Menu
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
mainMenuDraw();
drawCursor();
lcd.setCursor(0, 0);
lcd.print("Elapsed Time:");
lcd.setCursor(7,1);
lcd.print(hours);
lcd.print(":");
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds);
break;
}
case btnLEFT:
{
mainMenuDraw();
drawCursor();
lcd.setCursor(0, 0);
lcd.print("Fan Speed:");
lcd.setCursor(7,1);
break;
}
case btnSELECT:
{
mainMenuDraw();
drawCursor();
if (lcd_key == btnSELECT && start == false) {
lcd.setCursor(0,0);
lcd.print("TEST Running !");
}
else {
lcd.setCursor(0,0);
lcd.print("TEST STARTED!");
delay(2500);
a = true;
}
break;
}
case btnUP:
{
mainMenuDraw();
drawCursor();
if (a == true){
lcd.setCursor(0, 0);
lcd.print("Next Sampling:");
lcd.setCursor(7,1);
lcd.print(59 - minutes);
lcd.print(":");
lcd.print(59 - seconds);
}
else{
lcd.setCursor(0, 0);
lcd.print("TEST Has Not ");
lcd.setCursor(0, 1);
lcd.print("Been Started Yet!");
}
break;
}
case btnNONE:
{
mainMenuDraw();
drawCursor();
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.setCursor(10,0);
lcd.print(temp,1);
lcd.setCursor(14,0);
lcd.print("C");
lcd.setCursor(10,1);
lcd.print(hum,1);
lcd.setCursor(14,1);
lcd.print("%");
if (a == true){
lcd.setCursor(15,1);
lcd.print((char)126);
}
break;
}
}
Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.print(seconds);
Serial.print(",");
Serial.print(hum);
Serial.print(",");
Serial.println(temp);
}