Help with Forward-reverse Switches for Two Motor Bi-directional controller

Hello,

I need a bit of help getting the forward-reverse logic out of two switches, one for each motor, for an H-bridge motor controller (L6203). I would like the high-low state of a pin to change motor direction.

I've got the switches wired and have verified thier output, the change from high-low works good. But getting the flip-flop high/low from two pins that the switch high/low should flip-flop is giving me no joy. Given my sketch skills, its in the program. I've tried using the switch statement but no luck and "if and else" statements and this one is just if statement with boolean for pins 6 & 7 state.

What I woud like to do:
The high/low state on pins 6 & 7 to flip-flop high/low on pins 4,5 and 7,8 respectfully.
After gettting this going, going to add debounce to both switches. But that is another day.

Hope this is enough information and that I have presented it well enough.

I would be great to get a pointer or two! TIA!

Regards,
Tom

/*32 KHz PWM DC motor controller L6203 Bridge Driver
20120114 Included forward-reverse switches pins
*/
int inputMotor1Variable = 0; // Motor1 assigns value of 0 
int inputMotor2Variable = 0; // Motor2 assigns value of 0 
int pinA = 3; // pins 3 11 are PWM output controlled by Timer2
int pinB = 11;
boolean inputPin1 = 6;  // input 1 to pin 6
boolean inputPin2 = 7;  // input 2 to pin 7
int motor1IN1 = 4;  // motor 1 IN1
int motor1IN2 = 5;  // motor 1 IN2
int motor2IN1 = 8;  // motor 2 IN1
int motor2IN2 = 9;  // motor 2 IN2

void setup(){

//__ set TIMER2 for PWM 32KHz
byte mask = B11111000;
TCCR2B &= mask;
TCCR2B |= (0<<CS22) | (0<<CS21) | (1<<CS20); // same as TCCR2B |= B00000001; TCCR2B is now xxxxx001

//__pinmode
pinMode(pinA,OUTPUT);
pinMode(pinB,OUTPUT);
pinMode(motor1IN1,OUTPUT);  // declare pin4 as output
pinMode(motor1IN2,OUTPUT);  // declare pin5 as output
pinMode(motor2IN1,OUTPUT);  // declare pin8 as output
pinMode(motor2IN2,OUTPUT);  // declare pin9 as output
pinMode(inputPin1,INPUT);  // declare pin6 as input
pinMode(inputPin2,INPUT);  // declare pin7 as input
}

void loop()
{
inputMotor1Variable = analogRead(1); // set variable to value of  analog pin 1
inputMotor1Variable = map(inputMotor1Variable, 0, 1023, 0, 255); //map to value 0-255 to use for PWM
analogWrite(pinA,inputMotor1Variable); // Alt PWM output

inputMotor2Variable = analogRead(2); // set variable to value of //analog pin 2
inputMotor2Variable = map(inputMotor2Variable, 0, 1023, 0, 255); //map to value 0-255 to use for PWM
analogWrite(pinB,inputMotor2Variable); //Az PWM output

digitalRead(inputPin1);  // read inputPin1
if (inputPin1 == true)  // check if inputPin1, pin6, is HIGH
  {
  digitalWrite(motor1IN1,LOW);
  digitalWrite(motor1IN2,HIGH);  // Motor1 IN1=Low Motor1 IN2 = High
  }
if (inputPin1 == false)
{
  digitalWrite(motor1IN1,HIGH);
  digitalWrite(motor1IN2,LOW);  // Motor1 IN1 = High Motor1 IN2 = Low
} 
digitalRead(inputPin2);  // read inputPin2
if (inputPin2 == true)  // check if inputPin2, Pin7, is HIGH
  {
  digitalWrite(motor2IN1,LOW);
  digitalWrite(motor2IN2,HIGH);  // Motor2 IN1=Low Motor2IN2 = High
  }
if (inputPin2 == false)
{
  digitalWrite(motor2IN1,HIGH);
  digitalWrite(motor2IN2,LOW);  // Motor2 IN1 = High Motor2 IN2 = Low
}
}

You're using digitalRead the wrong way. See http://arduino.cc/en/Reference/DigitalRead

Thanks. That is something I forgot (well, did not know to change) when making the change for int to boolean for pins 6 & 7. I'll change back from boolean to int.
Any other mistakes you see?

Cordially,
Tom

Got the changes made. But no joy.

/*32 KHz PWM DC motor controller L6203 Bridge Driver
20120114 Included forward-reverse switches pins
*/
int inputMotor1Variable = 0; // Motor1 assigns value of 0 
int inputMotor2Variable = 0; // Motor2 assigns value of 0 
int pinA = 3; // pins 3 11 are PWM output controlled by Timer2
int pinB = 11;
int inputPin1 = 6;  // input 1 to pin 6
int inputPin2 = 7;  // input 2 to pin 7
int motor1IN1 = 4;  // motor 1 IN1
int motor1IN2 = 5;  // motor 1 IN2
int motor2IN1 = 8;  // motor 2 IN1
int motor2IN2 = 9;  // motor 2 IN2

void setup(){

//__ set TIMER2 for PWM 32KHz
byte mask = B11111000;
TCCR2B &= mask;
TCCR2B |= (0<<CS22) | (0<<CS21) | (1<<CS20); // same as TCCR2B |= B00000001; TCCR2B is now xxxxx001

//__pinmode
pinMode(pinA,OUTPUT);
pinMode(pinB,OUTPUT);
pinMode(motor1IN1,OUTPUT);  // declare pin4 as output
pinMode(motor1IN2,OUTPUT);  // declare pin5 as output
pinMode(motor2IN1,OUTPUT);  // declare pin8 as output
pinMode(motor2IN2,OUTPUT);  // declare pin9 as output
pinMode(inputPin1,INPUT);  // declare pin6 as input
pinMode(inputPin2,INPUT);  // declare pin7 as input
}

void loop()
{
inputMotor1Variable = analogRead(1); // set variable to value of  analog pin 1
inputMotor1Variable = map(inputMotor1Variable, 0, 1023, 0, 255); //map to value 0-255 to use for PWM
analogWrite(pinA,inputMotor1Variable); // Alt PWM output

inputMotor2Variable = analogRead(2); // set variable to value of //analog pin 2
inputMotor2Variable = map(inputMotor2Variable, 0, 1023, 0, 255); //map to value 0-255 to use for PWM
analogWrite(pinB,inputMotor2Variable); //Az PWM output

digitalRead(inputPin1);  // read inputPin1
if (inputPin1 == HIGH)  // check if inputPin1, pin6, is HIGH
  {
  digitalWrite(motor1IN1,LOW);
  digitalWrite(motor1IN2,HIGH);  // Motor1 IN1=Low Motor1 IN2 = High
  }
if (inputPin1 == LOW)
{
  digitalWrite(motor1IN1,HIGH);
  digitalWrite(motor1IN2,LOW);  // Motor1 IN1 = High Motor1 IN2 = Low
} 
digitalRead(inputPin2);  // read inputPin2
if (inputPin2 == HIGH)  // check if inputPin2, Pin7, is HIGH
  {
  digitalWrite(motor2IN1,LOW);
  digitalWrite(motor2IN2,HIGH);  // Motor2 IN1=Low Motor2IN2 = High
  }
if (inputPin2 == LOW)
{
  digitalWrite(motor2IN1,HIGH);
  digitalWrite(motor2IN2,LOW);  // Motor2 IN1 = High Motor2 IN2 = Low
}
}
digitalRead(inputPin1);  // read inputPin1
if (inputPin1 == true)  // check if inputPin1, pin6, is HIGH
  {
  digitalWrite(motor1IN1,LOW);
  digitalWrite(motor1IN2,HIGH);  // Motor1 IN1=Low Motor1 IN2 = High
  }
if (digitalRead(inputPin1) == HIGH)  // check if inputPin1, pin6, is HIGH
{
    digitalWrite(motor1IN1,LOW);
    digitalWrite(motor1IN2,HIGH);  // Motor1 IN1=Low Motor1 IN2 = High
}

Didn't you read the reference page I linked for digitalRead() ? Or is there something in that page you don't understand ? If so, feel free to ask.

digitalRead(inputPin1);  // read inputPin1

The digitalRead() function returns the state of the switch attached to the pin. You discard that.

if (inputPin1 == true)  // check if inputPin1, pin6, is HIGH

How can this possibly be true? You assigned the value 6 to the pin. The test should be for the value read from the pin, that you discarded, to be HIGH or LOW, not true or false.

mromani:
Didn't you read the reference page I linked for digitalRead() ? Or is there something in that page you don't understand ? If so, feel free to ask.

:slight_smile: read it
could be the understanding part....
better read it again.
Thanks for your input
Tom

PaulS:

digitalRead(inputPin1);  // read inputPin1

The digitalRead() function returns the state of the switch attached to the pin. You discard that.

if (inputPin1 == true)  // check if inputPin1, pin6, is HIGH

How can this possibly be true? You assigned the value 6 to the pin. The test should be for the value read from the pin, that you discarded, to be HIGH or LOW, not true or false.

does this address that?

//__pinmode
pinMode(pinA,OUTPUT);
pinMode(pinB,OUTPUT);
pinMode(motor1IN1,OUTPUT);  // declare pin4 as output
pinMode(motor1IN2,OUTPUT);  // declare pin5 as output
pinMode(motor2IN1,OUTPUT);  // declare pin8 as output
pinMode(motor2IN2,OUTPUT);  // declare pin9 as output
pinMode(inputPin1,INPUT);  // declare pin6 as input
pinMode(inputPin2,INPUT);  // declare pin7 as input
}

does this address that?

No.

You set the value of inputPin1:

int inputPin1 = 6;  // input 1 to pin 6

Nothing in the code changes that (which is a good thing, as it is unlikely that the Arduino unplugged a switch wire and plugged it in elsewhere). So, 6 is not HIGH.

int switchState1 = digitalRead(inputPin1);
if(switchState1 == HIGH)
{
}

is what you want to be doing.

Bonus!
Y'all got me on the right track. Thank you! Going to wire it to the H bridge now.
Regards,
Tom

/*32 KHz PWM DC motor controller L6203 Bridge Driver
20120114 Included forward-reverse switches pins
*/
int inputMotor1Variable = 0; // Motor1 assigns value of 0 
int inputMotor2Variable = 0; // Motor2 assigns value of 0 
int pinA = 3;       // pins 3 11 are PWM output controlled by Timer2
int pinB = 11;
int inputPin1 = 6;  // input 1 to pin 6 motor 1 forward reverse switch
int inputPin2 = 7;  // input 2 to pin 7 motor 2 forward reverse switch
int motor1IN1 = 4;  // motor 1 IN1 motor 1 IN1
int motor1IN2 = 5;  // motor 1 IN2 motor 1 IN2
int motor2IN1 = 8;  // motor 2 IN1 motor 2 IN1
int motor2IN2 = 9;  // motor 2 IN2 motor 2 IN2
int val1 = 1;       // variable to store the read value motor 1
int val2 = 1;       // variable to store the read value motor 2

void setup(){

//__ set TIMER2 for PWM 32KHz
byte mask = B11111000;
TCCR2B &= mask;
TCCR2B |= (0<<CS22) | (0<<CS21) | (1<<CS20); // same as TCCR2B |= B00000001; TCCR2B is now xxxxx001

//__pinmode
pinMode(pinA,OUTPUT);       // declare pin3 as output
pinMode(pinB,OUTPUT);       // declare pin11 as output
pinMode(motor1IN1,OUTPUT);  // declare pin4 as output
pinMode(motor1IN2,OUTPUT);  // declare pin5 as output
pinMode(motor2IN1,OUTPUT);  // declare pin8 as output
pinMode(motor2IN2,OUTPUT);  // declare pin9 as output
pinMode(inputPin1,INPUT);   // declare pin6 as input
pinMode(inputPin2,INPUT);   // declare pin7 as input
}

void loop()
{
inputMotor1Variable = analogRead(1); // set variable to value of  analog pin 1
inputMotor1Variable = map(inputMotor1Variable, 0, 1023, 0, 255); //map to value 0-255 to use for PWM
analogWrite(pinA,inputMotor1Variable); // Alt PWM output

inputMotor2Variable = analogRead(2); // set variable to value of //analog pin 2
inputMotor2Variable = map(inputMotor2Variable, 0, 1023, 0, 255); //map to value 0-255 to use for PWM
analogWrite(pinB,inputMotor2Variable); //Az PWM output

val1 = digitalRead(inputPin1);   // input 1 to pin 6 motor 1 forward reverse switch
if (val1 == HIGH)                // val1 motor1 flip flop variable
{
    digitalWrite(motor1IN1,LOW);
    digitalWrite(motor1IN2,HIGH);  // Motor1 IN1=Low Motor1 IN2 = High
  }

if (digitalRead(inputPin1) == LOW)  // check if inputPin1, pin6, is LOW
{
  digitalWrite(motor1IN1,HIGH);
  digitalWrite(motor1IN2,LOW);  // Motor1 IN1 = High Motor1 IN2 = Low
} 

val2 = digitalRead(inputPin2);   // pin 7 motor 2 forward reverse switch
if (val2 == HIGH)                // val2 motor2 flip flop variable
{
    digitalWrite(motor2IN1,LOW);
    digitalWrite(motor2IN2,HIGH);  // Motor2 IN1=Low Motor2 IN2 = High
  }

if (digitalRead(inputPin2) == LOW)  // check if inputPin2, pin7, is LOW
{
  digitalWrite(motor2IN1,HIGH);
  digitalWrite(motor2IN2,LOW);  // Motor1 IN2 = High Motor2 IN2 = Low
}
}

Thanks! I'll try this as well. Precise.

PaulS:

does this address that?

No.

You set the value of inputPin1:

int inputPin1 = 6;  // input 1 to pin 6

Nothing in the code changes that (which is a good thing, as it is unlikely that the Arduino unplugged a switch wire and plugged it in elsewhere). So, 6 is not HIGH.

int switchState1 = digitalRead(inputPin1);

if(switchState1 == HIGH)
{
}



is what you want to be doing.
val2 = digitalRead(inputPin2);   // pin 7 motor 2 forward reverse switch
if (val2 == HIGH)                // val2 motor2 flip flop variable
{
    digitalWrite(motor2IN1,LOW);
    digitalWrite(motor2IN2,HIGH);  // Motor2 IN1=Low Motor2 IN2 = High
  }

if (digitalRead(inputPin2) == LOW)  // check if inputPin2, pin7, is LOW
{
  digitalWrite(motor2IN1,HIGH);
  digitalWrite(motor2IN2,LOW);  // Motor1 IN2 = High Motor2 IN2 = Low
}
  • Be consistent in your coding: if you need to read a pin status, don't do it in two different ways in a few lines of code.
  • Don't read hardware state more than once while you're deciding how to react to it. It could change in the middle of your "reasoning".
  • Use meaningful names for your variables, instead of copy-n-pasting a comment describing what they're for every time you use them.
val2 = digitalRead(inputPin2);   // pin 7 motor 2 forward reverse switch
if (val2 == HIGH) {
    digitalWrite(motor2IN1,LOW);
    digitalWrite(motor2IN2,HIGH);
}
else {
    digitalWrite(motor2IN1,HIGH);
    digitalWrite(motor2IN2,LOW);
}

(If you don't use inputPin2 state elsewhere, you could also avoid using val2).