Controlling a DC motor (12v) using a SN754410 Motor Driver Ic and 2 snap-action switches?

I am using a Arduino Uno with a SN754410 ic to control my 12v motor (exactly like diagram from http://itp.nyu.edu/physcomp/Labs/DCMotorControl) Managed to use a SPDT switch to change direction (sheer miracle for me)

I plan to put 1 snap-action switch in front of my bot and 1 at the back, so it will travel forward until it hits an obstacle, then reverse all the way until it hits another obstacle. Once the motor is activated, it will go maybe go for 5 minutes, then pause for 10mins, then on for 5 mins again, and so forth.

First attempt with the arduino so any help with detailed instructions about how to go about wiring these two extra switches and the programming code would be greatly appreciated!

Also, I need it to be mobile so I'm thinking of connecting a 12v2.2AH/20HR sealed rechargeable battery to the motor itself, and another 9v for the board cos I read it regulates it to 5v. Will this be ok? Will it be able to last my bot for a few hours?


My current code:

int switchPin = 2;    // switch input
int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9;    // H-bridge enable pin
 
  void setup() {
    // set the switch as an input:
    pinMode(switchPin, INPUT);

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);
  }
 
  void loop() {
    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPin) == HIGH) {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
    }
  }

Picture of Controlling a DC motor (12v) using a SN754410 Motor Driver Ic and 2 snap-action switches?
sort by: active | newest | oldest
Basically, you need a variable that "remembers" the last button pressed. So you need to declare

int frontswitch = //whatever
int backswitch = //whatever boolean whereami;

if (digitalRead(frontswitch) == HIGH) whereami=true; if(digitalRead(backswitch) == HIGH) whereami=false;

if (whereami) == true)
{ digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high }
// if the switch is low, motor will turn in the other direction:
else
{ digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low

 
I don;'t get what you mean about timing. When does it stop ?

The code isn't complete, you need to fill in some gaps. Now the motor should turn in one direction until you hit a switch then reverse until it hits another one.

Unlike the relay circuit, this isn't "safe", if the computer crashes, the motor may stay on and potentially burn out.
STUPID editor.

There WAS a line that said "boolean whereami;" below the int rearswitch line.....

Did you delete your other question ?
email.jodi.now (author)  steveastrouk5 years ago
Cos my question is different so yup. I really appreciated your previous help! :D Any thoughts on this one?

The switch looks like
617_Snap-action switch with 50mm lever 3-pin, SPDT, 5A.jpg
So all the notes that would be left for someone to use the work I did for you have been deleted.

Thanks for that.
email.jodi.now (author)  steveastrouk5 years ago
I was unaware that was the normal practice. I apologize
What is a "snap action switch" ? Do you mean a microswitch, or a toggle switch ?