#include <LiquidCrystal.h>
// number of analog samples to take per reading, per channel
#define NUM_SAMPLES 30
// voltage divider calibration values
#define DIV_1 58.13465
#define DIV_2 58.07185
#define DIV_3 58.07185
#define DIV_4 58.07185
// ADC reference voltage / calibration value
#define V_REF 4.991
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sum[4] = {0}; // sums of samples taken
unsigned char sample_count = 0; // current sample number
float voltage[4] = {0.0}; // calculated voltages
char l_cnt = 0; // used in 'for' loops
int pinButton = 6; //the button -- 12 PIN
int LED = 7; //the pin we connect the LED--13 PIN
int RELAY = 8; //the pin we connect the RELAY --14 PIN
int ON = 9; //the pin we connect the --15 PIN
int OFF = 10; //the pin we connect the --16 PIN
int phase1 = A0; //the pin we connect the -- 23 PIN
int phase2 = A1; //the pin we connect the -- 24 PIN
void setup()
{
lcd.begin(16, 4); // main lcd starting point
lcd.setCursor(0,1);
lcd.print("KSF ELECTRONICS");
delay(1000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" obaidkakar.");
lcd.setCursor(0,2);
lcd.print(" blogspot.com");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" WARNING ");
lcd.setCursor(0,1);
lcd.print("After button on ");
lcd.setCursor(0,2);
lcd.print("Motor will start");
lcd.setCursor(0,3);
lcd.print("in 13 Seconds");
delay(1000);
lcd.clear(); // clear screen
pinMode(pinButton, INPUT); //set the button pin as INPUT
pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
pinMode(RELAY, OUTPUT); //set the LED pin as OUTPUT
pinMode(ON, OUTPUT);
pinMode(OFF, OUTPUT);
pinMode(phase1, INPUT);
pinMode(phase2, INPUT);
Serial.begin(9600);
}
void loop()
{
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES) {
// sample each channel A2 to A5
for (l_cnt = 0; l_cnt < 4; l_cnt++) {
sum[l_cnt] += analogRead(A2 + l_cnt); // input were given to lcd
}
sample_count++;
delay(40);
}
// calculate the voltage for each channel
for (l_cnt = 0; l_cnt < 4; l_cnt++) {
voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;
}
// display voltages on LCD
// each voltage is multiplied by the resistor network
// division factor to calculate the actual voltage
// voltage 1 - A (pin A2)
lcd.setCursor(0, 0);
lcd.print("SETTING ");
lcd.print(voltage[2] * DIV_3, 1); // pin A4--Setting --27 PIN
lcd.print(" AC ");
lcd.setCursor(0, 1);
lcd.print("PHASE1 ");
lcd.print(voltage[0] * DIV_1, 1); // pin A2-- PHASE 1--25 PIN
lcd.print(" AC ");
lcd.setCursor(0, 2);
lcd.print("PHASE2 ");
lcd.print(voltage[1] * DIV_2, 1); // pin A3 --PHASE 2--26 PIN
lcd.print(" AC ");
lcd.setCursor(0, 3);
lcd.print("OUTPUT ");
lcd.print(voltage[3] * DIV_4, 1); // pin A5--Output--28 PIN
lcd.print(" AC ");
if(voltage[1]== 0){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("PHASE2 MISSING ");
}
if(voltage[0]== 0){
lcd.clear();
lcd.setCursor(0,2);
lcd.print("PHASE1 MISSING ");
delay(10);
}
// voltage 4 - D (pin A5)
// reset count and sums
sample_count = 0;
for (l_cnt = 0; l_cnt < 4; l_cnt++)
{
sum[l_cnt] = 0;
}
int stateButton = digitalRead(pinButton); //read the state of the button
int stateButton1 = digitalRead(voltage[0]); //read the state of the button
int stateButton2 = digitalRead(phase2); //read the state of the button
if(stateButton == 1 && stateButton1 ==1 && stateButton2 == 1 ) { //if is pressed+ all phases present then.
digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
digitalWrite(ON, HIGH);
digitalWrite(OFF, LOW);
} else { //if not pressed
digitalWrite(LED, LOW); //write 0 or low to led pin
digitalWrite(ON, LOW);
digitalWrite(OFF, HIGH);
delay(10000);
} if(voltage[0] < voltage[2]) digitalWrite(RELAY, HIGH);
else { digitalWrite(RELAY,LOW);
delay(20);
}
}