
Motor Controller Arduino
By:
RevoluPowered on
May 9th, 2012 | syntax:
C++ | size: 1.19 KB | hits: 41 | expires: Never
// Arduino Robot Controller and interface for motor driver.
struct motor
{
int positive, negitive, direction;
void setDirection( const int& theDirection )
{
if(theDirection != 0)
{
if( theDirection > 0 )
{
digitalWrite( positive, HIGH );
digitalWrite( negitive, LOW );
direction = 1;
}
else
{
digitalWrite( positive, LOW );
digitalWrite( negitive, HIGH );
direction = -1;
}
}
else
{
digitalWrite( positive, LOW );
digitalWrite( negitive, LOW );
direction = 0;
}
}
void reset()
{
digitalWrite( positive, LOW );
digitalWrite( negitive, LOW );
}
}left, right;
void setup()
{
right.positive = 5;
right.negitive = 4;
pinMode( right.positive, OUTPUT );
pinMode( right.negitive, OUTPUT );
left.positive = 9;
left.negitive = 8;
pinMode( left.positive, OUTPUT );
pinMode( left.negitive, OUTPUT );
left.reset();
right.reset();
}
void loop()
{
left.setDirection( 1 );
right.setDirection( 1 );
delay( 1000 );
left.setDirection( -1 );
right.setDirection ( -1 );
delay( 1000 );
left.setDirection( 0 );
right.setDirection( 0 );
delay( 1000 );
}