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
}
}