Hello. I tried to build a circuit that controls relay according to voltage. But I have small problems and hope you can help me about them. The main purpose of circuit, at first opening it measures the main voltage and if it is higher than 10.5V it gives that voltage to relay until voltage drops under 10.5V. İf drops below 10.5 cuts. And after voltage gets higher than 12V, it gives power to relay, again. İnfinity loop. But my circuit doesn't do that initial measuring, always close relay at initial. And also, sometimes it doesn't work according to my voltage values. I mean cuts at 11V and opens at 12.30V. If you can see what I am doing wrong, it would really help me
I added photo and also codes of circuit.
I added photo and also codes of circuit.
Code:
int okunan=0,okunan2=0,ort,ort2,top,top2;
float milivolt,milivolt2;
char i=0,k=0,txt[15];
void kurulum() {
ADC_Init();
ADCON0=01000000;
//ANSEL =00000001;
TRISIO.GP5=0;
GPIO.GP5 =0;
TRISIO.GP2=0;
GPIO.GP2 =0;
//OSCCAL=0x80;
}
void main(){
kurulum();
while(1){
for(i=0;i<20;i++)
{
okunan=ADC_Read(0);
top+=okunan;
delay_ms(10);
}
ort=top/20;
top=0;
milivolt=ort*4.8828125;
if(milivolt < 2626){
GPIO.GP5 = 0;
GPIO.GP2 = 0;
delay_ms(100);
}
if(milivolt > 2626){
GPIO.GP5 = 1;
GPIO.GP2 = 1;
delay_ms(100);
}
while(1){
for(i=0;i<20;i++)
{
okunan=ADC_Read(0);
top+=okunan;
delay_ms(10);
}
ort=top/20;
top=0;
milivolt=ort*4.8828125;
if(milivolt < 2626){
GPIO.GP5 = 0;
GPIO.GP2 = 0;
delay_ms(100);
}
if(milivolt > 2975 ){
GPIO.GP5 = 1;
GPIO.GP2 = 1;
delay_ms(100);
}
}
}
}