Don't like ads? PRO users don't see any ads ;-)
Guest

Motor Controller Arduino

By: RevoluPowered on May 9th, 2012  |  syntax: C++  |  size: 1.19 KB  |  hits: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Arduino Robot Controller and interface for motor driver.
  2.  
  3. struct motor
  4. {                  
  5.         int positive, negitive, direction;
  6.         void setDirection( const int& theDirection )
  7.         {
  8.                 if(theDirection != 0)
  9.                 {
  10.                         if( theDirection > 0 )
  11.                         {
  12.                                 digitalWrite( positive, HIGH );
  13.                                 digitalWrite( negitive, LOW );
  14.                                 direction = 1;
  15.                         }
  16.                         else
  17.                         {
  18.                                 digitalWrite( positive, LOW );
  19.                                 digitalWrite( negitive, HIGH );
  20.                                 direction = -1;
  21.                         }
  22.                 }
  23.                 else
  24.                 {
  25.                         digitalWrite( positive, LOW );
  26.                         digitalWrite( negitive, LOW );
  27.                         direction = 0;
  28.                 }
  29.         }
  30.         void reset()
  31.         {
  32.                 digitalWrite( positive, LOW );
  33.                 digitalWrite( negitive, LOW );
  34.         }
  35. }left, right;
  36.  
  37.  
  38. void setup()
  39. {
  40.        
  41.         right.positive = 5;
  42.         right.negitive = 4;
  43.        
  44.         pinMode( right.positive, OUTPUT );
  45.         pinMode( right.negitive, OUTPUT );
  46.        
  47.         left.positive = 9;
  48.         left.negitive = 8;
  49.        
  50.         pinMode( left.positive, OUTPUT );
  51.         pinMode( left.negitive, OUTPUT );
  52.        
  53.         left.reset();
  54.         right.reset();
  55. }
  56.  
  57. void loop()
  58. {
  59.         left.setDirection( 1 );
  60.         right.setDirection( 1 );
  61.         delay( 1000 );
  62.         left.setDirection( -1 );
  63.         right.setDirection ( -1 );
  64.         delay( 1000 );
  65.         left.setDirection( 0 );
  66.         right.setDirection( 0 );
  67.         delay( 1000 );
  68. }