From ColdSphinX, 4 Years ago, written in C++.
This paste is a reply to Arduino Nano L293D from ColdSphinX - view diff
Embed
  1. #define DEBUG 0
  2.  
  3. //L293D
  4. //Motor A (left)
  5. #define MOTOR_PIN1 9 // Pin 2 of L293
  6. #define MOTOR_PIN2 10 // Pin 7 of L293
  7. //Motor B (right)
  8. #define MOTOR_PIN3 11 // Pin  10 of L293
  9. #define MOTOR_PIN4 12 // Pin  15 of L293
  10.  
  11. //IR Remote
  12. #include <IRremote.h>
  13. #define IR_PIN 8
  14. IRrecv irrecv(IR_PIN);
  15. decode_results ir_results;
  16.  
  17. //Ultrasonic sensor
  18. #define US_TRIG_PIN 7
  19. #define US_ECHO_PIN 6
  20. long duration;
  21. int distance;
  22.  
  23. enum movements{
  24.   UNDEFINED,
  25.   STOP,
  26.   FORWARD,
  27.   BACKWARD,
  28.   LEFT_ROTATE,
  29.   RIGHT_ROTATE,
  30.   LEFT_CIRCLE,
  31.   RIGHT_CIRCLE
  32. };
  33.  
  34. enum movements current_mode;
  35. enum movements next_mode;
  36. boolean evading = false;
  37. boolean moving = false;
  38.  
  39. #define TIME_WAIT 3600
  40.  
  41. void setup() {
  42.   randomSeed(analogRead(0));
  43.   //Set pins as outputs
  44.   pinMode(MOTOR_PIN1, OUTPUT);
  45.   pinMode(MOTOR_PIN2, OUTPUT);
  46.   pinMode(MOTOR_PIN3, OUTPUT);
  47.   pinMode(MOTOR_PIN4, OUTPUT);
  48.   stop();
  49.   current_mode = STOP;
  50.   next_mode = STOP;
  51.   pinMode(US_TRIG_PIN, OUTPUT);
  52.   digitalWrite(US_TRIG_PIN, LOW);
  53.   pinMode(US_ECHO_PIN, INPUT);
  54. #if DEBUG > 0
  55.   Serial.begin(9600);
  56.   Serial.println("IR Receiver Raw Data + Button Decode Test");
  57. #endif
  58.   irrecv.enableIRIn(); // Start the receiver
  59. }
  60.  
  61. void motor_a_stop() {
  62.   digitalWrite(MOTOR_PIN1, LOW);
  63.   digitalWrite(MOTOR_PIN2, LOW);
  64. }
  65.  
  66. void motor_a_forward() {
  67.   digitalWrite(MOTOR_PIN1, LOW);
  68.   digitalWrite(MOTOR_PIN2, HIGH);
  69. }
  70.  
  71. void motor_a_backward() {
  72.   digitalWrite(MOTOR_PIN1, HIGH);
  73.   digitalWrite(MOTOR_PIN2, LOW);
  74. }
  75.  
  76. void motor_b_stop() {
  77.   digitalWrite(MOTOR_PIN3, LOW);
  78.   digitalWrite(MOTOR_PIN4, LOW);
  79. }
  80.  
  81. void motor_b_forward() {
  82.   digitalWrite(MOTOR_PIN3, LOW);
  83.   digitalWrite(MOTOR_PIN4, HIGH);
  84. }
  85.  
  86. void motor_b_backward() {
  87.   digitalWrite(MOTOR_PIN3, HIGH);
  88.   digitalWrite(MOTOR_PIN4, LOW);
  89. }
  90.  
  91. void left_stop() {
  92.   motor_a_stop();
  93. }
  94.  
  95. void right_stop() {
  96.   motor_b_stop();
  97. }
  98.  
  99. void stop() {
  100.   left_stop();
  101.   right_stop();
  102.   moving = false;
  103. }
  104.  
  105. void left_forward() {
  106.   motor_a_backward();
  107.   moving = true;
  108. }
  109.  
  110. void left_backward() {
  111.   motor_a_forward();
  112.   moving = true;
  113. }
  114.  
  115. void right_forward() {
  116.   motor_b_forward();
  117.   moving = true;
  118. }
  119.  
  120. void right_backward() {
  121.   motor_b_backward();
  122.   moving = true;
  123. }
  124.  
  125. void forward() {
  126.   left_forward();
  127.   right_forward();
  128. }
  129.  
  130. void backward() {
  131.   left_backward();
  132.   right_backward();
  133. }
  134.  
  135. void left_rotate() {
  136.   left_backward();
  137.   right_forward();
  138. }
  139.  
  140. void right_rotate() {
  141.   left_forward();
  142.   right_backward();
  143. }
  144.  
  145. void left_circle() {
  146.   left_stop();
  147.   right_forward();
  148. }
  149.  
  150. void right_circle() {
  151.   right_stop();
  152.   left_forward();
  153. }
  154.  
  155. void wait() {
  156.   delay(TIME_WAIT);
  157.   stop();
  158. }
  159.  
  160. void test_movements(){
  161.   forward();
  162.   wait();
  163.   backward();
  164.   wait();
  165.   left_rotate();
  166.   wait();
  167.   right_rotate();
  168.   wait();
  169.   left_circle();
  170.   wait();
  171.   right_circle();
  172.   wait();
  173.   stop();
  174.   wait();
  175. }
  176.  
  177. void ir_decode(){
  178.   switch(ir_results.value){
  179.     /*
  180.      * 0xFF38C7 => 5 (stop)         => STOP
  181.      * 0xFF18E7 => 2 (forward)      => FORWARD
  182.      * 0xFF4AB5 => 8 (backward)     => BACKWARD
  183.      * 0xFF10EF => 4 (left rotate)  => LEFT_ROTATE
  184.      * 0xFF5AA5 => 6 (right rotate) => RIGHT_ROTATE
  185.      * 0xFF7A85 => 3 (right circle) => LEFT_CIRCLE
  186.      * 0xFF30CF => 1 (left circle)  => RIGHT_CIRCLE
  187.      */
  188.     case 0xFF38C7:
  189. #if DEBUG > 0
  190.       Serial.println("Stop (5)");
  191. #endif
  192.       next_mode = STOP;
  193.       break;
  194.  
  195.     case 0xFF18E7:
  196. #if DEBUG > 0
  197.       Serial.println("Forward (2)");
  198. #endif
  199.       next_mode = FORWARD;
  200.       break;
  201.  
  202.     case 0xFF4AB5:
  203. #if DEBUG > 0
  204.       Serial.println("Backward (8)");
  205. #endif
  206.       next_mode = BACKWARD;
  207.       break;
  208.  
  209.     case 0xFF10EF:
  210. #if DEBUG > 0
  211.       Serial.println("Left rotate (4)");
  212. #endif
  213.       next_mode = LEFT_ROTATE;
  214.       break;
  215.  
  216.     case 0xFF5AA5:
  217. #if DEBUG > 0
  218.       Serial.println("Right rotate (6)");
  219. #endif
  220.       next_mode = RIGHT_ROTATE;
  221.       break;
  222.  
  223.     case 0xFF7A85:
  224. #if DEBUG > 0
  225.       Serial.println("Left circle (3)");
  226. #endif
  227.       next_mode = LEFT_CIRCLE;
  228.       break;
  229.  
  230.     case 0xFF30CF:
  231. #if DEBUG > 0
  232.       Serial.println("Right circle (1)");
  233. #endif
  234.       next_mode = RIGHT_CIRCLE;
  235.       break;
  236.  
  237. #if DEBUG > 0
  238.     default:
  239.       Serial.print("Unknown button: ");
  240.       Serial.println(ir_results.value, HEX);
  241. #endif
  242.   }
  243. }
  244.  
  245. void sonic_check(){
  246.   //reset
  247.   digitalWrite(US_TRIG_PIN, LOW);
  248.   delayMicroseconds(2);
  249.   //Set the trigger pin on HIGH state for 10 micro seconds
  250.   digitalWrite(US_TRIG_PIN, HIGH);
  251.   delayMicroseconds(10);
  252.   digitalWrite(US_TRIG_PIN, LOW);
  253.   //Read echo
  254.   duration = pulseIn(US_ECHO_PIN, HIGH);
  255.   //Calculating distance
  256.   distance = duration * 0.034 / 2;
  257. #if DEBUG > 1
  258.   Serial.print("Distance: ");
  259.   Serial.println(distance);
  260. #endif
  261.   if (distance <= 5){
  262.     switch(current_mode){
  263.       case LEFT_ROTATE:
  264.         next_mode = LEFT_ROTATE;
  265.         break;
  266.       case RIGHT_ROTATE:
  267.         next_mode = RIGHT_ROTATE;
  268.         break;
  269.       case LEFT_CIRCLE:
  270.         next_mode = LEFT_ROTATE;
  271.         break;
  272.       case RIGHT_CIRCLE:
  273.         next_mode = RIGHT_ROTATE;
  274.         break;
  275.       default:
  276.         if (random(10) >= 5){
  277.           next_mode = LEFT_ROTATE;
  278.         } else {
  279.           next_mode = RIGHT_ROTATE;
  280.         }
  281.     }
  282.     evading = true;
  283.     //mode_check();
  284.   } else if (evading){
  285.     next_mode = FORWARD;
  286.     evading = false;
  287.   }
  288. }
  289.  
  290. void mode_check(){
  291. #if DEBUG > 2
  292.   Serial.println("Mode: ");
  293.   Serial.print("# Current: ");
  294.   Serial.println(current_mode);
  295.   Serial.print("# Next: ");
  296.   Serial.println(next_mode);
  297. #endif
  298.   if (next_mode != current_mode) {
  299.     switch(next_mode){
  300.       case STOP:
  301.         stop();
  302.         current_mode = STOP;
  303.         break;
  304.       case FORWARD:
  305.         forward();
  306.         current_mode = FORWARD;
  307.         break;
  308.       case BACKWARD:
  309.         backward();
  310.         current_mode = BACKWARD;
  311.         break;
  312.       case LEFT_ROTATE:
  313.         left_rotate();
  314.         current_mode = LEFT_ROTATE;
  315.         break;
  316.       case RIGHT_ROTATE:
  317.         right_rotate();
  318.         current_mode = RIGHT_ROTATE;
  319.         break;
  320.       case LEFT_CIRCLE:
  321.         left_circle();
  322.         current_mode = LEFT_CIRCLE;
  323.         break;
  324.       case RIGHT_CIRCLE:
  325.         right_circle();
  326.         current_mode = RIGHT_CIRCLE;
  327.         break;
  328.     }
  329.   } else {
  330.     delay(500);
  331.   }
  332. }
  333.  
  334. void loop() {
  335.   if (irrecv.decode(&ir_results)){
  336.     ir_decode();
  337.     irrecv.resume(); // receive the next value
  338.   }
  339.   if (moving){
  340.     sonic_check();
  341.   }
  342.   mode_check();
  343. #ifdef DEBUG
  344.     //test_movements()
  345. #endif
  346. }

Replies to Arduino Nano L293D rss

Title Name Language When
Arduino Nano L293D IR Ultrasonic ColdSphinX cpp 4 Years ago.