Well I've updated my program a bit but I still can't get it to work. I have checked the voltages at ANS13 as the pot is changed and they're changing as expected but something in my program must not be working correctly. Something tells me maybe my "if" statements are wrong, I'm really a bit lost right now. I've checked my circuit wiring and I'm 100% sure everything is connected correctly.
/* This c program will take a voltage reading from a pot. If the voltage is between 1 and 4 volts the program will light a green led.
/if voltage is below 1 volt or above 4 it will light the red led and sound the alarm */
#include <pic.h>
#include <htc.h>
__CONFIG(LVPDIS &BORDIS & INTCLK & WDTDIS &PWRTEN);
unsigned char Lights[]={0xff};
static unsigned int ADReading = 0;\
float value = 0;
void Initialize();
void GetAnalogVoltage();
main()
{
Initialize();
for(;
// a for statement that will go on forever
GetAnalogVoltage();
}
void Initialize()
{
OSCCON = 0x7E; // Sets up internal oscillator for 8 MHz.
TRISB5 = 1; // Selects the INPUT pin.
ANS13 = 1; // pin is assigned as an analog input
ADCON0 = 11010000; // Analog channel pins for ans13 selected
ADCON1 = 10000000; // Sets voltage reference A/D conversion results right justified, Voltage reference bits VCFG1 VSS set to 0, VCFG0 VDD Set to 0,
}
void GetAnalogVoltage() // Data type, all of the below processes the conversion
{
char i;
ADON = 1; //Analog to Digital Clock is enabled
for(i = 0;i<6;i++); // loop for delay. Waiting time for acquisition
GODONE = 1; // Anolog to Digital conversion in progress, by setting this bit to 1 it starts the conversion cycle
while(GODONE); // Puts the results of the conversion cycle in a while loop. when low the results will go to ADreading
ADReading = (ADRESH *256) + ADRESL; // multiplies hi value (ADRESH) by 256 and adds the low value (ADRESL) giving us a 10bit value
value =(5.0*(ADReading/1023.0)); // Range calculation, Multiply the Value in ADReading by 5 then Divides the result by 1023
//PORTB=0xff;
if(value>=1&&value<=4) // If voltage value is above 1v and below 4v then Green LED (RB4) will light up
{
RB3 = 1;
RB4 = 0;
}
if(value<=1&&value>=4); // If voltage value is below 1v and above 4v then Red LED (RB3) will light up
{
RB4 = 1;
RB3 = 0;
}
}