Thanks…managed to get it right. here is final code :
#include <PCD8544.h>
#include <DHT.h>
#define DHTPIN 2 //temp humidity sensor pin
#define DHTTYPE DHT22 //what type of sensor it is
#define switchState 9 // my switch under toggle
#define testBedPower 8 // to apply power to test bed
#define loadSwitch 12 // pin for switching on relay for load
#define failLed 13
#define passLed 11
DHT dht(DHTPIN,DHTTYPE);
float Aref_Ratio = 4.408;
//for humidity and temp sensor
float temperature = 0;
float humidity = 0;
//voltage related
float dividerVoltage = 0;
float calculatedVoltage = 0;
float initialVoltage,loadVoltage;
float average = 0;
//general purpose
int buttonState = 0;
int x,count = 0;
boolean listenToButton =true;
unsigned char const halo_small_bmp[196] = {
0, 0, 252, 252, 252, 252, 0, 128, 128, 128, 128, 128,128, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128,128, 128, 128, 128, 0, 0, 0, 0, 0, 252, 252, 252,252,
0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 15, 7, 3, 3, 7, 7,255, 255, 254, 252,
0, 0, 128, 195, 239, 231, 227, 227,227, 227, 231, 255, 255, 254, 252, 0, 0, 255, 255, 255, 255, 0, 0, 248,254, 255, 255, 15, 7, 3, 3, 3, 7, 7, 159,
255, 254, 252, 224, 0, 0, 0, 63, 63, 63, 63, 0, 0, 0, 0, 0, 0, 63, 63, 63, 63, 0, 0, 15, 31, 63, 63, 60, 56, 56, 60, 30, 63, 63, 63,
63, 0, 0,63, 63, 63, 63, 0, 0, 3, 7, 15, 31, 62, 60, 60, 56, 56, 60, 62, 31, 31, 15, 3, 0, 0};
static PCD8544 lcd;
void setup() {
lcd.begin(84, 48); // lcd resolution
pinMode(switchState,INPUT); //start button
pinMode(testBedPower,OUTPUT);
pinMode(loadSwitch,OUTPUT);
pinMode(failLed,OUTPUT);
pinMode(passLed,OUTPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(passLed,LOW);
environment_display();
buttonState = digitalRead(switchState);
//jig toggle not down
while (buttonState == LOW && temperature < 35 && listenToButton) { // more than 35 load resistor is too hot
buttonState = digitalRead(switchState);
lcd.setCursor(10,1);
lcd.drawBitmap(halo_small_bmp,56,3);
lcd.setCursor(0,4); lcd.print("Insert PCB for");
lcd.setCursor(0,5); lcd.print(" TESTING ");
}
listenToButton = false;
while(temperature > 35){
lcd.clear();
lcd.setCursor(0,3);lcd.print("Jig too HOT!");
}
lcd.clear();
while (buttonState = HIGH){
digitalWrite(testBedPower,HIGH);
buttonState = digitalRead(switchState);
environment_display();
//starting the power supply
getVoltage();
initialVoltage = ((average/200 * 4.98)/1024); // actual voltage at divider network
lcd.setCursor(0,2);lcd.print("ADC :" );lcd.print(dividerVoltage); //actually the raw ADC
initialVoltage = ((average/200 * 4.98)/1024); // actual voltage at divider network
lcd.setCursor(0,3);lcd.print("Act :");lcd.print(initialVoltage);
initialVoltage = initialVoltage * Aref_Ratio;
lcd.setCursor(0,4);lcd.print("Volt :");lcd.print(initialVoltage);
delay(3000);
// check if power supply starts with correct voltage level
if (initialVoltage < 7.29) {
lcd.clear();
lcd.setCursor(0,0);lcd.print("POWER SUPPLY");
lcd.setCursor(0,1);lcd.print("FAILED INITIAL");
lcd.setCursor(0,2);lcd.print("STARTUP TEST");
lcd.setCursor(0,4);lcd.print("Volt : ");lcd.print(initialVoltage);
lcd.setCursor(0,5);lcd.print("ADC : ");lcd.print(dividerVoltage);
digitalWrite(failLed,HIGH);
delay(3000);
asm volatile (" jmp 0");
} else {
digitalWrite(passLed,HIGH);
delay(20);
digitalWrite(passLed,LOW);
}
//load test working correctly
lcd.clear();
digitalWrite(loadSwitch,HIGH); //apply load
environment_display();
lcd.setCursor(10,1);
lcd.drawBitmap(halo_small_bmp,56,3);
lcd.setCursor(0,4);lcd.print(" OUTPUT TEST PASSED" );
delay(2000);
lcd.setCursor(0,4);lcd.print(" LOAD TEST RUNNING " );
delay(3000);
lcd.clear();
environment_display();
getVoltage();
lcd.setCursor(0,2);lcd.print("ADC :" );lcd.print(dividerVoltage); //actually the raw ADC
calculatedVoltage = ((average/200 * 4.98)/1024); // actual voltage at divider network
if (calculatedVoltage >= 6.70) digitalWrite(passLed,HIGH);
loadVoltage = calculatedVoltage;
lcd.setCursor(0,3);lcd.print("Act :");lcd.print(loadVoltage);
loadVoltage = calculatedVoltage * Aref_Ratio;
lcd.setCursor(0,4);lcd.print("Calc :");lcd.print(loadVoltage);
digitalWrite(loadSwitch,LOW);
delay(3000);
while (loadVoltage < 6.65 && buttonState == HIGH){
buttonState = digitalRead(switchState);
lcd.clear();
environment_display();
lcd.setCursor(10,1);
lcd.drawBitmap(halo_small_bmp,56,3);
lcd.setCursor(0,4);lcd.print(" OUTPUT TEST FAILED" );
digitalWrite(failLed,HIGH);
delay(2000);
loadVoltage = 7;
}
//digitalWrite(passLed,HIGH);
}//while loop for button power supply under test still in jig
asm volatile (" jmp 0"); //reset if button is opened during test
}//main loop
void environment_display(){
humidity = dht.readHumidity();
temperature = dht.readTemperature();
lcd.setCursor(0, 0);
lcd.print("T:");lcd.print(temperature,0);lcd.print("C ");
lcd.print("H:");lcd.print(humidity,0);lcd.print("%");
}
void getVoltage(){
average = 0;
for (count=0;count<200;count++){
dividerVoltage = analogRead(A2);
average = dividerVoltage + average;
}
}