Connect with us

Motor Reverses Direction Before Code Arduino

Discussion in 'Microcontrollers, Programming and IoT' started by Jaderman, Apr 26, 2013.

Scroll to continue with content
  1. Jaderman

    Jaderman

    32
    0
    Dec 12, 2009
    Hey all, I'm having a problem with this code. When I turn the robot on, the right motor (motor2) spins backwards for about 2 seconds and then it all runs normal. Why in the world is that happening? The code is a work in progress so its a mess. Don't try too hard to understand it all. Here's the code:


    const int encoder1A = 2; // Encoder 1
    const int motor1_1Pin = 4; // H-bridge 1 leg 1 (1Y, 2Y)
    const int motor1_2Pin = 8; // H-bridge 1 leg 2 (3Y, 4Y)
    const int enablePin1 = 6; // H-bridge 1 enable
    //const int motor2power = 11;

    const int motor2_1Pin = 12; // H-bridge 2 leg 1 (1Y, 1Y)
    const int motor2_2Pin = 13; // H-bridge 2 leg 2 (3Y, 4Y)
    const int enablePin2 = 9; // H-bridge 2 enable


    volatile unsigned int encoderCount = 0;


    void setup() {
    pinMode(encoder1A, INPUT);
    pinMode(motor1_1Pin, OUTPUT);
    pinMode(motor1_2Pin, OUTPUT);
    pinMode(enablePin1, OUTPUT);
    digitalWrite(enablePin1, LOW);

    pinMode(motor2_1Pin, OUTPUT);
    pinMode(motor2_2Pin, OUTPUT);
    pinMode(enablePin2, OUTPUT);
    digitalWrite(enablePin2, LOW);


    attachInterrupt(0, readEncoder, RISING);

    // Serial.begin (9600);
    // Serial.println("Begin Code");
    }


    void loop() {

    digitalWrite(enablePin1, HIGH);
    digitalWrite(enablePin2, HIGH);

    digitalWrite(motor1_1Pin, HIGH);
    digitalWrite(motor1_2Pin, LOW);
    digitalWrite(motor2_1Pin, LOW);
    digitalWrite(motor2_2Pin, HIGH);

    if((encoderCount == 312) || (encoderCount == 624) || (encoderCount == 936) || (encoderCount == 1248) || (encoderCount == 1560) || (encoderCount == 1872) || (encoderCount == 2184) || (encoderCount == 2496) || (encoderCount == 2808) || (encoderCount == 3120) || (encoderCount == 3432) || (encoderCount == 3744) || (encoderCount == 4056) || (encoderCount == 4368) || (encoderCount == 4680) || (encoderCount == 4992)){
    digitalWrite(motor2_2Pin, LOW);
    digitalWrite(motor2_2Pin, LOW);
    delay(225);
    }

    if (encoderCount == 1400) {
    digitalWrite(motor1_1Pin, LOW);
    digitalWrite(motor1_2Pin, LOW);
    delay(2730);
    interrupts();
    }

    if (encoderCount == 2250) {
    digitalWrite(motor1_1Pin, LOW);
    digitalWrite(motor1_2Pin, LOW);
    delay(2550);
    interrupts();
    }

    if (encoderCount == 5000) {
    digitalWrite(motor1_1Pin, LOW);
    digitalWrite(motor1_2Pin, LOW);
    delay(2500);
    interrupts();
    }

    if (encoderCount == 5720) {

    digitalWrite(motor1_1Pin, HIGH);
    digitalWrite(motor1_2Pin, LOW);
    digitalWrite(motor2_1Pin, LOW);
    digitalWrite(motor2_2Pin, LOW);
    delay(2000);

    digitalWrite(motor1_1Pin, LOW);
    digitalWrite(motor1_2Pin, LOW);
    digitalWrite(motor2_1Pin, LOW);
    digitalWrite(motor2_2Pin, LOW);
    delay(1000);

    digitalWrite(motor1_1Pin, LOW);
    digitalWrite(motor1_2Pin, HIGH);
    digitalWrite(motor2_1Pin, HIGH);
    digitalWrite(motor2_2Pin, LOW);
    delay(2000);

    digitalWrite(motor1_1Pin, LOW);
    digitalWrite(motor1_2Pin, LOW);
    digitalWrite(motor2_1Pin, LOW);
    digitalWrite(motor2_2Pin, LOW);
    delay(60000);
    }
    }


    void readEncoder() {

    if (digitalRead(encoder1A) == HIGH){
    encoderCount++;

    if ((encoderCount == 1360) || (encoderCount == 2266) || (encoderCount == 5000) || (encoderCount == 5720)) {
    noInterrupts();
    }
    // Serial.println(encoderCount);
    }
    }
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    9,511
    1,960
    Nov 17, 2011
    I'm not familiar with the ardujino and coding it, but a few things are conspicuous
    Code:
    digitalWrite(motor1_1Pin, HIGH); 
    digitalWrite(motor1_2Pin, LOW);
    digitalWrite(motor2_1Pin, LOW); 
    digitalWrite(motor2_2Pin, HIGH);
    
    Motor 1a and 2 are set up inversely.


    Code:
    void readEncoder() {
    
    if (digitalRead(encoder1A) == HIGH){
    encoderCount++;
    
    if ((encoderCount == 1360) || (encoderCount == 2266) || (encoderCount == 5000) || (encoderCount == 5720)) {
    noInterrupts();
    } 
    
    encoderCount is constantly incremented, but never reset. What happens if encoderCount reaches the max. possible value that can be represented by an unsigned int on the arduino? Will the counter wrap around to 0?


    Another idea that has nothing to do with your original question:
    Code:
    if((encoderCount == 312) || (encoderCount == 624) || (encoderCount == 936) || (encoderCount == 1248) || (encoderCount == 1560) || (encoderCount == 1872) || (encoderCount == 2184) || (encoderCount == 2496) || (encoderCount == 2808) || (encoderCount == 3120) || (encoderCount == 3432) || (encoderCount == 3744) || (encoderCount == 4056) || (encoderCount == 4368) || (encoderCount == 4680) || (encoderCount == 4992)){
    could be replaced by
    Code:
    if ((encoderCound mod 312) == 0)
    I used mod to describe the modulus (rest after division) because I don't know if there is an equivalent in the arduino's programming language and how that would be stated. The idea is that if encoderCount is evenly divisible by 312 (without rest) this is equivalent to encoderCount being 312, 624,...
     
    Last edited: Apr 26, 2013
  3. GreenGiant

    GreenGiant

    830
    6
    Feb 9, 2012
    simple answer to the main problem, pin 13 has an LED on it, that is on for a lot of built in start up code, I believe it is the LED labelled "L", you may want to move the wire that is on this pin to a different pin then remap it in the software, this should fix the issue.

    This explains why it runs backwards, since Im guessing that the 2_2 means it is the return side of the motor.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-