1+ /*
2+ * Serial Bidirectional Motor Control
3+ *
4+ * Example of using L293 library to control one or more DC Birirectional Motors via the Serial line
5+ *
6+ * Created by Giuseppe Masino, 28 may 2016
7+ * Author URL http://www.facebook.com/peppe.masino1
8+ *
9+ * This example and the L293 library are released under the license
10+ * CreativeCommons ShareAlike-Attribution 4.0 International
11+ *
12+ * License info: http://creativecommons.org/licenses/by-sa/4.0/
13+ *
14+ * -----------------------------------------------------------------------------------
15+ *
16+ * Things that you need:
17+ * - Arduino\Genuino MEGA2560
18+ * (any Arduino\Genuino board can be used, the important is that the L293 pin 1 is connected to a PWM-enabled pin of Arduino)
19+ * (on the Arduino MEGA2560 all pins from 2 to 13 are PWM-enabled)
20+ * (use of pin 13 is discouraged because it can cause issues when the board resets)
21+ *
22+ * - L293
23+ * - A DC Birirectional Motor (max 5V-4mA)
24+ * - A breadboard
25+ *
26+ *
27+ * The circuit:
28+ * - Arduino pin 2 -> L293 pin 1
29+ * - Arduino pin 3 -> L293 pin 2
30+ * - Arduino pin 4 -> L293 pin 7
31+ * - Arduino GND -> L293 pin 4, 5
32+ * - Arduino 5V -> L293 pin 16, 8
33+ *
34+ * - L293 pin 3 -> a terminal of the motor
35+ * - L293 pin 6 -> the other terminal of the motor
36+ *
37+ */
38+
39+ // import the library in the sketch
40+ #include < L293.h>
41+
42+ // these are constants and won't change
43+ // give a name to the pins that you use
44+ const int speedPin = 2 ; // that is the pin that we use to control the motor's speed
45+ const int forwardPin = 3 ; // this is the pin that we use to tell the motor to go forward
46+ const int reversePin = 4 ; // this is the pin that we use to tell the motor to go reverse
47+
48+ // make a new istance of the L293 library and call it "motor"
49+ // then show what are the pins used to control speed, to tell the motor to go forward and to tell the motor to go reverse
50+ L293 motor (speedPin,forwardPin,reversePin);
51+
52+ void setup ()
53+ {
54+ // these istructions will be executed one time
55+
56+ Serial.begin (9600 ); // enable serial communication
57+ }
58+
59+ void loop ()
60+ {
61+ if (Serial.available () > 0 ) // check if there is an incoming command on the serial line
62+ {
63+ String command = Serial.readString (); // store the command in a variable
64+
65+ // select the proper action
66+ if (command == " forward" ) motor.forward (255 );
67+ else if (command == " reverse" ) motor.back (255 );
68+ else if (command == " stop" ) motor.stop ();
69+ }
70+ }
0 commit comments