Is there anyone here who can help me with my problem. I need to combine this 2 process so that i dont need a master and slave.
The 1st process contains the input using keypad, i will input a temperature and save it on EEPROM.The 2nd process is all about reading temperature from sht.20 temp and humidity sensor.It will then comapre it to the saved data in EEPROM and relays will turn on or off based on condition.
Im badly need someone who can help me.Im not that good a this.Thank you
Here’s my code
Master code
//SPI MASTER (ARDUINO)
#include<SPI.h>
#include<Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
byte value = 0;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup (void){
lcd.begin(20,4);
lcd.backlight();
SPI.begin(); //Begins the SPI commnuication
SPI.setClockDivider(SPI_CLOCK_DIV8); //Sets clock for SPI communication at 8 (16/8=2Mhz)
digitalWrite(SS,HIGH); // Setting SlaveSelect as HIGH (So master doesnt connnect with slave)
}
void loop(void)
{
byte Mastersend;
lcd.setCursor(0,0);
lcd.print("Enter Ref Temp:");
char key = customKeypad.getKey();
if (key != NO_KEY)
{
if ( (key >= '0') && (key<= '9') )
{
value = value *10;
value = value + key -'0';
lcd.setCursor(0,1);
lcd.print(value);
}
if ( key == '#' ) // Maximum Temperature
{
digitalWrite(SS, LOW);
// SPI.transfer(value); //Send the mastersend value to slave also receives value from slave
EEPROM.update(0,value);
lcd.clear();
value = 0;
}
if ( key == '*' ) // Minimum Temperature
{
digitalWrite(SS, LOW);
// SPI.transfer(value); //Send the mastersend value to slave also receives value from slave
EEPROM.update(1,value);
lcd.clear();
value = 0;
}
}
}
Slave Code
//Code for the slave board
#include<Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include "DFRobot_SHT20.h"
#include<SPI.h>
//i2c pins
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //
//temp&humid
DFRobot_SHT20 sht20;
// declaration of variable for relay
const int window1 = 2;
const int window2 = 3;
const int exhaustfan = 4;
const int orbitfan = 5;
const int fanheater = 6;
const int mistpump = 7;
const int heater =8;
//
int resetPin = 8;
volatile boolean received;
volatile byte Slavereceived;
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void setup() {
Serial.begin(9600);
pinMode(MISO,OUTPUT); //Sets MISO as OUTPUT (Have to Send data to Master IN
SPCR |= _BV(SPE); //Turn on SPI in Slave Mode
received = false;
SPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation
lcd.begin(20,4);
lcd.backlight();//Power on the back light
sht20.initSHT20(); // Init SHT20 Sensor
delay(100);
sht20.checkSHT20(); // Check SHT20 Sensor
// Relay modules
pinMode(window1,OUTPUT);
pinMode(window2,OUTPUT);
pinMode(exhaustfan,OUTPUT);
pinMode(orbitfan,OUTPUT);
pinMode(fanheater,OUTPUT);
pinMode(mistpump,OUTPUT);
pinMode(heater,OUTPUT);
//
pinMode(resetPin, OUTPUT);
//Temperature And Humidity
float humd = sht20.readHumidity(); // Read Humidity
float temp = sht20.readTemperature(); // Read Temperature
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(temp);
lcd.print("C");
delay(100);
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.print(humd);
lcd.print("%");
delay(100);
//
}
ISR (SPI_STC_vect){ //Inerrrput routine function
Slavereceived = SPDR; // Value received from master if store in variable slavereceived
received = true;
EEPROM.update(0,Slavereceived);//Sets received as True
resetFunc();
Serial.print("Received:");
Serial.println(Slavereceived);
lcd.setCursor(0,3);
}
void loop() {
byte eepromval = EEPROM.read(0);
byte eepromles = eepromval - 2;
Serial.println("Data");
Serial.print(eepromval);
Serial.print(eepromles);
Serial.println();
float humd = sht20.readHumidity(); // Read Humidity
float temp = sht20.readTemperature(); // Read Temperature
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(temp);
lcd.print("C");
delay(100);
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.print(humd);
lcd.print("%");
lcd.setCursor(0,3);
lcd.print("Ref Temp:");
lcd.print(eepromval);
delay(100);
v1(eepromval,eepromles);
}
void v1(byte eepromval,byte eepromles){
//Temperature And Humidity
float humd = sht20.readHumidity(); // Read Humidity
float temp = sht20.readTemperature(); // Read Temperature
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(temp);
lcd.print("C");
delay(100);
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.print(humd);
lcd.print("%");
lcd.setCursor(0,3);
lcd.print("Ref Temp:");
lcd.print(eepromval);
delay(1000);
//
if(temp >= eepromval){//process #1
lcd.setCursor(0,2);
lcd.print("Process 1");
digitalWrite(window1,LOW); // relay #1
digitalWrite(window2,HIGH); //relay #2
digitalWrite(exhaustfan,LOW); //relay #3
digitalWrite(orbitfan,LOW); //relay #4
digitalWrite(fanheater,HIGH); //relay #5
digitalWrite(mistpump,LOW); //relay #6
digitalWrite(heater,HIGH); //relay #8
}else{
v2(eepromval,eepromles);
}
}
void v2(byte eepromval,byte eepromles){
float humd = sht20.readHumidity(); // Read Humidity
float temp = sht20.readTemperature(); // Read Temperature
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(temp);
lcd.print("C");
delay(100);
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.print(humd);
lcd.print("%");
lcd.setCursor(0,3);
lcd.print("Ref Temp:");
lcd.print(eepromval);
delay(1000);
if(temp < eepromval){
lcd.setCursor(0,2);
lcd.print("Process 2");
digitalWrite(window1,LOW);
digitalWrite(window2,HIGH);
digitalWrite(exhaustfan,LOW);
digitalWrite(orbitfan,LOW);
digitalWrite(fanheater,HIGH);
digitalWrite(mistpump,HIGH);
digitalWrite(heater,HIGH);
}else{
v3(eepromval,eepromles);
}
}
void v3(byte eepromval,byte eepromles){
float humd = sht20.readHumidity(); // Read Humidity
float temp = sht20.readTemperature(); // Read Temperature
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(temp);
lcd.print("C");
delay(100);
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.print(humd);
lcd.print("%");
lcd.setCursor(0,3);
lcd.print("Ref Temp:");
lcd.print(eepromval);
delay(1000);
if(temp < 26){
lcd.setCursor(0,2);
lcd.print("Process 2");
digitalWrite(window1,HIGH);
digitalWrite(window2,LOW);
digitalWrite(exhaustfan,HIGH);
digitalWrite(orbitfan,LOW);
digitalWrite(fanheater,LOW);
digitalWrite(mistpump,HIGH);
digitalWrite(heater,LOW);
}else{
v4(eepromval,eepromles);
}
}
void v4(byte eepromval,byte eepromles){
float humd = sht20.readHumidity(); // Read Humidity
float temp = sht20.readTemperature(); // Read Temperature
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(temp);
lcd.print("C");
delay(100);
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.print(humd);
lcd.print("%");
lcd.setCursor(0,3);
lcd.print("Ref Temp:");
lcd.print(eepromval);
delay(1000);
if(temp < eepromles || temp >= 26){
lcd.setCursor(0,2);
lcd.print("Process 3");
digitalWrite(window1,LOW);
digitalWrite(window2,HIGH);
digitalWrite(exhaustfan,HIGH);
digitalWrite(orbitfan,HIGH);
digitalWrite(fanheater,HIGH);
digitalWrite(mistpump,HIGH);
digitalWrite(heater,HIGH);
}else{
v1(eepromval,eepromles);
}
}