Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.91 KB | None | 0 0
  1. /*
  2.   HG7881_Motor_Driver_Example - Arduino sketch
  3.    
  4.   This example shows how to drive a motor with using HG7881 (L9110) Dual
  5.   Channel Motor Driver Module.  For simplicity, this example shows how to
  6.   drive a single motor.  Both channels work the same way.
  7.    
  8.   This example is meant to illustrate how to operate the motor driver
  9.   and is not intended to be elegant, efficient or useful.
  10.    
  11.   Connections:
  12.    
  13.     Arduino digital output D10 to motor driver input B-IA.
  14.     Arduino digital output D11 to motor driver input B-IB.
  15.     Motor driver VCC to operating voltage 5V.
  16.     Motor driver GND to common ground.
  17.     Motor driver MOTOR B screw terminals to a small motor.
  18.      
  19.   Related Banana Robotics items:
  20.    
  21.     BR010038 HG7881 (L9110) Dual Channel Motor Driver Module
  22.     https://www.BananaRobotics.com/shop/HG7881-(L9110)-Dual-Channel-Motor-Driver-Module
  23.  
  24.   https://www.BananaRobotics.com
  25. */
  26.  
  27. // wired connections
  28. #define HG7881_B_IA 10 // D10 --> Motor B Input A --> MOTOR B +
  29. #define HG7881_B_IB 11 // D11 --> Motor B Input B --> MOTOR B -
  30.  
  31. // functional connections
  32. #define MOTOR_B_PWM HG7881_B_IA // Motor B PWM Speed
  33. #define MOTOR_B_DIR HG7881_B_IB // Motor B Direction
  34.  
  35. // the actual values for "fast" and "slow" depend on the motor
  36. #define PWM_SLOW 50  // arbitrary slow speed PWM duty cycle
  37. #define PWM_FAST 200 // arbitrary fast speed PWM duty cycle
  38. #define DIR_DELAY 1000 // brief delay for abrupt motor changes
  39.  
  40. boolean isValidInput;
  41.  
  42. void setup()
  43. {
  44.   Serial.begin( 9600 );
  45.   pinMode( MOTOR_B_DIR, OUTPUT );
  46.   pinMode( MOTOR_B_PWM, OUTPUT );
  47.   digitalWrite( MOTOR_B_DIR, LOW );
  48.   digitalWrite( MOTOR_B_PWM, LOW );
  49. }
  50.  
  51. void loop()
  52. {
  53.   // draw a menu on the serial port
  54.   Serial.println( "-----------------------------" );
  55.   Serial.println( "MENU:" );
  56.   Serial.println( "1) Fast forward" );
  57.   Serial.println( "2) Forward" );
  58.   Serial.println( "3) Soft stop (coast)" );
  59.   Serial.println( "4) Reverse" );
  60.   Serial.println( "5) Fast reverse" );
  61.   Serial.println( "6) Hard stop (brake)" );
  62.   Serial.println( "-----------------------------" );
  63.   do
  64.   {
  65.     byte c;
  66.     // get the next character from the serial port
  67.     Serial.print( "?" );
  68.     while( !Serial.available() )
  69.       ; // LOOP...
  70.     c = Serial.read();
  71.     // execute the menu option based on the character recieved
  72.     commands(c);  
  73.   }while( isValidInput == true );
  74.   // repeat the main loop and redraw the menu...
  75. }
  76.  
  77. void commands(byte c)
  78. {
  79.   switch( c )
  80.   {
  81.     case '1': // 1) Fast forward
  82.       Serial.println( "Fast forward..." );
  83.       // always stop motors briefly before abrupt changes
  84.       digitalWrite( MOTOR_B_DIR, LOW );
  85.       digitalWrite( MOTOR_B_PWM, LOW );
  86.       delay( DIR_DELAY );
  87.       // set the motor speed and direction
  88.       digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
  89.       analogWrite( MOTOR_B_PWM, 255-PWM_FAST ); // PWM speed = fast
  90.       isValidInput = true;
  91.       break;      
  92.        
  93.     case '2': // 2) Forward      
  94.       Serial.println( "Forward..." );
  95.       // always stop motors briefly before abrupt changes
  96.       digitalWrite( MOTOR_B_DIR, LOW );
  97.       digitalWrite( MOTOR_B_PWM, LOW );
  98.       delay( DIR_DELAY );
  99.       // set the motor speed and direction
  100.       digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
  101.       analogWrite( MOTOR_B_PWM, 255-PWM_SLOW ); // PWM speed = slow
  102.       isValidInput = true;
  103.       break;      
  104.        
  105.     case '3': // 3) Soft stop (preferred)
  106.       Serial.println( "Soft stop (coast)..." );
  107.       digitalWrite( MOTOR_B_DIR, LOW );
  108.       digitalWrite( MOTOR_B_PWM, LOW );
  109.       isValidInput = true;
  110.       break;      
  111.  
  112.     case '4': // 4) Reverse
  113.       Serial.println( "Fast forward..." );
  114.       // always stop motors briefly before abrupt changes
  115.       digitalWrite( MOTOR_B_DIR, LOW );
  116.       digitalWrite( MOTOR_B_PWM, LOW );
  117.       delay( DIR_DELAY );
  118.       // set the motor speed and direction
  119.       digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse
  120.       analogWrite( MOTOR_B_PWM, PWM_SLOW ); // PWM speed = slow
  121.       isValidInput = true;
  122.       break;      
  123.        
  124.     case '5': // 5) Fast reverse
  125.       Serial.println( "Fast forward..." );
  126.       // always stop motors briefly before abrupt changes
  127.       digitalWrite( MOTOR_B_DIR, LOW );
  128.       digitalWrite( MOTOR_B_PWM, LOW );
  129.       delay( DIR_DELAY );
  130.       // set the motor speed and direction
  131.       digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse      
  132.       analogWrite( MOTOR_B_PWM, PWM_FAST ); // PWM speed = fast
  133.       isValidInput = true;
  134.       break;
  135.        
  136.     case '6': // 6) Hard stop (use with caution)
  137.       Serial.println( "Hard stop (brake)..." );
  138.       digitalWrite( MOTOR_B_DIR, HIGH );
  139.       digitalWrite( MOTOR_B_PWM, HIGH );
  140.       isValidInput = true;
  141.       break;      
  142.        
  143.     default:
  144.       // wrong character! display the menu again!
  145.       isValidInput = false;
  146.       break;
  147.   }
  148. }
  149. /*EOF*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement