Вы находитесь на странице: 1из 2

/* * CIRC-22 Controlling Motors (L293D) * * A demo program which demonstrates how to control two * DC motors * For more details

and links visit: http://tinyurl.com/nsuwln */ //L293 Pin assignments //Motor one int enable12 = 9; //bridge 1 and 2 enable (pin 1 on the 16 pin IC) //pull this pin HIGH to turn the outputs on and LOW to turn t hem off //using PWM on this puin will control speed int in1 = 2; //in1 (out1 will be HIGH when set HIGH and LOW when set LOW) (pin 2 on the 16 pin IC) int in2 = 3; //in2 (out2 will be HIGH when set HIGH and LOW when set LOW) (pin 7 on the 16 pin IC) //Motor two int enable34 = 10; //bridge 1 and 2 enable (pin 1 on the 16 pin IC) //pull this pin HIGH to turn the outputs on and LOW to turn them off //using PWM on this puin will control speed int in3 = 4; //in1 (out1 will be HIGH when set HIGH and LOW when set LOW) (pin 2 on the 16 pin IC) int in4 = 5; //in2 (out2 will be HIGH when set HIGH and LOW when set LOW) (pin 7 on the 16 pin IC) void setup(){ pinMode(enable12, OUTPUT); //Set the three pins for bridge 1 & 2 to outputs pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(enable34, OUTPUT); //Set the three pins for 3 & 4 to outputs pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); } void loop(){ setSpeedMotorOne(255); ly a call to analogWrite setSpeedMotorTwo(255); ly a call to analogWrite motorOneForward(); the other LOW) motorTwoBackward(); the other HIGH) delay(1000); brakeMotorOne(); top quicker than setting W) brakeMotorTwo(); top quicker than setting W) delay(1000); //Set the speed of motor one 255 = on 0 = off (simp on enable12 pin) //Set the speed of motor two 255 = on 0 = off (simp on enable34 pin) //Spin motor one forwards (set one bridge HIGH and //Spin motor two backwards (set one bridge LOW and //wait for a second //by leaving the bridge on the motors are braked (s the speed to zero) (sets both sides of the bridge to LO //by leaving the bridge on the motors are braked (s the speed to zero) (sets both sides of the bridge to LO //wait for a second

motorOneBackward(); //Spin motor one backwards (set one bridge LOW and the other HIGH) motorTwoForward(); //Spin motor two forwards (set one bridge HIGH and the other LOW) delay(1000); //wait for a second setSpeedMotorOne(0); //Set the speed of motor one 255 = on 0 = off (simp ly a call to analogWrite on enable12 pin) setSpeedMotorTwo(0); //Set the speed of motor two 255 = on 0 = off (simp ly a call to analogWrite on enable34 pin) delay(1000); //wait for a second } ////// Motor One Routines //////////// void motorOneForward(){ digitalWrite(in1, LOW); digitalWrite(in2, HIGH); } void motorOneBackward(){ digitalWrite(in1, HIGH); digitalWrite(in2, LOW); } void setSpeedMotorOne(int newSpeed){ analogWrite(enable12, newSpeed); } void brakeMotorOne(){ digitalWrite(in3, LOW); digitalWrite(in4, LOW); } ////// Motor Two Routines //////////// void motorTwoForward(){ digitalWrite(in3, LOW); digitalWrite(in4, HIGH); } void motorTwoBackward(){ digitalWrite(in3, HIGH); digitalWrite(in4, LOW); } void setSpeedMotorTwo(int newSpeed){ analogWrite(enable34, newSpeed); } void brakeMotorTwo(){ digitalWrite(in1, LOW); digitalWrite(in2, LOW); }

Вам также может понравиться