// // // // 2020.7.20 16:45 // int matrix[4][4] = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; const int analogSG1 = A0; const int analogSG2 = A1; const int analogSG3 = A2; const int analogTHER = A3; int relay2Pin = 3; //set pin 3 for relay2 (heater2 = pan) output int relay1Pin = 2; // set pin 2 for relay1 (heater1 = tower) output int motorPin = 13; int solenoidPin = 12; int valSG1=0; //value of switch group 1 int oldvalSG1=0; //old value of switch group 1 int LEDstateSG1=0; //LED state of switch group 1 int valSG2=0; //value of switch group 2 int oldvalSG2=0; //old value of switch group 2 int LEDstateSG2=0; //LED state of switch group 2 int valSG3=0; //value of switch group 3 int oldvalSG3=0; //old value of switch group 3 const float R4 = 100000; //R4 is R4 in the circuit, the resistor next to thermistor int mainonoff1 = 0; // switch controlled onoff of heater1 int onoffstate1 = 0; //actual onoff state of heater1 int mainonoff2 = 0; // switch controlled onoff of motor int mainonoff3 = 0; // switch controlled onoff of solenoid //int onoffstate2 = 0; //actual onoff state of heater1 int activebutton = 0; //indicates which button is pressed int counter_motor = 0; int counter_sol_ON = 0; int counter_sol_OFF = 0; int counter_sol_lmt = 0; int counter_serial = 0; int counter_sumTActual = 0; int counter_LED = 0; int keySG1, keySG2, keySG3, old_keySG1, old_keySG2, old_keySG3; int col; int sol_state; // solenoid ON/OFF state float ave_TActual, sum_TActual; void setup() { Serial.begin( 9600 ); pinMode(relay1Pin, OUTPUT); pinMode(relay2Pin, OUTPUT); pinMode(motorPin, OUTPUT); pinMode(solenoidPin, OUTPUT); //Set up pins for LED matrix for (int i=5;i<=11;i++) { pinMode(i, OUTPUT); digitalWrite(i, LOW); } digitalWrite(motorPin, LOW); digitalWrite(solenoidPin, LOW); } void loop() { // thermistor to read TActual: the current temperature of tower float valTher,V,R,TActual; float TSet; //Tset is the set temperature for heater1 valTher = analogRead(analogTHER); V = 5*valTher/1024; R = (5-V)*R4/V; float y = 4076.6/(log(R)+2.11); TActual=y-273; sum_TActual = sum_TActual + TActual; if (counter_sumTActual > 500) { ave_TActual = sum_TActual/500; counter_sumTActual = 0; sum_TActual = 0; Serial.println (ave_TActual); } counter_sumTActual = counter_sumTActual + 1; //********************************************* // Switch scan process //********************************************* // *** Read switch group1 *** valSG1 = analogRead(analogSG1); if (valSG1 <250) { keySG1 = 1; } else if ( valSG1 < 600 ) { keySG1 = 2; } else if ( valSG1 < 800 ) { keySG1 = 3; } else { keySG1 = 0; } // *** Ckeck Switch push ** if(keySG1 != old_keySG1 ) { if ( keySG1 == 1 ) { LEDstateSG1 = (LEDstateSG1+1) % 3; } else if ( keySG1 == 3 ){ //if sw3 is pressed if (LEDstateSG1 == 0) { LEDstateSG1 = 0; } else{ LEDstateSG1 = LEDstateSG1 - 1; } } else if ( keySG1 == 2 ) { //if sw2 is pressed if(mainonoff1 == 0){ mainonoff1 = 1; } else { mainonoff1 = 0; } //SSR1 activated after SW2 is pressed, deactivated once pressed again activebutton = 2; } } old_keySG1 = keySG1; // *** Read switch group2 *** valSG2 = analogRead(analogSG2); if (valSG2 <250) { keySG2 = 1; } else if ( valSG2 < 600 ) { keySG2 = 2; } else if ( valSG2 < 800 ) { keySG2 = 3; } else { keySG2 = 0; } if(keySG2 != old_keySG2 ) { if ( keySG2 == 1 ) { LEDstateSG2 = (LEDstateSG2+1) % 3; } else if ( keySG2 == 3 ){ if (LEDstateSG2 == 0) { LEDstateSG2 = 0; } else{ LEDstateSG2 = LEDstateSG2 - 1; } } else if ( keySG2 == 2 ) { //if sw2 is pressed if(mainonoff2 == 0){ // *** Set Motor flag OFF -> ON mainonoff2 = 1; // *** Set Solenoid ON duration ** if ( LEDstateSG2 == 0 ){ counter_sol_lmt = 500; // 0.5sec set sol ON duration } else if ( LEDstateSG2 == 1 ){ counter_sol_lmt = 700; // 0.7sec } else { counter_sol_lmt = 900; // 0.9sec } // *** Initialize Solenoid ON/OFF counter counter_sol_ON = 0; // initialize sol_counter counter_sol_OFF = 0; sol_state = 0; // Start from Solenoid OFF state } else { // *** Set Moter flag ON -> OFF mainonoff2 = 0; // Motor OFF } } } old_keySG2 = keySG2; // *** read switch group3 *** valSG3 = analogRead(analogSG3); if(valSG3 < 800) { keySG3 = 1; } else { keySG3 = 0; } if(keySG3 != old_keySG3 ) { if ( keySG3 == 1 ){ if( mainonoff3 == 0 ){ mainonoff3 = 1; digitalWrite ( relay2Pin, HIGH ); }else{ mainonoff3 = 0; digitalWrite ( relay2Pin, LOW ); } } } old_keySG3 = keySG3; //********************************************* // Set LED pattern of column0 (Thermal setting) //********************************************* if (LEDstateSG1 == 0) { matrix[2][0] =1; matrix[1][0] =0; matrix[0][0] =0; TSet=110; //set temperature to 110 degrees } else if (LEDstateSG1 == 1) { matrix[2][0] =1; matrix[1][0] =1; matrix[0][0] =0; TSet=120; //set temperature to 120 degrees } else if (LEDstateSG1 == 2) { matrix[2][0] =1; matrix[1][0] =1; matrix[0][0] =1; TSet=130; //set temperature to 130 degrees } //*********************************************** // Set LED Pattern of column1( Actual Thermal control) //*********************************************** if ((TActual >= 110) && (TActual < 120)) { matrix[2][1] =1; matrix[1][1] =0; matrix[0][1] =0; } else if ((TActual >= 120) && (TActual < 130)) { matrix[2][1] =1; matrix[1][1] =1; matrix[0][1] =0; } else if (TActual >= 130){ matrix[2][1] =1; matrix[1][1] =1; matrix[0][1] =1; } //****************************************************************** // Set LED pattern of column2 ( Solenoid ON duration) //****************************************************************** if (LEDstateSG2 == 0) { matrix[2][2] =1; matrix[1][2] =0; matrix[0][2] =0; // counter_sol_lmt = 500; } else if (LEDstateSG2 == 1) { matrix[2][2] =1; matrix[1][2] =1; matrix[0][2] =0; // counter_sol_lmt = 700; } else if (LEDstateSG2 == 2) { matrix[2][2] =1; matrix[1][2] =1; matrix[0][2] =1; // counter_sol_lmt = 900; } //*************************************** // Control SSR1 for Heater1 process //*************************************** if (mainonoff1==1){ if (ave_TActual < TSet-3) { digitalWrite(relay1Pin, HIGH);// set relay pin to HIGH onoffstate1 = 1; } else if (ave_TActual < TSet) { if (onoffstate1 == 1){ digitalWrite(relay1Pin, HIGH);// set relay pin to HIGH onoffstate1 = 1; } else { digitalWrite(relay1Pin, LOW); onoffstate1 = 0; } } else { digitalWrite(relay1Pin, LOW); onoffstate1 = 0; } } //************************************* // Control motor process //************************************* // *** During Motor ON *** if (mainonoff2 == 1){ digitalWrite(motorPin, HIGH); // *** Control Solenoid *** if( sol_state == 1 ){ counter_sol_ON ++ ; //****************************************************************** // Set LED pattern of column2 ( Solenoid ON duration) //****************************************************************** if (LEDstateSG2 == 0) { matrix[2][3] =1; matrix[1][3] =0; matrix[0][3] =0; } else if (LEDstateSG2 == 1) { matrix[2][3] =1; matrix[1][3] =1; matrix[0][3] =0; } else if (LEDstateSG2 == 2) { matrix[2][3] =1; matrix[1][3] =1; matrix[0][3] =1; } if( counter_sol_ON > counter_sol_lmt ){ counter_sol_ON = 0; counter_sol_OFF = 0; sol_state = 0; digitalWrite(solenoidPin, LOW); } }else{ counter_sol_OFF ++ ; matrix[2][3] =0; matrix[1][3] =0; matrix[0][3] =0; if( counter_sol_OFF > 10000 ){ counter_sol_ON = 0; counter_sol_OFF = 0; sol_state = 1; digitalWrite(solenoidPin, HIGH); } } /* counter_sol_ = sol_counter - 1; if (counter_motor > 10000) { if(counter_sol < counter_sol_lmt){ digitalWrite(solenoidPin, HIGH); counter_sol = counter_sol + 1; } else if (counter_sol <= (counter_sol_lmt + 10000 )) { digitalWrite(solenoidPin, LOW); counter_sol = counter_sol + 1; } else if (counter_sol > (counter_sol_lmt + 10000)) { counter_sol = 0; } } counter_motor = counter_motor + 1; */ // *** During Motor OFF *** } else { digitalWrite(motorPin, LOW); sol_state = 0; digitalWrite(solenoidPin, LOW); counter_sol_ON = 0; counter_sol_OFF = 0; //****************************************************************** // Set LED pattern of column3 ( Solenoid OFF) //****************************************************************** matrix[2][3] =0; matrix[1][3] =0; matrix[0][3] =0; // counter_motor = 0; } //***************************************** // Display matrix patter to LED //***************************************** for ( int i = 0 ; i <= 2 ; i++ ){ digitalWrite ( i+5, matrix[i][col]); } for ( int j = 0 ; j <= 3 ; j++ ) { digitalWrite ( j+8, HIGH ); } digitalWrite ( col+8, LOW ); col = ( col+1 )%4; // delay(1); }