0

So i have an issue in which when I run my code with my ultrasonic sensors, and h bridge with motors one motor spins at all times and the other spins every 6 seconds for 2 seconds but i don't know why. Any help?

Here is the code:

int in1 = 2;
int in2 = 3;
int in3 = 4;
int in4 = 5;
int in5 = 6;
int in6 = 7;
int trig = 8;
int echol = 9;
int echor = 12;
int echof = 11;
long df, tf, dr, tr, dl, tl;
 void setup() {

  Serial.begin(9600);
    }

void loop() {

 pinMode (in1, OUTPUT);
 pinMode (in2, OUTPUT);
 pinMode (in3, OUTPUT);
 pinMode (in4, OUTPUT);
 pinMode (in5, OUTPUT);
 pinMode (in6, OUTPUT);
 pinMode (trig, OUTPUT);
 pinMode (echol, INPUT);
 pinMode (echor, INPUT);
 pinMode (echof, INPUT);


forward();

  digitalWrite (trig, HIGH);
  delay (0.01);
  tf = pulseIn (echof, HIGH);
  digitalWrite (trig, LOW);
  df = tf * 0.03156;

  if (df < 1.5){
  digitalWrite (trig, HIGH);
  delay (0.01);
  tr = pulseIn (echor, HIGH);
  tl = pulseIn (echol, HIGH);
  digitalWrite (trig, LOW);
  dr = tr * 0.03156;
  dl = tl * 0.03156;

   if (dr > dl) {

    right();
    delay (5000);
    forward();

  }
  else {

    left();
    delay (5000);
    forward();

  }

}

}



void forward(){
 digitalWrite (in1, HIGH);
 digitalWrite (in2, LOW);
 digitalWrite (in3, HIGH);
 digitalWrite (in4, LOW);
 digitalWrite (in5, HIGH);
 digitalWrite (in6, LOW);
}

void backward(){
  digitalWrite (in1, LOW);
  digitalWrite (in2, HIGH);
  digitalWrite (in3, LOW);
  digitalWrite (in4, HIGH);
  digitalWrite (in5, LOW);
  digitalWrite (in6, HIGH);
}

void left(){
  digitalWrite (in1, LOW);
  digitalWrite (in2, LOW);
  digitalWrite (in3, HIGH);
  digitalWrite (in4, LOW);
  digitalWrite (in5, HIGH);
  digitalWrite (in6, LOW);
}

void right(){
  digitalWrite (in1, HIGH);
  digitalWrite (in2, LOW);
  digitalWrite (in3, LOW);
  digitalWrite (in4, LOW);
  digitalWrite (in5, HIGH);
  digitalWrite (in6, LOW);
}

2 Answers2

0

Not sure of everything that is wrong, but multiplying a long by tl * 0.03156 and storing the value in a long is probably not doing what you intend. You should use floating-point values to contain the results of that sort of calculation.

TomServo
  • 7,248
  • 5
  • 30
  • 47
0

First of all, you should move your pin setup in Setup(), there is no need to reinitialize pin i/o setup on every loop.

void Setup()
{
  Serial.begin(9600);
  pinMode (in1, OUTPUT);
  pinMode (in2, OUTPUT);
  pinMode (in3, OUTPUT);
  pinMode (in4, OUTPUT);
  pinMode (in5, OUTPUT);
  pinMode (in6, OUTPUT);
  pinMode (trig, OUTPUT);
  pinMode (echol, INPUT);
  pinMode (echor, INPUT);
  pinMode (echof, INPUT);
}

From what I know about the H-Bridge module, they usually have 3 inputs per motor, of which only ONE should be on at a time. I could not find any correlation to that effect ion your code.... This is a constraint, so you should organize your code around it. It would make it easier to read and debug. There is no debugger on the Arduino, so organizing code does help a lot. And if you need more help, it will sure be a lot easier for others to understand what you code does.

void MotorA(int dir)
{
  // dir = 0 = STOP, +1 = Forward, -1 = Reverse
  digitalWrite(in1, dir > 0);     
  digitalWrite(in2, dir == 0);      // You gave no details on the module
  digitalWrite(in3, dir < 0);       // you have. the actual logic may differ...
}

void MotorB(int dir)
{
  // dir = 0 = STOP, +1 = Forward, -1 = Reverse
  digitalWrite(in4, dir > 0);
  digitalWrite(in5, dir == 0);
  digitalWrite(in6, dir < 0);
}

void Stop()
{
  MotorA(0);
  MotorB(0);
}

void Forward()
{
  MotorA(+1);
  MotorB(+1);
}

void Reverse()
{
  MotorA(-1);
  MotorB(-1);
}

void Left()
{
  MotorA(+1);
  MotorB(-1);
}

void Right()
{
  MotorA(-1);
  MotorB(+1);
}

May I also suggest you start with a simpler loop to start with, until you have the motors running? Add one feature at a time. This will help you get your project up and ready much faster in the end.

void Loop()
{
  Forward()
  delay(5000);
  Stop()
  delay(1000);
  Reverse();
  delay(1000);
 // ---
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19