Dagu 5rover arduino code

Hello i Got the dagu 5 rover and the 4wd motor controller, attached it to an arduino,  i wrote code to use it but it doesn't seem to be functional

 

volatile int rotaryCount = 0;

 

#define INTERRUPT0 0  // that is, pin 2

#define INTERRUPT1 1//pin 3

 

#define DIRECTIONA 4

#define MOTORA 5

 

#define DIRECTIONB 7

#define MOTORB 6

 

int QEM [16] = {0,-1,1,2,1,0,2,-1,-1,2,0,1,2,1,-1,0};               // Quadrature Encoder Matrix

 

 

const int Input0A = 8; 

const int Input0B = 9;

volatile long Position0 = 0;

volatile unsigned char Old0, New0;

 

const int Input1A = 10; 

const int Input1B = 11;

volatile long Position1 =0;

volatile unsigned char Old1, New1;

 

void ISR0()

{

Old0 = New0;

New0 = digitalRead (Input0A) * 2 + digitalRead (Input0B);           // Convert binary input to decimal value

Position0 += QEM [Old0 * 4 + New0];

}

void ISR1(){

Old1 = New1;

New1 = digitalRead (Input1A) * 2 + digitalRead (Input1B);           // Convert binary input to decimal value

Position1 += QEM [Old1 * 4 + New1];

}

 

void setup ()

{

  Serial.begin(9600);

pinMode(Input0A, INPUT);

pinMode(Input0B, INPUT);

pinMode(Input1A, INPUT);

pinMode(Input1B, INPUT);

attachInterrupt (INTERRUPT0, ISR0, CHANGE);   // interrupt 0 is pin 2, interrupt 1 is pin 3

attachInterrupt (INTERRUPT1, ISR1, CHANGE); 

pinMode (MOTORA, OUTPUT);

pinMode (DIRECTIONA, OUTPUT);

pinMode (MOTORB, OUTPUT);

pinMode (DIRECTIONB, OUTPUT);

Serial.println("setup DOne");

 

}  // end of setup

 

int robotDirection;

unsigned long start;

int time_to_go;

int message;

void loop ()

{

        int pos0=Position0;

int pos1=Position1;

        //Serial.println("loop");

if(Serial.available() >0)

{

message =Serial.read();

//Serial.println("new");

if(message=='f'){

robotDirection=0;

}

else if (message =='b'){

robotDirection=2;

}

else if (message =='l'){

robotDirection=1;

}

else if (message== 'r'){

robotDirection=3;

}

 

analogWrite (MOTORA, 200);

analogWrite (MOTORB, 200);

                // Serial.println("move");

switch (robotDirection)

{

case 0:

                               // Serial.println("forward");

//forward

digitalWrite (DIRECTIONA, 1); 

digitalWrite (DIRECTIONB, 1); 

break;

 

case 1: 

// turn left

digitalWrite (DIRECTIONA, 1); 

digitalWrite (DIRECTIONB, 0); 

break;

 

case 2:

//backward

digitalWrite (DIRECTIONA, 0); 

digitalWrite (DIRECTIONB, 0); 

break;

 

case 3: 

//move right

digitalWrite (DIRECTIONA, 0); 

digitalWrite (DIRECTIONB, 1); 

break;

 

} // end of switch

 

 while(true){

          /*if(Serial.available() >0){

            message =Serial.read();

            if(message=='s'){

              break;

            }

          }*/

 

          if(Position0>pos0+5000 || Position1>pos1+5000 || Position0>pos0-5000 || Position1>pos1-5000 )

            break;

 }

       // Serial.println("moveDone");

        }

 

analogWrite (MOTORA, 0);

analogWrite (MOTORB, 0);

 

}  // end of loop

 

What was the last thing you

What was the last thing you added to your code before it stopped working?

the wheels only moves in

the wheels only moves in case of forward and left, and only one motor moves, first electronics project ever, but the wiring is correct …

My suggestion would be to add some Serial.println calls to your

code. I would specifically look at your pos0, pos1 vars as well as maybe adding print calls to your movement calls.

I am surprised you didn’t use a switch/case on your raw serial data.

#define INTERRUPT0 0  // that is, pin 2
#define INTERRUPT1 1//pin 3
#define DIRECTIONA 4
#define MOTORA 5
#define DIRECTIONB 7
#define MOTORB 6

int QEM [16] = {0,-1,1,2,1,0,2,-1,-1,2,0,1,2,1,-1,0}; // Quadrature Encoder Matrix

const int Input0A = 8;
const int Input0B = 9;
volatile long Position0 = 0;
volatile unsigned char Old0, New0;

const int Input1A = 10;
const int Input1B = 11;
volatile long Position1 =0;
volatile unsigned char Old1, New1;

int message;

void ISR0() {
  Old0 = New0;
  New0 = digitalRead (Input0A) * 2 + digitalRead (Input0B); // Convert binary input to decimal value
  Position0 += QEM [Old0 * 4 + New0];
}

void ISR1() {
  Old1 = New1;
  New1 = digitalRead (Input1A) * 2 + digitalRead (Input1B); // Convert binary input to decimal value
  Position1 += QEM [Old1 * 4 + New1];
}

void setup () {
  Serial.begin(9600);
  pinMode(Input0A, INPUT);
  pinMode(Input0B, INPUT);
  pinMode(Input1A, INPUT);
  pinMode(Input1B, INPUT);
  attachInterrupt (INTERRUPT0, ISR0, CHANGE); // interrupt 0 is pin 2, interrupt 1 is pin 3
  attachInterrupt (INTERRUPT1, ISR1, CHANGE);
  pinMode (MOTORA, OUTPUT);
  pinMode (DIRECTIONA, OUTPUT);
  pinMode (MOTORB, OUTPUT);
  pinMode (DIRECTIONB, OUTPUT);
  Serial.println(“setup DOne”);
}  // end of setup

void loop () {
  int pos0=Position0;
  int pos1=Position1;
  if(Serial.available() >0) {
    message =Serial.read();
  switch(message) {
    case ‘b’:
      digitalWrite(DIRECTIONA, 0);
      digitalWrite(DIRECTIONB, 0);
      break;
    case ‘f’:
      digitalWrite(DIRECTIONA, 1);
      digitalWrite(DIRECTIONB, 1);
      break;
    case ‘l’:
      digitalWrite(DIRECTIONA, 1);
      digitalWrite(DIRECTIONB, 0);
      break;
    case ‘r’:
      digitalWrite(DIRECTIONA, 0);
      digitalWrite(DIRECTIONB, 1);
    analogWrite (MOTORA, 200);
    analogWrite (MOTORB, 200);
  }
    while(true) {
      if ((Position0>pos0+5000) || (Position1>pos1+5000) ||
          (Position0>pos0-5000) || (Position1>pos1-5000)) {
        break;
      }
    }
  }
  analogWrite (MOTORA, 0);
  analogWrite (MOTORB, 0);
}  // end of loop