#include <EEPROM.h>
#define potentiometer A5
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
int Aux = 5;
int Tv = 6;
int Dvd = 7;
int Usb = 8;
int pin_switch = 2;
int relay = 12;
byte relayState = 0;
int read_pot = 0;
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long duration;
int tempPin = A1; // the output pin of LM35
int fan = 3; // the pin where fan is
int led = 4 ; // led pin
int temp;
int tempMin = 40; // the temperature to start the fan
int tempMax = 70; // the maximum temperature when fan is at 100%
int fanSpeed;
//int ledState;
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
byte state = 0;
void setup()
{
delay(2000);
Serial.begin(9600);
pinMode(Aux, OUTPUT);
digitalWrite(Aux, LOW);
pinMode(Tv, OUTPUT);
digitalWrite(Tv, LOW);
pinMode(Dvd, OUTPUT);
digitalWrite(Dvd, LOW);
pinMode(Usb, OUTPUT);
digitalWrite(Usb, LOW);
pinMode(pin_switch, INPUT);
attachInterrupt(digitalPinToInterrupt(pin_switch), Switch, HIGH);
checkLedState();
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
pinMode(tempPin, INPUT);
pinMode(potentiometer, INPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
//LedState();
}
void Switch()
{
newSwitchState = digitalRead(pin_switch);
if ( newSwitchState != oldSwitchState )
{
// has the button switch been closed?
if ( newSwitchState == HIGH )
{
// increase the value of state
state++;
// delay(1000);
if (state > 4) {
state = 1;
}
//digitalWrite(Aux, LOW);
// digitalWrite(Tv, LOW);
// digitalWrite(Dvd, LOW);
// digitalWrite(Usb, LOW);
delay(1000);
LedState();
delay(1000);
}
oldSwitchState = newSwitchState;
EEPROM.update(0, state);
delay(200);
}
}
void loop() {
relayTime();
fanControl();
LedState();
}
void checkLedState() {
state = EEPROM.read(0);
LedState();
}
void LedState() {
if (state == 1) {
digitalWrite(Aux, HIGH);
digitalWrite(Tv, LOW);
digitalWrite(Dvd, LOW);
digitalWrite(Usb, LOW);
}
if (state == 2) {
digitalWrite(Aux, LOW);
digitalWrite(Tv, HIGH);
digitalWrite(Dvd, LOW);
digitalWrite(Usb, LOW);
}
if (state == 3) {
digitalWrite(Aux, LOW);
digitalWrite(Tv, LOW);
digitalWrite(Dvd, HIGH);
digitalWrite(Usb, LOW);
}
if (state == 4) {
digitalWrite(Aux, LOW);
digitalWrite(Tv, LOW);
digitalWrite(Dvd, LOW);
digitalWrite(Usb, HIGH);
}
}
void fanControl()
{
temp = readTemp();
Serial.print("Temperature: ");
Serial.println(temp);// get the temperature
if (temp < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
if ((temp >= tempMin) && (temp <=tempMax)) { // if temperature is higher than minimum temp
fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
if (temp > tempMax) { // if temp is higher than tempMax
digitalWrite(led, HIGH); // turn on led
} else { // else turn of led
digitalWrite(led, LOW);
}
delay(200);
}
/*int readTemp() { // get the temperature and convert it to celsius
temp = analogRead(tempPin);
return temp * 0.48828125;
}*/
int readTemp() {
Vo = analogRead(tempPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
Tc = T - 273.15;
temp = Tc;
// delay(200);
return temp*2.5 ;
// return temp * 1;
}
void relayTime()
{
read_pot = analogRead(potentiometer);
duration = map(read_pot, 0, 1023, 300UL, 30000UL);
currentMillis = millis();
if ((currentMillis - previousMillis) > duration)
{
previousMillis = previousMillis + duration;
relayState = 1 ;
digitalWrite(relay, relayState);
}
}