Go Down

Topic: Why my robot does not advance when it detects the opponent (Read 501 times) previous topic - next topic

MOLIM

As it says in the title of the topic I can not advance the robot? It only happens to turn on itself. Where is the error. thank you.

Code: [Select]

#include <Ultrasonic.h>
Ultrasonic ultrason(2);

int ENA=3;
int IN1=4;
int IN2=5;
int IN3=6;
int IN4=7;
int ENB=5;

int led1=13;

void setup()
{
 pinMode(ENA,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(ENB,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
 pinMode(led1,OUTPUT);
 digitalWrite(led1,LOW);
}
void loop()
{
 
  ultrason.MeasureInCentimeters();
  int dist_cm=ultrason.RangeInCentimeters;
  if(dist_cm > 90)                                   
   {
     //The robot Turns on itself
     digitalWrite(led1,HIGH);
     delay(400);
     digitalWrite(led1,LOW);
     delay(400);
     analogWrite(ENA,38);
     digitalWrite(IN1,HIGH);
     digitalWrite(IN2,LOW);
     analogWrite(ENB,38);
     digitalWrite(IN3,LOW);
     digitalWrite(IN4,HIGH);       
   }
 else
   {
    //It Detects the opponent it advances
    digitalWrite(led1,HIGH);
    delay(50);
    digitalWrite(led1,LOW);
    delay(50);   
    digitalWrite(ENA,HIGH);
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,HIGH);
    digitalWrite(ENB,HIGH);
    digitalWrite(IN3,HIGH);
    digitalWrite(IN4,LOW);
     
   }
}
 

pert

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.


Add a Serial.println() of dist_cm so you can see in the Serial Monitor what's actually going on.

Go Up