Hello friends I'm new here. I need your help. Thanks in advance.
Okay my problem is that I am developing a mobile robot that solves a maze by using ultrasonic, infrared sensor, etc. The problem is I know how to use the multiple channel but I failed to manipulate the data to get the motor function according to the logic.
Here is the idea:
There are 2 sensors, so meaning that it have 4 logic:
Code:
sensor 1 sensor2 motor1 motor2
0 0 0 0
0 1 0 1
1 0 1 0
1 1 1 1
I know that we can do it directly from sensor to motor but in my case I don't actually use 2 sensors, but 4 sensors and 2 output, so in order to make this function I have to use logic control.
Here is the coding that I developed. It seems not to be functioning properly. It doesn't go to the subroutine maybe?
Code:
#include <16F877A.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000) //20MHz
#define 1 PIN_C0
#define 2 PIN_C1
#byte PORTB=6
#byte PORTC=7
void fwd()
{
(output_high(pin_b1) && output_high(pin_b1));
delay_ms(20);
(output_low(pin_b0) && output_low(pin_b1));
}
void stp()
{
delay_ms(20);
output_low(pin_b0) && output_low(pin_b1);
}
void rgt()
{
output_high(pin_b1);
delay_ms(20);
output_low(pin_b0) && output_low(pin_b1);
}
void lft()
{
output_high(pin_b0);
delay_ms(20);
output_low(pin_b0) && output_low(pin_b1);
}
void machai()
{
if (output_low(pin_C0) && output_high(pin_C1))
{
fwd();
}
else if (output_low(pin_C0) && output_high(pin_C1))
{
rgt();
}
else if (output_high(pin_C0) && output_low(pin_C1))
{
lft();
}
else if (output_high(pin_C0) && output_high(pin_C1))
{
stp();
}
}
void main()
{
int adc_value0,adc_value1;
int i;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
set_tris_b(0b00000000);
set_tris_c(0b00000011);
set_tris_a(0b00000011);
portc=0;
portb=0;
while(1)
{
for(i=0;i<2;i++)
{
set_adc_channel(0); //set to read channel a0 next
delay_us(20);
adc_value0 = read_adc(); //get 10 bit readig for channel 0
if (adc_value0<128) output_low(pin_C0);
if (adc_value0>128) output_high(pin_C0);
delay_ms(500);
set_adc_channel(1); //set to read channel a1 next
delay_us(20);
adc_value1 = read_adc(); //get 10 bit readig for channel 1
if (adc_value1<128) output_low(pin_C1);
if (adc_value1>128) output_high(pin_C1);
delay_ms(500);
machai();
}
}
}
Thanks a million.