#include <18f452.h>
#device ADC = 10;
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 10000000)
#use rs232(UART1, baud=9600)
#include "flex_lcd.c"
#include <math.h>
int x = 0;
int previousS2 = 0;
int count = 0;
int s1, s2, s3, rate, previousS1;
float distance_value;
unsigned int16 i, ldr_value;
//=============================================================================================
void start()
{
do {
s2 = input(PIN_C1);
while ( s2 == 1 && count == 0 )
previousS2 = 1;
lcd_putc("\f");
lcd_putc("Press S2 to");
lcd_gotoxy(1,0);
lcd_putc("start");
delay_ms(100);
}while(previousS2 == 0);
}
//=============================================================================================
void pause()
{
do {
previousS2 = 0;
s2 = input(PIN_C1);
if (s2 == 1)
previousS2 = 1;
lcd_putc("\f");
lcd_putc("Pause");
lcd_gotoxy(1,0);
lcd_putc("Press S2 to continue");
delay_ms(200);
lcd_gotoxy(1,0);
lcd_putc("ress S2 to continue");
delay_ms(200);
lcd_gotoxy(1,0);
lcd_putc("ss S2 to continue");
delay_ms(200);
lcd_gotoxy(1,0);
lcd_putc("s S2 to continue");
delay_ms(200);
lcd_putc(" S2 to continue");
delay_ms(200);
}while(previousS2 == 0);
}
//=============================================================================================
void init() // Initialization
{
lcd_init(); // Always call this first.
setup_adc_ports(ALL_ANALOG); // Setup ADC
setup_adc( ADC_CLOCK_DIV_16 );
lcd_putc("\f"); // clear LCD
printf("\t\t==================================================\r\n"); //output for UART
printf("\t\t||\tDISTANCE (cm)\t||\tLDR (ohm)\t||\r\n");
printf("\t\t==================================================\r\n");
delay_ms(100);
}
//=============================================================================================
void ldr() // KY-018
{
set_adc_channel(0);
for(i = 0; i < 3; i++) // Read the sensor for 3 times
{
ldr_value = read_adc(); + ldr_value;
delay_ms(10);
}
ldr_value = (ldr_value/3); // Average value
}
//=============================================================================================
void distance() // SHARP IR SENSOR
{
set_adc_channel(1);
for(i = 0; i < 100; i++) // Read the sensor for 100 times
{
distance_value = read_adc() + distance_value;
}
distance_value = (distance_value/100) ; // Average value
distance_value = 10650.08 * pow(distance_value,-0.935) - 8; //convert voltage to cm
}
//=============================================================================================
void ldr_led() // LIGHT LEDs WITH SPECIFIC LDR CONDITION
{
if (ldr_value >= 135 && ldr_value <= 341)
output_b(0b00011111);
else if (ldr_value >= 109 && ldr_value <= 134)
output_b(0b00001111);
else if (ldr_value >= 94 && ldr_value <= 108)
output_b(0b00000111);
else if (ldr_value >= 82 && ldr_value <= 93)
output_b(0b00000011);
else if (ldr_value >= 73 && ldr_value <= 82)
output_b(0b00000001);
else if (ldr_value < 73)
{
output_b(0b00100000);
delay_ms(100);
output_b(0b00000000);
delay_ms(100);
}
delay_ms(100);
}
//=============================================================================================
void distance_led() // LIGHT LEDs WITH SPECIFIC DISTANCE CONDITION
{
if (distance_value > 44 && distance_value <= 50)
{
rate = 200;
output_low(PIN_C4);
output_high(PIN_C5);
delay_ms(rate);
output_low(PIN_C5);
delay_ms(rate);
}
else if (distance_value > 39 && distance_value <= 44)
{
rate = 400;
output_low(PIN_C4);
output_high(PIN_C5);
delay_ms(rate);
output_low(PIN_C5);
delay_ms(rate);
}
else if (distance_value > 34 && distance_value <= 39)
{
rate = 600;
output_low(PIN_C4);
output_high(PIN_C5);
delay_ms(rate);
output_low(PIN_C5);
delay_ms(rate);
}
else if (distance_value > 29 && distance_value <= 34)
{
rate = 800;
output_low(PIN_C4);
output_high(PIN_C5);
delay_ms(rate);
output_low(PIN_C5);
delay_ms(rate);
}
else if (distance_value >= 0 && distance_value <= 29)
{
output_high(PIN_C4);
output_high(PIN_C5);
}
}
//=============================================================================================
void display_ldr() // LDR LCD OUTPUT
{
lcd_putc("\f");
lcd_putc("LDR=");
printf(lcd_putc, "%Lu", ldr_value);
printf(lcd_putc, "%c", 0b11110100); // display O
if (ldr_value >= 341)
lcd_putc("\nDARK");
else if (ldr_value >= 135 && ldr_value <= 341)
lcd_putc("\nSTEP 1");
else if (ldr_value >= 109 && ldr_value <= 134)
lcd_putc("\nSTEP 2");
else if (ldr_value >= 94 && ldr_value <= 108)
lcd_putc("\nSTEP 3");
else if (ldr_value >= 82 && ldr_value <= 93)
lcd_putc("\nSTEP 4");
else if (ldr_value >= 73 && ldr_value <= 82)
lcd_putc("\nSTEP 5");
else if (ldr_value < 73)
lcd_putc("\nBRIGHT");
delay_ms(100);
}
//=============================================================================================
void display_distance() // DISTANCE LCD OUTPUT
{
lcd_putc("\f");
lcd_putc("DISTANCE=\n");
printf(lcd_putc, "%0.2f", distance_value); ;
lcd_putc("cm");
}
//=============================================================================================
void UART() //display value to UART terminal
{
printf("\t\t||\t%0.2f\t\t||\t%Lu\t\t||\r\n",distance_value,ldr_value);
}
//=============================================================================================
void main() // Main Program
{
init();
start(); //start program
while(1)
{
s1 = input(PIN_C0);
s3 = input(PIN_C2);
if (s3 == 1)
pause(); //pause
ldr();
distance();
if (s1 == 1 && x == 0) // check s1 status
{
previousS1 = 1;
x = 1;
}
else if (s1 == 1 && x == 1)
{
previousS1 = 0;
x = 0;
}
if (previousS1 == 1) //switch display mode
display_ldr();
else
display_distance();
ldr_led();
distance_led();
UART();
delay_ms(100);
}
}