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

Replies to Arduino Nano L293D rss

Title Name Language When
Arduino Nano L293D ColdSphinX cpp 3 Years ago.