0

I am new to electronics and I'm having a problem when creating my project. I have created a Bluetooth controlled rover using HC-06, Dual H-bridge motor driver and 4 DC motors. However, only two of the DC Motors are moving and the other two remain still.

Could you please suggest what happened?

Attached is the wiring of the motors and code.

char t;

void setup() {
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available()) {
    t = Serial.read();
    Serial.println(t);
  }

  if(t == '1') {
    digitalWrite(9,HIGH);
    digitalWrite(10,LOW);
    digitalWrite(11,HIGH);
    digitalWrite(12,LOW);
  }

  else if(t == '2') {
    digitalWrite(9,LOW);
    digitalWrite(10,HIGH);
    digitalWrite(11,LOW);
    digitalWrite(12,HIGH);
  }

  else if(t == '3') {
    digitalWrite(9,LOW);
    digitalWrite(10,LOW);
    digitalWrite(11,HIGH);
    digitalWrite(12,LOW);
  }

  else if(t == '4'){ 
    digitalWrite(9,HIGH);
    digitalWrite(10,LOW);
    digitalWrite(11,LOW);
    digitalWrite(12,LOW);
  }

  else if(t == '5') {
    digitalWrite(9,LOW);
    digitalWrite(10,LOW);
    digitalWrite(11,LOW);
    digitalWrite(12,LOW);
  }

  delay(100);
}

Wiring diagram

  • 1
    I have seen this question a number of times. What have you done to investigate the problem? Did you test each thing on its own: the arduino board, the driver module and the motors. – Jot Oct 18 '18 at 15:22
  • 1
    Why are you posting a new question, instead of editing your own? Duplicate of arduino.stackexchange.com/questions/56995/… – chrisl Oct 18 '18 at 15:32
3

To analyze your problem:

  1. Switch two wires between two motors (and repeat this to get all combinations), to find out if the code/wiring until the motors is wrong, or there is a problem with the motors.
  2. Print out the values of pins 9, 10, 11 and 12 and the value of t/input to see if the pins are controlled as you expect.
  3. Remove all but one motors, for all motors one at a time, to see if there is no influence between them.

Below is not an answer to your question, but I can only align the code as answer.

It is better coding style to use the following form:

void loop() {
  if(Serial.available()){
    t = Serial.read();
    Serial.println(t);
  }

  if ((t >= '1') && (t <= '5'))      
  {
    int input = (int) t - (int)'0';

    digitalWrite( 9, (input == 1) || (input == 4) ? HIGH : LOW);
    digitalWrite(10, (input == 2)                 ? HIGH : LOW);
    digitalWrite(11, (input == 1) || (input == 3) ? HIGH : LOW);
    digitalWrite(12, (input == 2)                 ? HIGH : LOW);
  }

  delay(100);
}

Note: ? : is a ternary operator which means : a ? b : c => if a then b else c.

Instead of using 9, 10, 11, 12 define pin numbers (e.g. PIN_MOTOR_X), and for 1, 2, 3, 4, 5 input numbers define LEFT, RIGHT, UP/FORWARD, DOWN/BACKWARD, STOP.

  • I don't know if I agree that code is better coding style, but it is definitely much more compact. I slightly disagree with it being "better", because it's less obvious as to what the code does. Part of good code style is making things readable. This isn't bad code, just not obvious. I definitely agree that naming the motors will make things a lot better. – computercarguy Oct 18 '18 at 18:32
  • @computercarguy Actually it would maybe be better to write a separate digitalWrite that has a parameter in for the 'boolean' so the ternary operator would not be needed (four times). (assuming the ternary operator is what bothers you, which I can understand). – Michel Keijzers Oct 18 '18 at 22:19
  • Yes, there's a lot of different ways to write this. The way you did it just isn't one I'd be comfortable supporting, and that's only because of the mental load to figure out what's going on. I tend to write things more plainly/simply, so I don't have to really think about it when I have to read it later. Abstracting the digitalWrite would probably be one of the better ways to simplify this. And yes, when there's more than one ternary in a block of code, I kind of get the cold sweats. :-) I know how easy it is to abuse them. – computercarguy Oct 18 '18 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.