Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Control 2 motors at the same time

  • Loading branch information...
commit f243ef2d4adc59e37ab12ba6b1db02ea3d3ca4e7 1 parent e8f2562
@orlandov authored
Showing with 41 additions and 17 deletions.
  1. +41 −17 hbridge.pde
View
58 hbridge.pde
@@ -1,40 +1,64 @@
-// vim:ft=c
-//the H bridge takes two outputs from the Arduino to control the motor.
-byte pwm_pin = 9;
-byte motor_pin0 = 2;
-byte motor_pin1 = 3;
+/*
+ Control two motors from an Arduino attached to a L293D H-Bridge IC.
+ Orlando Vazquez, 2008
+ vim:ft=cpp
+*/
+
+/*
+ The H bridge takes 3 outputs from the Arduino to control each motor.
+ * enable (aka PWM)
+ * output1
+ * output2
+*/
+
+byte motor0_pwm = 9;
+byte motor0_pin0 = 2;
+byte motor0_pin1 = 3;
+
+byte motor1_pwm = 6;
+byte motor1_pin0 = 4;
+byte motor1_pin1 = 5;
// potentiometer
-byte pot_pin = 16;
+byte pot_pin = 2;
// direction switch
-byte switch_pin = 4;
+byte switch_pin = 7;
void setup() {
// setup the motor control pins as outputs
- pinMode(motor_pin0, OUTPUT);
- pinMode(motor_pin1, OUTPUT);
- pinMode(pwm_pin, OUTPUT);
+ pinMode(motor0_pwm, OUTPUT);
+ pinMode(motor0_pin0, OUTPUT);
+ pinMode(motor0_pin1, OUTPUT);
+ pinMode(motor1_pwm, OUTPUT);
+ pinMode(motor1_pin0, OUTPUT);
+ pinMode(motor1_pin1, OUTPUT);
pinMode(switch_pin, INPUT);
- pinMode(pot_pin, INPUT);
// start the motor on full to get it going
- analogWrite(pwm_pin, 255);
+ analogWrite(motor0_pwm, 255);
+ analogWrite(motor1_pwm, 255);
}
void loop() {
// read the pot
byte duty_cycle = analogRead(pot_pin) / 4;
int direction = digitalRead(switch_pin);
- analogWrite(pwm_pin, duty_cycle);
+
+ analogWrite(motor0_pwm, duty_cycle);
+ analogWrite(motor1_pwm, duty_cycle);
if (direction) {
- digitalWrite(motor_pin0, LOW);
- digitalWrite(motor_pin1, HIGH);
+ digitalWrite(motor0_pin0, LOW);
+ digitalWrite(motor0_pin1, HIGH);
+ digitalWrite(motor1_pin0, LOW);
+ digitalWrite(motor1_pin1, HIGH);
}
else {
- digitalWrite(motor_pin0, HIGH);
- digitalWrite(motor_pin1, LOW);
+ digitalWrite(motor0_pin1, LOW);
+ digitalWrite(motor0_pin0, HIGH);
+ digitalWrite(motor1_pin0, HIGH);
+ digitalWrite(motor1_pin1, LOW);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.