Tuesday, March 06, 2007

H-Bridge Board + Arduino

Completed H-Bridge board with major components attached + connection wires for Arduino. This board allows for two motors + four inputs slots (for sensors, switches, etc.) to be create with Arduino. Arduino is now able to control the direction of two motors as well as have four inputs (if necessary) to detect + control. The following are two sets of code; one used to control the drawing surface + the other to control the drawing device.

Controlling the Drawing Surface:

/* Controlling Two DC Motors Using H-Bridge + PhotoCells
* ------------
*
* Uses an H-Bridge (L293E or SN754410) to control the direction of two DC motors.
* Additional DC motor added. Photocells used to control direction of each motor.
* Modification to Physical Computing tutorial:
* http://itp.nyu.edu/physcomp/Labs/DCMotorControl
*
* Modified 28 February 2007
* By Kyle Janzen
* http://kylejanzen.blogspot.com/
*
* based on an original by Physical Computing @ ITP
*/

int photo1Pin = 1; // select analog input pin for photocell1
int photo2Pin = 2; // select analog input pin for photocell2
int val1 = 0; // variable to store the value coming from photocell1
int val2 = 0; // variable to store the value coming from photocell2
int motor1Pin = 3; // H-bridge leg 1
int motor2Pin = 4; // H-bridge leg 2
int motor3Pin = 5; // H-bridge leg 3
int motor4Pin = 6; // H-bridge leg 4
int speed1Pin = 9; // H-bridge enable pin 1-2
int speed2Pin = 10; // H-bridge enable pin 3-4

void setup()
{

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

// set speedPin high so that motor can turn on:
digitalWrite(speed1Pin, HIGH);
digitalWrite(speed2Pin, HIGH);

}

void loop()
{
val1 = analogRead(photo1Pin); // set val1 to equal reading of photo1Pin
val2 = analogRead(photo2Pin); // set val2 to equal reading of photo2Pin
if (val1 < 280) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
delay(val2*5); // determines time interval that motor is activate
digitalWrite(motor3Pin, HIGH); // set leg 3 of the H-bridge high
digitalWrite(motor4Pin, LOW); // set leg 4 of the H-bridge low
delay(val2*6); // determines time interval that motor is activate
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge high
delay(val2*5); // determines time interval that motor is activate
digitalWrite(motor3Pin, LOW); // set leg 3 of the H-bridge low
digitalWrite(motor4Pin, HIGH); // set leg 4 of the H-bridge high
delay(val2*6); // determines time interval that motor is activate
}else if (val1 > 350){
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
delay(val2*2); // determines time interval that motor is activate
digitalWrite(motor3Pin, HIGH); // set leg 3 of the H-bridge high
digitalWrite(motor4Pin, LOW); // set leg 4 of the H-bridge low
delay(val2*2); // determines time interval that motor is activate
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge low
delay(val2*2); // determines time interval that motor is activate
digitalWrite(motor3Pin, LOW); // set leg 3 of the H-bridge low
digitalWrite(motor4Pin, HIGH); // set leg 4 of the H-bridge high
delay(val2*2); // determines time interval that motor is activate
}else{
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor3Pin, LOW); // set leg 3 of the H-bridge low
digitalWrite(motor4Pin, LOW); // set leg 4 of the H-bridge low
delay(val2*5); // determines time interval that motor is deactivate
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor3Pin, LOW); // set leg 3 of the H-bridge low
digitalWrite(motor4Pin, LOW); // set leg 4 of the H-bridge low
delay(val2*6); // determines time interval that motor is deactivate
}
}

Controlling the Drawing Device:

/* Controlling the Speed of a DC Motor Using H-Bridge + PhotoCell
* ------------
*
* Uses an H-Bridge (L293E or SN754410) to control the speed of a DC motors.
* Photocells used to control the speed of the motor. Analog PWM (pins 9-11) creates
* pulse for motor speed, which is controlled by photocell input.
* Modification to Physical Computing tutorial:
* http://itp.nyu.edu/physcomp/Labs/DCMotorControl
*
* Modified 3 March 2007
* By Kyle Janzendf
* http://kylejanzen.blogspot.com/
*
* based on an original by Physical Computing @ ITP
*/

int photo1Pin = 1; // select analog input pin for photocell1
int val1 = 0; // variable to store the value coming from photocell1
int motor1Pin = 3; // H-bridge leg 1
int motor2Pin = 4; // H-bridge leg 2
int speed1Pin = 9; // H-bridge enable pin 1-2


void setup()
{

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

// set speedPin high so that motor can turn on:
analogWrite(speed1Pin, HIGH);

}

void loop()
{
val1 = analogRead(photo1Pin); // set val1 to equal reading from photocell
if (val1 < 475) { // stops motor is reading is less than #
digitalWrite(motor1Pin, LOW); // set motor1pin low
digitalWrite(motor2Pin, LOW); // set motor2pin low
}else{
digitalWrite(motor1Pin, HIGH); // set motor1pin high
digitalWrite(motor2Pin, LOW); // set motor2pin low
analogWrite(speed1Pin, val1/10 ); // determines speed of motor using analog PWM
}
}

No comments: