h bridge dc motor control

const int EN = A0; //enable
const int MC1= 7; //motor control 1
const int MC2 = 8; //motor control 2
const int POT = 1; // pot reader
const int PWM = 5; //pwm on pin 5
 int val=0; //for storing the reading from the pot
 int velocity = 0; // for storing the deired velocity (from 0-255)

 void setup ()
 {
  pinMode(EN, OUTPUT);
  pinMode(MC1, OUTPUT);
  pinMode(MC2, OUTPUT);
  pinMode(PWM, OUTPUT);
  brake(); //initialize w/ motor stopped
 }


void loop ()
{
  val = analogRead(POT);

  // go forward
    if (val > 562)
        {
          velocity = map (val,563,1023,0,255);
          forward(velocity);
          }
   // go backward
   else if (VAL < 462)
           {
            velocity = map(val,461,0,0255);
            reverse(velocity);
            }
    // brake
    else
        {
          brake();
        }
}

// motor goes forward at given rate ( from 0-255)
void forward (int rate)
{
  digitalWrite(EN, LOW);
    digitalWrite(MC1, HIGH);
      digitalWrite(MC2, LOW);
        analogWrite(PWM, rate);
          digitalWrite(EN, HIGH);
            }

            // motor goes backward at given rate ( from 0-255)
                void backward (int rate)
                {
                  digitalWrite(EN, LOW);
                    digitalWrite(MC1, LOW);
                      digitalWrite(MC2, LOW);
                        analogWrite(PWM, 0);
                          digitalWrite(EN, HIGH);
                            }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Simple Motor Speed Control Programconst int MOTOR = 9; //Motor on Digital Pin 9const int POT = 0; //Pot on analog pin 0void setup(){   Serial.begin(9600);  pinMode (MOTOR, OUTPUT);
}void loop(){ int val = analogRead(POT); //Read one value from the POT Serial.println(val); //Print it to the serial port  val = map(val, 0, 1023, 0, 255); //scale it to servo range analogWrite(MOTOR, val);    delay(100);
}



homwork


// wired connections
#define B_IA 10 // D10 --> Motor B Input A --> MOTOR B +
#define B_IB 11 // D11 --> Motor B Input B --> MOTOR B -

// functional connections
#define MOTOR_B_PWM HG7881_B_IA // Motor B PWM Speed
#define MOTOR_B_DIR HG7881_B_IB // Motor B Direction

// the actual values for "fast" and "slow" depend on the motor
#define PWM_SLOW 50  // arbitrary slow speed PWM duty cycle
#define PWM_FAST 200 // arbitrary fast speed PWM duty cycle
#define DIR_DELAY 1000 // brief delay for abrupt motor changes

void setup()
{
  Serial.begin( 9600 );
  pinMode( MOTOR_B_DIR, OUTPUT );
  pinMode( MOTOR_B_PWM, OUTPUT );
  digitalWrite( MOTOR_B_DIR, LOW );
  digitalWrite( MOTOR_B_PWM, LOW );
}

void loop()
{
  boolean isValidInput;
  // draw a menu on the serial port
  Serial.println( "-----------------------------" );
  Serial.println( "MENU:" );
  Serial.println( "1) Fast forward" );
  Serial.println( "2) Forward" );
  Serial.println( "3) Soft stop (coast)" );
  Serial.println( "4) Reverse" );
  Serial.println( "5) Fast reverse" );
  Serial.println( "6) Hard stop (brake)" );
  Serial.println( "-----------------------------" );
  do
  {
    byte c;
    // get the next character from the serial port
    Serial.print( "?" );
    while( !Serial.available() )
      ; // LOOP...
    c = Serial.read();
    // execute the menu option based on the character recieved
    switch( c )
    {
      case '1': // 1) Fast forward
        Serial.println( "Fast forward..." );
        // always stop motors briefly before abrupt changes
        digitalWrite( MOTOR_B_DIR, LOW );
        digitalWrite( MOTOR_B_PWM, LOW );
        delay( DIR_DELAY );
        // set the motor speed and direction
        digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
        analogWrite( MOTOR_B_PWM, 255-PWM_FAST ); // PWM speed = fast
        isValidInput = true;
        break;   
     
      case '2': // 2) Forward   
        Serial.println( "Forward..." );
        // always stop motors briefly before abrupt changes
        digitalWrite( MOTOR_B_DIR, LOW );
        digitalWrite( MOTOR_B_PWM, LOW );
        delay( DIR_DELAY );
        // set the motor speed and direction
        digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
        analogWrite( MOTOR_B_PWM, 255-PWM_SLOW ); // PWM speed = slow
        isValidInput = true;
        break;   
     
      case '3': // 3) Soft stop (preferred)
        Serial.println( "Soft stop (coast)..." );
        digitalWrite( MOTOR_B_DIR, LOW );
        digitalWrite( MOTOR_B_PWM, LOW );
        isValidInput = true;
        break;   

      case '4': // 4) Reverse
        Serial.println( "Fast forward..." );
        // always stop motors briefly before abrupt changes
        digitalWrite( MOTOR_B_DIR, LOW );
        digitalWrite( MOTOR_B_PWM, LOW );
        delay( DIR_DELAY );
        // set the motor speed and direction
        digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse
        analogWrite( MOTOR_B_PWM, PWM_SLOW ); // PWM speed = slow
        isValidInput = true;
        break;   
     
      case '5': // 5) Fast reverse
        Serial.println( "Fast forward..." );
        // always stop motors briefly before abrupt changes
        digitalWrite( MOTOR_B_DIR, LOW );
        digitalWrite( MOTOR_B_PWM, LOW );
        delay( DIR_DELAY );
        // set the motor speed and direction
        digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse   
        analogWrite( MOTOR_B_PWM, PWM_FAST ); // PWM speed = fast
        isValidInput = true;
        break;
     
      case '6': // 6) Hard stop (use with caution)
        Serial.println( "Hard stop (brake)..." );
        digitalWrite( MOTOR_B_DIR, HIGH );
        digitalWrite( MOTOR_B_PWM, HIGH );
        isValidInput = true;
        break;   
     
      default:
        // wrong character! display the menu again!
        isValidInput = false;
        break;
    }
  } while( isValidInput == true );

  // repeat the main loop and redraw the menu...
}

Add caption


Comments

Popular posts from this blog

pointers code no lb

real time & EEPROM lab

interrup and sound lab