Drone Obstacle Avoidance Code
Drone Obstacle Avoidance Code
Drone Obstacle Avoidance Code
void loop(){
int a=160;
int b=160;
int c=160;
int d=160;
int e=160;
if(input_ROLL>1600){
d=210;
}
else if(input_ROLL<1400){
c=210;
}
if(input_PITCH>1600){
a=210;
}
else if(input_PITCH<1400){
b=210;
}
dist1 = sonar1.ping_cm();
dist2 = sonar6.ping_cm();
dist3 = sonar3.ping_cm();
dist4 = sonar4.ping_cm();
dist5 = sonar5.ping_cm();
dist6 = 200; // This Sensor is disabled due to my bad sensor.If you want to enable
just replace "200" with "sonar6.ping_cm()"
// For Pitch..............
if(dist1<a && dist1>0 && (dist2>b || dist2==0)){
out2.write(antifront);
b=210;
}
else if((dist1>=a || dist1==0) && (dist2>=b || dist2==0)){
out2.write(pitch);
}
else if(dist2<b && dist2>0 && (dist1>a || dist1==0)){
out2.write(antiback);
a=210;
}
else if(dist1<a && dist2<b && dist1>0 && dist2>0){
out2.write(92);
c=210;
d=210;
e=210;
if(dist3>c || dist3==0){
out1.write(55);
}
else if(dist4>d || dist4==0){
out1.write(125);
}
else if(dist5>e ||dist5==0){
out3.write(110);
}
else if(dist6>50 ||dist6==0){
out3.write(60);
}
}
// For Roll..............
if(dist3<c && dist3>0 && (dist4>d || dist4==0)){
out1.write(antileft);
d=210;
}
else if(dist3>=c || dist3==0 && dist4>=d || dist4==0){
out1.write(roll);
}
else if(dist4<d && dist4>0 && (dist3>c || dist3==0)){
out1.write(antiright);
c=210;
}
else if(dist3<c && dist4<d && dist3>0 && dist4>0){
out1.write(92);
a=210;
b=210;
e=210;
if(dist1>a || dist1 ==0){
out2.write(125);
}
else if(dist2>b || dist2 ==0){
out2.write(55);
}
else if(dist5>e || dist5 ==0){
out3.write(110);
}
else if(dist6>50 || dist6 ==0){
out3.write(60);
}
}
// For Throttle..............
if(dist5<e && dist5>0 && (dist6>50 || dist6==0)){
out3.write(antiUP);
}
else if(dist5>=e || dist5==0 && dist6>=50 || dist6==0){
out3.write(throttle);
}
else if(dist6<50 && dist6>0 && (dist5>e || dist5==0)){
out3.write(antidown);
}
else if(dist5<e && dist6<50 && dist5>0 && dist6>0){
out3.write(88);
a=210;
b=210;
c=210;
d=210;
if(dist1>a || dist1==0){
out2.write(125);
}
else if(dist2>b || dist2==0){
out2.write(55);
}
else if(dist3>c|| dist3==0){
out1.write(125);
}
else if(dist4>d || dist4==0){
out1.write(55);
}
}
if(f_clear && b_clear && l_clear && r_clear && up_clear && down_clear){
if(val<1500){
leds[0] = CRGB(0, 255, 0);
FastLED.show();
out1.write(roll);
out2.write(pitch);
out3.write(throttle);
}
else if(val>=1500){
leds[0] = CRGB(235, 255, 200);
FastLED.show();
}
}
else if(dist1<a && dist1>0 || dist2<b && dist2>0 || dist3<c && dist3>0 || dist4<d
&& dist4>0 || dist5<e &&& dist5>0 || dist6<50 && dist6>0){
if(val<1500){
leds[0] = CRGB(255, 0, 0);
FastLED.show();
}
else if(val>=1500){
leds[0] = CRGB(235, 255, 200);
FastLED.show();
}
}
if(last_CH2_state == 0){
last_CH2_state = 1;
counter_2 = current_count;
}
}
else if(last_CH2_state == 1){
last_CH2_state = 0;
input_PITCH = current_count - counter_2;
}
if(PINB & B00000100 ){
if(last_CH3_state == 0){
last_CH3_state = 1;
counter_3 = current_count;
}
}
else if(last_CH3_state == 1){
last_CH3_state = 0;
input_THROTTLE = current_count - counter_3;
}
}