From ColdSphinX, 7 Years ago, written in C++.
Embed
  1. //L293D
  2. //Motor A
  3. const int motorPin1  = 5;  // Pin 14 of L293
  4. const int motorPin2  = 6;  // Pin 10 of L293
  5. //Motor B
  6. const int motorPin3  = 10; // Pin  7 of L293
  7. const int motorPin4  = 9;  // Pin  2 of L293
  8.  
  9. #define TIME_WAIT 15000
  10.  
  11. void setup() {
  12.   //Set pins as outputs
  13.   pinMode(motorPin1, OUTPUT);
  14.   pinMode(motorPin2, OUTPUT);
  15.   pinMode(motorPin3, OUTPUT);
  16.   pinMode(motorPin4, OUTPUT);
  17. }
  18.  
  19. void left_stop() {
  20.   digitalWrite(motorPin1, LOW);
  21.   digitalWrite(motorPin2, LOW);
  22. }
  23.  
  24. void right_stop() {
  25.   digitalWrite(motorPin1, LOW);
  26.   digitalWrite(motorPin2, LOW);
  27. }
  28.  
  29.  
  30. void stop() {
  31.   left_stop();
  32.   right_stop();
  33. }
  34.  
  35. void left_forward() {
  36.   digitalWrite(motorPin1, LOW);
  37.   digitalWrite(motorPin2, HIGH);
  38. }
  39.  
  40. void left_backward() {
  41.   digitalWrite(motorPin1, HIGH);
  42.   digitalWrite(motorPin2, LOW);
  43. }
  44.  
  45. void right_forward() {
  46.   digitalWrite(motorPin3, LOW);
  47.   digitalWrite(motorPin4, HIGH);
  48. }
  49.  
  50. void right_backward() {
  51.   digitalWrite(motorPin3, HIGH);
  52.   digitalWrite(motorPin4, LOW);
  53. }
  54.  
  55. void forward() {
  56.   left_forward();
  57.   right_forward();
  58. }
  59.  
  60. void backward() {
  61.   left_backward();
  62.   right_backward();
  63. }
  64.  
  65. void left_rotate() {
  66.   left_backward();
  67.   right_forward();
  68. }
  69.  
  70. void right_rotate() {
  71.   left_forward();
  72.   right_backward();
  73. }
  74.  
  75. void left_circle() {
  76.   left_stop();
  77.   right_forward();
  78. }
  79.  
  80. void right_circle() {
  81.   right_stop();
  82.   left_forward();
  83. }
  84.  
  85. void wait() {
  86.   delay(TIME_WAIT);
  87.   stop();
  88. }
  89.  
  90. void loop() {
  91.   forward();
  92.   wait();
  93.   backward();
  94.   wait();
  95.   left_rotate();
  96.   wait();
  97.   right_rotate();
  98.   wait();
  99.   left_circle();
  100.   wait();
  101.   right_circle();
  102.   wait();
  103.   stop();
  104.   delay(2000);
  105. }

Replies to Arduino Nano L293D rss

Title Name Language When
Re: Arduino Nano L293D ColdSphinX cpp 7 Years ago.