I'm an engineering student but unfortunately I know nothing about code besides basic if and else statements that they teach in C introduction. Despite that I was able to put a hodge podge of code together and get it to do what I wanted, except one part.
I've got the temperature relay to give a reading. I figured out how to get it displayed on a LCD. Then I figured out how to get the relay working. All which were thankfully easy. I somehow figured out how to communicate through serial with the arduino so when a certain number or phrase is typed in, it takes the number converts it to an int which I can then use to control the relay. If the temp is 20 and I put 19 it says cooling. If i put 21 it says heating. The problem is that when the temperature rises or lowers to match that temperature the relay does not switch to fix the temp.
//read value from serial
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string, then the string's value:
if (inChar == '\n') {
Serial.print("New Temp:");
Serial.println(inString.toInt());
//Serial.print("String: ");
//Serial.println(inString);
// clear the string for new input:
NewTemp = inString.toInt();
Serial.println(NewTemp);
inString = "";
}
if (fixtemp > NewTemp){
digitalWrite(pinOut1, HIGH);
digitalWrite(pinOut2, HIGH);
lcd.setCursor(0, 2);
lcd.print("COOLING");
}
else if (fixtemp < NewTemp){
digitalWrite(pinOut2, HIGH);
digitalWrite(pinOut1, LOW);
lcd.setCursor(0, 3);
lcd.print("HEATING");
lcd.setCursor(0, 2);
}
I don't know if I'm just not seeing something but I thought with the way it's written, the relays would be able to actuate by themselves. As it is now the relays only actuate when a temperature is entered through the serial monitor and does not activate to counteract if the temperature is going above or below the temperature indicated through serial. Hope that makes sense.
I'm using an Arduino Uno, KY-028 NTC Temperature sensor, Sim900 and a regular 5v operated 2 channel relay.
Entirety of Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String textMessage;
int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
int pinOut1 = 10;
int pinOut2 = 11;
float i = 0;
float fixtemp = 20.;
int fixInput = 560;
float fixedDegreeValue = 5.5;
int incomingByte = 0;
String inString = "";
int inChar = "";
int NewTemp;
void setup ()
{
Serial.begin (9600);
SIM900.begin(9600);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
lcd.begin(16,2);
lcd.backlight();
// Give time to your GSM shield log on to network
delay(9000);
Serial.print("SIM900 ready...");
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
void loop ()
{
lcd.setCursor(0, 0);
lcd.print("TEMP CONTROL");
sensorValue = analogRead(sensorPin);
if (sensorValue > fixInput ){ // ice cube
// Serial.print(sensorValue);
i = sensorValue - fixInput;
i = i /fixedDegreeValue;
//Serial.print(sensorValue);
// Serial.print(" and i = ");
// Serial.println(i);
Serial.print("Temperature = ");
fixtemp = fixtemp - i;
fixtemp = fixtemp - 71;
Serial.print(fixtemp);
Serial.println(" C");
}
else if(sensorValue < fixInput) //thea cup
{
i = fixInput - sensorValue;
i = i /fixedDegreeValue;
// Serial.print(sensorValue);
// Serial.print(" and i = ");
// Serial.println(i);
Serial.print("Temperature = ");
fixtemp = fixtemp + i;
fixtemp = fixtemp - 71;
Serial.print(fixtemp);
Serial.println(" C");
}
/*if(SIM900.available()>0){
textMessage = SIM900.readString();
Serial.print(textMessage);
delay(10);
}*/
//read value from serial
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string, then the string's value:
if (inChar == '\n') {
Serial.print("New Temp:");
Serial.println(inString.toInt());
//Serial.print("String: ");
//Serial.println(inString);
// clear the string for new input:
NewTemp = inString.toInt();
Serial.println(NewTemp);
inString = "";
}
if (fixtemp > NewTemp){
digitalWrite(pinOut1, HIGH);
digitalWrite(pinOut2, HIGH);
lcd.setCursor(0, 2);
lcd.print("COOLING");
}
else if (fixtemp < NewTemp){
digitalWrite(pinOut2, HIGH);
digitalWrite(pinOut1, LOW);
lcd.setCursor(0, 3);
lcd.print("HEATING");
lcd.setCursor(0, 2);
}
else if(inString = "OFF")
{
digitalWrite(pinOut1, LOW);
digitalWrite(pinOut2, LOW);
lcd.setCursor(0, 3);
lcd.print("OFF");
lcd.setCursor(0, 2);
}
//textMessage.indexOf("OFF")>=0
else {
digitalWrite (pinOut2, HIGH);
digitalWrite(pinOut1, LOW);
lcd.setCursor(0, 3);
lcd.print("SETTLED");
lcd.setCursor(0, 2);
}
}
fixtemp = 20;
delay(5000);
}
There might be some unnecessary bits in there.