This is the code I'm coded in my Arduino sketch, I won't be able to test it till I get home. Not getting any errors when I check it, but I'm sure there will be some tweaking I need to do to it.
/*
* Final Lab
*/
const int lightSensor= 5; //pin for light source
const int tempSensor = 4; //pin for temp sensor
int button = 10; //pin for button
int buttonState= 0; //state of the button
const int Relay1 = 11; // pin for relay 1
const int Relay2 = 12; // pin for relay 2
const int LED1 = 8; //pin 1 for LED1
const int LED2 = 2; //pin 2 for LED2
const int LED3 = 3; //pin 2 for LED3
const int LED4 = 4; //pin 4 for LED4
const int LED5 = 5; //pin 5 for LED5
const int LED6 = 6; //pin 6 for LED6
const int LED7 = 7; //pin 7 for LED7
int buttonval=0; //value of button
int timer=0; //timer value
unsigned long previoustime=0; //setup time for flashing of LED4
int val =0; //set value for button
int old_val = 0; //set old value for button
const int state = LOW; //set button off
int sensValue = 0; //variable to hold sensor readings
int buttonVal = 0; //variable to hold button read
int countdownState=0; //count down state
float tval = 0; //Temp value
float tempcel = 0; // Tempature for celsuis
float diff = 24.03; // differencial
float tempsensValue = 0; // temp sensor reading
void setup() {
// put your setup code here, to run once:
pinMode(LED1, OUTPUT); // Intiates pin as an output
pinMode(LED2, OUTPUT); // Intiates pin as an output
pinMode(LED3, OUTPUT); // Intiates pin as an output
pinMode(LED4, OUTPUT); // Intiates pin as an output
pinMode(LED5, OUTPUT); // Intiates pin as an output
pinMode(Relay1, OUTPUT); //relay 1 pin output
pinMode(Relay2, OUTPUT); // relay 2 pin output
Serial.begin(9600); //begin serial communication
lightsOn();
delay(300);
lightsOff();
delay(300);
lightsOn();
delay(300);
lightsOff();
delay(300);
lightsOn();
delay(300);
lightsOff();
}
void ledThirty(){
digitalWrite(LED4, HIGH); // Turn on LED4
if (millis()- previoustime >=30000){ //set time to flash at 30 seconds
digitalWrite(LED4, LOW); //turn off LED1
delay(300); //delay for LED4
digitalWrite(LED4, HIGH); // TURN ON LED
previoustime = millis(); // used to reset previoustime to match millis
}
}
void SerialOutput(){
Serial.print("Timer="); //provide text to explain output
Serial.print(timer); //output variable x value
Serial.print("\n"); // drop down a line
Serial.print("buttonState="); //provide text to explain output
Serial.print(buttonState); //output variable x value
Serial.print("\n"); // drop down a line
Serial.print("countdownState="); //provide text to explain output
Serial.print(countdownState); //output variable x value
Serial.print("\n"); // drop down a lineSerial.print("Value");
Serial.print(sensValue); //out sensor value to scren
Serial.print("\n");
Serial.print("Temp Value");
Serial.print(tempsensValue); //out sensor value to scren
Serial.print("\n");
Serial.print("Tval");
Serial.print(tval); //out sensor value to scren
Serial.print("\n");
Serial.print("temcel=l");
Serial.print(tempcel); //out sensor value to scren
Serial.print("\n");
}
void lightsOn() {
digitalWrite(LED1,HIGH); // Turn on LED1
digitalWrite(LED2,HIGH); // Turn on LED2
digitalWrite(LED3,HIGH); // Turn on LED3
digitalWrite(LED4,HIGH); // Turn on LED4
digitalWrite(LED5,HIGH); // Turn on LED5
digitalWrite(LED6,HIGH); // Turn on LED6
digitalWrite(LED7,HIGH); // Turn on LED7
}
void lightsOff(){
digitalWrite(LED1,LOW); // Turn on LED1
digitalWrite(LED2,LOW); // Turn on LED2
digitalWrite(LED3, LOW); // Turn on LED3
digitalWrite(LED4,LOW); // Turn on LED4
digitalWrite(LED5,LOW); // Turn on LED5
digitalWrite(LED6,LOW); // Turn on LED6
digitalWrite(LED7,LOW); // Turn on LED7
}
void lightRead(){ //for light sensor
if (sensValue <= 300) { //if sensor value is below 500 switch on LED2
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
}
else { //if sensor value is above 300 turn on LED3
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
}
}
void heater()
{
if (tempcel >= 30) { //if tempcel is below 30 celsius turn on heater
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,HIGH);
}
else if(tempcel <=40) {
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2,LOW);
}
}
void buttonCheck(){
buttonval = digitalRead(button); //check for button press
if ((buttonval == 1) && (buttonState == 0))
{
//if the button is pushed for the first time startup timer etc.
digitalWrite(LED6, HIGH);
digitalWrite(Relay1, LOW); //Turn on Heater
delay(10000);
digitalWrite(Relay1,HIGH);
delay(1000);
digitalWrite(Relay2,LOW);
delay(3000);
digitalWrite(Relay2,HIGH);
Serial.print("button pushed and state should change");
Serial.print("\n");
countdownState=1; //set variable to 1 to run countdown used in countdownCheck function
buttonState=1; //increment buttonState by 1
}
else if ((buttonval == 1) && (buttonState == 1)){
// if the button is pushed for the second time - reset everything and shutdown the counting function through countdownState=0
digitalWrite(LED6, HIGH); //turn on
digitalWrite(LED7,HIGH); //turn on
countdownState = 0; //set variable to 1 to run countdown
buttonState--; //decrement buttonState by 1
timer=0; //reset the timer
digitalWrite(LED3, LOW); //turn off LED
digitalWrite(Relay1, HIGH); //turn on Relay1
}
else if (tempcel >= 30) { //if tempcel is below 30 celsius turn on heater
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,HIGH);
digitalWrite(LED5, HIGH);
}
else if (tempcel <=40) {
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2,LOW);
digitalWrite(LED5, LOW);
}
}
void countdownCheck(){
if ((countdownState == 1) && (timer != 30)) {
//if the countdown is set to run (countdownState==1) and max time not met, turn on relay, led and increment the timer counter
timer++; // increment timer
digitalWrite(Relay1, LOW); //turn on relay
digitalWrite(LED3, HIGH); //turn on LED
}
if ((countdownState ==1) && (timer == 30)){
//if we hit the maximum timer counter value of 5, reset countdownState to turn off counting and reset buttonState and timer
countdownState = 0;//reset countdownState to off
timer = 0; //reset the timer to 0
buttonState = 0; //reset the buttonState for next cycle
digitalWrite(Relay1, HIGH); //turn off relay
digitalWrite(LED3, LOW); //turn off LED
}
}
void tempRead(){
tempsensValue = analogRead(tempSensor); //read sensor data
tval = ((tempsensValue*3.3)/1024);
tempcel= (tval * 100) - diff;sensValue = analogRead(lightSensor); //read data from sensor
}
void loop() {
// put your main code here, to run repeatedly:
tempRead();
buttonCheck();
countdownCheck();ledThirty();
lightRead();
heater();
SerialOutput();
delay(1000);
}