PWM DC Motor drive using H Bridge and momentary inputs for direction

I would like to drive 3 DC motors using 3 H Bridges and 6 MOM switches. Each motor will have a pair of MOM switches to drive CW or CCW.
The schematic is breadboarded and code attached will compile, I can see the inputs toggle with the serial monitor but the motor never turns. I have cycled the motor in both directions with another simple test code that only sends PWM outputs to the driver. I tried some delays and left that commented out in the code posted.
I’d like to get the code working with one motor and then copy for three.
Any help would be greatly appreciated.

[code]

// initialize digital pins.
int upA = 4;          // const DI pin name for pin
int upA_val = 0;      // variable to store the read DI value

int dnA = 5;          // const DI pin name for pin
int dnA_val = 0;      // variable to store the read DI value

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  pinMode(upA, INPUT);
  pinMode(dnA, INPUT);

  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {

  upA_val = digitalRead(upA);
  dnA_val = digitalRead(dnA);

  if (upA_val == 1) {
    analogWrite(13, 128);  // turn the motor in one direction
    analogWrite(11, LOW);  //
  }

  if (dnA_val == 1) {
    analogWrite(13, LOW);  // turn the motor in the other direction
    analogWrite(11, 128);  //
  }
  else
    analogWrite(13, LOW);   // stop the motor for any other option 0,0 or 1,1
    analogWrite(11, LOW);   //
  Serial.println(upA_val);
  Serial.println(dnA_val);

  //delay(1);        // delay in between reads for stability

}

[/code]

Try some {} round the lines you intend to be controlled by the else. And it would make more sense to use 0 with analogWrite not LOW.

Steve

Thanks for the guidance. I changed the LOWs to 0s and added the {} around the lines ELSE controls. The motor would then turn in one direction, but would only humm in the other direction. After swapping wires, input pins, etc, I swapped the two IF sections in the code and the motor would only turn in the opposite direction. I pasted the ELSE section in after the first IF and that seems to working be properly. Does this present any potential problems??

I would like to control 3 motors with one MICRO. I have read that a long sting of IF statements is not ideal. SWITCH is mentioned as an alternative. IF the IFs will not work well I can read up and SWITCH to SWITCH. And likely be back here with a question or two.

Thanks for the quick reply slipstick Steve

[code]
  //  Drive DC motor CW when "up" MOM switch is held, CCW when "dn" MOM switch is held
 //    speed is controlled by PWM / analogWrite
 // 
  
   
  // initialize digital pins.
  int upA = 5;          // DI pin name for pin
  int upA_val = 0;      // variable to store the read DI value, initialized

  int dnA = 4;          // DI pin name for pin
  int dnA_val = 0;      // variable to store the read DI value, initialized

  int outA1 = 13;       //PWM pin
  int outA2 = 11;        //PWM pin
  
// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  pinMode(upA, INPUT);
  pinMode(dnA, INPUT);
  
  pinMode(outA1, OUTPUT);
  pinMode(outA2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  
  upA_val = digitalRead(upA);  // update 4 variable with DI 4 value
  dnA_val = digitalRead(dnA);    //  update 5 variable with DI 5 value
  
  
  
   if (dnA_val == 1){              //  
  analogWrite(outA1,0);   // turn the motor in the other direction
  analogWrite(outA2,128);   // 
  Serial.println(upA_val);
  Serial.println(dnA_val);
   }
 else  {
  analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
  analogWrite(outA2, 0);   // 
  Serial.println(upA_val);
  Serial.println(dnA_val);
   }
if (upA_val == 1){      //  
  analogWrite(outA1,128);   // turn the motor in one direction
  analogWrite(outA2,0);   // 
  Serial.println(upA_val);
  Serial.println(dnA_val);
  }
   
   else  {
  analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
  analogWrite(outA2, 0);   // 
  Serial.println(upA_val);
  Serial.println(dnA_val);
   }
 // delay(1);        // delay in between reads for stability

}

[/code]

If you are saying you want to have 6 switches and 3 motors which only run as long as a switch is pressed then there is nothing wrong with a set of if statements that should cause you any problems. Obviously you'll need another L293.

Switch/case only works on numeric variables so it would just complicate things in your case.

Steve

The motor runs slowly and the speed will not change. Editing the analogWrite values does not change the speed. I previously tested the motor controller with only analogWrite outputs and successfully changed directions and speeds.

The inputs or IF statements seem to be causing a problem.

I tried to use while and the motor would turn but not stop when the input would go low. Also attempted a break and results were the same. This second rev is below.

[code]
  //  Drive DC motor CW when up MOM switch is held, CCW
//  when dn MOM switch is held
 //  results using IF  ELSE:
 // motor speed changed but was pulsing now the motion is the same
 // in both directions but still vibrates
 //
 // this is test using WHILE - will not stop when input chg back to low
 //
  
   
  // initialize digital pins.
  int upA = 5;          // DI pin name for pin
  int upA_val = 0;      // variable to store the read DI value, initialized

  int dnA = 4;          // DI pin name for pin
  int dnA_val = 0;      // variable to store the read DI value, initialized

  int outA1 = 13;       //PWM pin
  int outA2 = 11;        //PWM pin
  
// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  pinMode(upA, INPUT);
  pinMode(dnA, INPUT);
  
  pinMode(outA1, OUTPUT);
  pinMode(outA2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  
  upA_val = digitalRead(upA);  // update 4 variable with DI 4 value
  dnA_val = digitalRead(dnA);    //  update 5 variable with DI 5 value
                                  // digitalRead
  
   while (dnA_val == 1){              //  
    analogWrite(outA1,0);   // turn the motor in one direction
    analogWrite(outA2,125);   // 
          
      if (dnA_val == 0) {     // 
        analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
        analogWrite(outA2, 0);   // 
    break;
  }
   }
    
    analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
    analogWrite(outA2, 0);   // 
         
   //delay(10);        // delay in between reads

  upA_val = digitalRead(upA);  // update 4 variable with DI 4 value
  dnA_val = digitalRead(dnA);    //  update 5 variable with DI 5 value
   
  while(upA_val == 1){      //  
  analogWrite(outA1,125);   // turn the motor in the opposite direction
  analogWrite(outA2,0);   // 
  
    
    if (dnA_val == 0) {     // 
        analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
        analogWrite(outA2, 0);   // 
    break;
  }
  }
   
  analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
  analogWrite(outA2, 0);   // 
  
       
  //delay(10);        // delay in between reads

}

[/code]

  while (dnA_val == 1){              // 
...      
      if (dnA_val == 0) {     //
... 
      }
   }

What do you reckon the chances are that dnA_val will be 0 while dnA_val is 1? And nothing inside the while ever changes dnA_val so how can you ever get out of that while loop?

Similar problems in the second while loop except you check upA_val for 1 and then dnA_val for 0. Did you really mean that?

Steve

Chances are pretty slim but if I add a digital Read in the while it might. Will do the same to the second while and correct so both while and if are checking upA.

thank you again. I read "Something must change the tested variable" but of course, the switch is changing - so that is taken care of. I measured it with my meter.

Now I think I see

[color=#222222][/color]
  while (dnA_val == 1){              //[color=#222222][/color]
  dnA_val = digitalRead(dnA);    //  update 5 variable with DI 5 value[color=#222222][/color]
      if (dnA_val == 0) {     //[color=#222222][/color]
...[color=#222222][/color]
      }[color=#222222][/color]
   }

Rev 4 works - thanks for the help I will try it without the break

[code]
  //  Drive DC motor CW when up MOM switch is held, CCW
 //   when dn MOM switch is held
 //  
 //   r3 added digitalReads inside while loops 
 //   r4 corrects up and dn and input pins that are commented
  
   
  // initialize digital pins as global variables
  
  int upA = 5;          // input will drive up
  int upA_val = 0;      // variable to store the read up DI value, initialized

  int dnA = 4;          // input will drive down
  int dnA_val = 0;      // variable to store the read down DI value, initialized

  int outA1 = 13;       //PWM pin to drive the motor H Bridge input
  int outA2 = 11;       //PWM pin to drive the motor H Bridge input
  
// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  pinMode(upA, INPUT);   
  pinMode(dnA, INPUT);
  
  pinMode(outA1, OUTPUT);
  pinMode(outA2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {

 //  Check down input and drive  down if it is +
  
  dnA_val = digitalRead(dnA);    // update up A variable with DI pin 4 value
                                   
   while (dnA_val == 1){        //  
    analogWrite(outA1,0);       // turn the motor in one direction
    analogWrite(outA2,225);     // 

  dnA_val = digitalRead(dnA);    //  update 4 variable with DI 4 value
          
      if (dnA_val == 0) {        // 
        analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
        analogWrite(outA2, 0);   // 
    break;
    }
   }
    
    analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
    analogWrite(outA2, 0);   // 
         
   //delay(10);        // delay in between reads




 //  Check up input and drive  down up if it is +


  upA_val = digitalRead(upA);    //  update 5 variable with DI 5 value
   
  while(upA_val == 1){      //  
  analogWrite(outA1,225);   // turn the motor in the opposite direction
  analogWrite(outA2,0);     // 

  upA_val = digitalRead(upA);  // update 5 variable with DI 5 value
      
    if (upA_val == 0) {     // 
        analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
        analogWrite(outA2, 0);   // 
    break;
    }
  }
   
  analogWrite(outA1, 0);   // stop the motor for any other option 0,0 or 1,1
  analogWrite(outA2, 0);   // 
  
       
  //delay(10);        // delay in between reads

}

[/code]