Pages: [1]   Go Down
Author Topic: Reversing a dc motor  (Read 1113 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been having a problem with reversing my dc motor. The motor is just running in the same direction and my program is not charging the direction as dictated by the sketch.
Someone to help me with the code to run the motor forward for some time the reverse for sometime and the stop the motor!

This is the code am using;
Code:
#define motorpin1 2
#define motorpin2 3
#define motorpin3 4
#define motorpin4 5
#define enablepin 8

void setup() {
  pinMode(motorpin1, OUTPUT);
  pinMode(motorpin2, OUTPUT);
  pinMode(motorpin3, OUTPUT);
  pinMode(motorpin4, OUTPUT);

  // set speedPin high so that motor can turn on:
  digitalWrite(enablepin, HIGH);
}

void loop() {
    digitalWrite(motorpin1, LOW);  
    digitalWrite(motorpin2, HIGH);  
    digitalWrite(motorpin3, LOW);  
    digitalWrite(motorpin4, HIGH);
    delay(1500);
    digitalWrite(motorpin1, LOW);  
    digitalWrite(motorpin2, LOW);  
    digitalWrite(motorpin3, LOW);  
    digitalWrite(motorpin4, LOW);
    delay(1500);
    digitalWrite(motorpin1,HIGH);  
    digitalWrite(motorpin2,LOW);  
    digitalWrite(motorpin3,HIGH);  
    digitalWrite(motorpin4,LOW);
    
}
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why does the DC motor have 4 pins, or age you running two? Are you using a motor driver?
« Last Edit: March 28, 2009, 01:10:34 pm by jeffonfire » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using two motors with a Pololu MD03A H-bridge motor driver.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think what is happening is that you're reversing the motors at the end of the loop and then the loop repeats and immediately spins the motors forward again.

Try putting  a delay(1500); just before the closing }

Code:
   digitalWrite(motorpin4,LOW);
    delay(1500); //<-- insert this line
}

HTH

Ver
Logged

Pages: [1]   Go Up
Jump to: