Go Down

Topic: Tips on Elegoo robot car, only moves 1 side of wheels (Read 4649 times) previous topic - next topic

totalimpact

So I got this Elegoo robot car on amazon, line tracking, ultra sonic...

I run the 3-4 individual code samples for back/forth, and the line tracing works perfect.

But when I run the example for the obstacle avoidance, it only spins the left set of tires. I have looked through it and cant find why it always wants to go left, it has HC-SR04 ultra sonic module on a servo. I have tried re-aligning the head on the servo, no change.

Here is the code I use on it.
Code: [Select]
#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo
int Echo = A4; 
int Trig = A5;
int in1 = 6;
int in2 = 7;
int in3 = 8;
int in4 = 9;
int ENA = 5;
int ENB = 10;
int ABS = 110;
int rightDistance = 0,leftDistance = 0,middleDistance = 0 ;
void _mForward()
{
 analogWrite(ENA,ABS);
 analogWrite(ENB,ABS);
 digitalWrite(in1,LOW);
 digitalWrite(in2,HIGH);
 digitalWrite(in3,LOW);
 digitalWrite(in4,HIGH);
 Serial.println("go forward!");
}

void _mBack()
{
 analogWrite(ENA,ABS);
 analogWrite(ENB,ABS);
 digitalWrite(in1,HIGH);
 digitalWrite(in2,LOW);
 digitalWrite(in3,HIGH);
 digitalWrite(in4,LOW);
 Serial.println("go back!");
}

void _mleft()
{
 analogWrite(ENA,ABS);
 analogWrite(ENB,ABS);
 digitalWrite(in1,LOW);
 digitalWrite(in2,HIGH);
 digitalWrite(in3,HIGH);
 digitalWrite(in4,LOW);
 Serial.println("go left!");
}

void _mright()
{
 analogWrite(ENA,ABS);
 analogWrite(ENB,ABS);
 digitalWrite(in1,HIGH);
 digitalWrite(in2,LOW);
 digitalWrite(in3,LOW);
 digitalWrite(in4,HIGH);
 Serial.println("go right!");
}
void _mStop()
{
  digitalWrite(ENA,LOW);
  digitalWrite(ENB,LOW);
  Serial.println("Stop!");
}
 /*Ultrasonic distance measurement Sub function*/
int Distance_test()   
{
  digitalWrite(Trig, LOW);   
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH); 
  delayMicroseconds(20);
  digitalWrite(Trig, LOW);   
  float Fdistance = pulseIn(Echo, HIGH); 
  Fdistance= Fdistance/58;       
  return (int)Fdistance;


void setup()
{
  myservo.attach(3);// attach servo on pin 3 to servo object
  Serial.begin(9600);     
  pinMode(Echo, INPUT);   
  pinMode(Trig, OUTPUT); 
  pinMode(in1,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(in3,OUTPUT);
  pinMode(in4,OUTPUT);
  pinMode(ENA,OUTPUT);
  pinMode(ENB,OUTPUT);
  _mStop();
}

void loop()
{
    myservo.write(90);//setservo position according to scaled value
    delay(500);
    middleDistance = Distance_test();
    #ifdef send
    Serial.print("middleDistance=");
    Serial.println(middleDistance);
    #endif

    if(middleDistance<=20)
    {     
      _mStop();
      delay(500);                         
      myservo.write(5);         
      delay(1000);     
      rightDistance = Distance_test();

      #ifdef send
      Serial.print("rightDistance=");
      Serial.println(rightDistance);
      #endif

      delay(500);
       myservo.write(90);             
      delay(1000);                                                 
      myservo.write(180);             
      delay(1000);
      leftDistance = Distance_test();

      #ifdef send
      Serial.print("leftDistance=");
      Serial.println(leftDistance);
      #endif

      delay(500);
      myservo.write(90);             
      delay(1000);
      if(rightDistance>leftDistance) 
      {
        _mright();
        delay(180);
       }
       else if(rightDistance<leftDistance)
       {
        _mleft();
        delay(180);
       }
       else if((rightDistance<=20)||(leftDistance<=20))
       {
        _mBack();
        delay(180);
       }
       else
       {
        _mForward();
       }
    } 
    else
        _mForward();                     
}


handy99

hi it looks like the servo library uses pin 9 and 10 i moved pin 10 to 11 and changed it is the program int ENB =11 and now it works fine


detach()

Description

Detach the Servo variable from its pin. If all Servo variables are detached, then pins 9 and 10 can be used for PWM output with analogWrite().

Syntax

servo.detach()

Parameters

servo: a variable of type Servo

See also

attach()
attached()
Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

Share

Servo

attach()

Description

Attach the Servo variable to a pin. Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.

Syntax

servo.attach(pin)
servo.attach(pin, min, max)

Parameters

servo: a variable of type Servo

pin: the number of the pin that the servo is attached to

min (optional): the pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)

max (optional): the pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400)

Example

#include <Servo.h>

Servo myservo;

void setup()
{
  myservo.attach(9);
}

void loop() {}

See also

attached()
detach()
Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

Share

rcacacho

could share the code example I am new to Arduino

aishen

Hi,
I just tried Robot car  version 1
I had to change motors connection (1red/1black, not 2 red/2blacks)
I am wondering if changing pin 10 to 11 is a good idea (as elegoo suggest, but I change tracking module one to pin 10 because it was on eleven ? I haven't tried yet but does someone did it ?
Any suggestion ?
Regards
Henri

duino_nano

Hi,

Did you tried to display, on the Serial Monitor, the distance that the HC-SR04 read?

My question is because I dont see in your program (section "setup") a small instruction to initiate the SR04 such as:

PWM_Mode_Setup();

Code: [Select]

void setup()
{ Serial.begin(9600);                   // 9600 bauds pour connexion série

 
  PWM_Mode_Setup();
 
 }


void PWM_Mode_Setup()
      { pinMode(URTRIG,OUTPUT);                           // A low pull on pin COMP/TRIG   
        digitalWrite(URTRIG,HIGH);                           // Set to HIGH     
        pinMode(URPWM, INPUT);                             // Sending Enable PWM mode command
       
        for(int i=0;i<4;i++)
            { Serial.write(EnPwmCmd[i]);}
      }




Go Up