Sparkfun - Monster Moto Shield Programming Issue

Hi guys, I am a student working on a project that involves 2 tamiya plasma - dc motors.

I have bought the monster moto shield with UNO to run this simple 2 motor robot controlled by bluetooth using android phone. Below is my code, but I have problem compiling. :blush: :sweat_smile:

#define BRAKEVCC 0
#define CW 1
#define CCW 2
#define BRAKEGND 3
#define CS_THRESHOLD 100

/* VNH2SP30 pin definitions
xxx[0] controls '1' outputs
xxx[1] controls '2' outputs */
char dataIn='S';char determinant;char det;int vel = 0; //Bluetooth Stuff
/int power = 4;/
int inApin[2] = {7, 4}; // INA: Clockwise input
int inBpin[2] = {8, 9}; // INB: Counter-clockwise input
int pwmpin[2] = {5, 6}; // PWM input
int cspin[2] = {2, 3}; // CS: Current sense ANALOG input
int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin)

int statpin = 13;

void setup()
{
Serial.begin(9600);

pinMode(statpin, OUTPUT);

//initialise digital pins as o/p
for (int i=0; i<2; i++)
{
pinMode(inApin*, OUTPUT);*
_ pinMode(inBpin*, OUTPUT);_
_ pinMode(pwmpin, OUTPUT);
}
// Initialize braked*
* for (int i=0; i<2; i++)
{
digitalWrite(inApin, LOW);
digitalWrite(inBpin, LOW);
}
// motorGo(0, CW, 1023);
// motorGo(1, CCW, 1023);
}
void motorOff(int motor)
{
// Initialize braked*

* for (int i=0; i<2; i++)
{
digitalWrite(inApin, LOW);
digitalWrite(inBpin, LOW);
}
analogWrite(pwmpin[motor], 0);
}
void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
{
if (motor <= 1)
{
if (direct <=4)
{
// Set inA[motor]
if (direct <=1)
digitalWrite(inApin[1], HIGH);
else*

* digitalWrite(inApin[1], LOW);
// Set inB[motor]
if ((direct==0)||(direct==2))
digitalWrite(inBpin[0], HIGH);
else*

* digitalWrite(inBpin[0], LOW);
analogWrite(pwmpin[0], pwm);
}
}
}
void loop()
{ det = check();*_

* while (det == 'F') // F, move forward*
* {motorGo(0,1,vel);motorGo(1,1,vel);det = check();}*

* while (det == 'B') // B, move back*
* {motorGo(0,2,vel);motorGo(1,2,vel);det = check();}*
* while (det == 'L') // L, move wheels left*
* {motorGo(0,1,vel/4);motorGo(1,1,vel);det = check();}*

* while (det == 'R') // R, move wheels right*
* {motorGo(1,1,vel/4);motorGo(0,1,vel);det = check();}*

* while (det == 'I') // I, turn right forward*
* {motorGo(0,1,vel/2);motorGo(1,1,vel);det = check();}*

* while (det == 'J') // J, turn right back*
* {motorGo(0,2,vel/2);motorGo(1,2,vel);det = check();} *

* while (det == 'G') // G, turn left forward*
* {motorGo(1,1,vel/2);motorGo(0,1,vel);det = check();}*

* while (det == 'H') // H, turn left back*
* {motorGo(1,2,vel/2);motorGo(0,2,vel);det = check();} *

* while (det == 'S') // S, stop*
* {motorGo(0,3,0);motorGo(1,3,0);det = check();}*

* //---------------------Toggle switch code------------------//*
_ /* while (det == 'U')
* {digitalWrite(power, HIGH);det = check();}
while (det == 'u')
{digitalWrite(power, LOW);det = check();}/

* //---------------------Mains Power------------------//
while (det == 'W')
{digitalWrite(statpin, HIGH);det = check();}
while (det == 'w')
{digitalWrite(statpin, LOW);det = check();}
}
int check()
{if (Serial.available() > 0) {dataIn = Serial.read();
if (dataIn == 'F'){determinant = 'F';}
else if (dataIn == 'B'){determinant = 'B';}else if (dataIn == 'L'){determinant = 'L';}
else if (dataIn == 'R'){determinant = 'R';}else if (dataIn == 'I'){determinant = 'I';}
else if (dataIn == 'J'){determinant = 'J';}else if (dataIn == 'G'){determinant = 'G';}
else if (dataIn == 'H'){determinant = 'H';}else if (dataIn == 'S'){determinant = 'S';}
else if (dataIn == '0'){vel = 400;}else if (dataIn == '1'){vel = 380;}
else if (dataIn == '2'){vel = 340;}else if (dataIn == '3'){vel = 320;}
else if (dataIn == '4'){vel = 280;}else if (dataIn == '5'){vel = 240;}
else if (dataIn == '6'){vel = 200;}else if (dataIn == '7'){vel = 160;}
else if (dataIn == '8'){vel = 120;}else if (dataIn == '9'){vel = 80;}
else if (dataIn == 'q'){vel = 40;}else if (dataIn == 'U'){determinant = 'U';}
else if (dataIn == 'u'){determinant = 'u';}else if (dataIn == 'W'){determinant = 'W';}
else if (dataIn == 'w'){determinant = 'w';}}return determinant;}
}*_

I am new to Arduino programming, please pardon me for any dumb mistakes. Hope someone can assist in identifying the problem :sweat_smile:

please pardon me for any dumb mistakes.

Not this one, we won't. There are two posts at the top of the forum that you were supposed to read BEFORE posting code here, so that you would know how to post code without the forum software mangling it.

I am absolutely positive that your code does NOT look like that.

Read the posts, and do it right.

Why does check() return a global variable? Why is that value assigned to another global variable?

PaulS:
Not this one, we won't. There are two posts at the top of the forum that you were supposed to read BEFORE posting code here, so that you would know how to post code without the forum software mangling it.

I am absolutely positive that your code does NOT look like that.

Read the posts, and do it right.

Why does check() return a global variable? Why is that value assigned to another global variable?

Apologize for not reading prior to post.

#define BRAKEVCC 0
#define CW   1
#define CCW  2
#define BRAKEGND 3
#define CS_THRESHOLD 100

/*  VNH2SP30 pin definitions
 xxx[0] controls '1' outputs
 xxx[1] controls '2' outputs */
char dataIn='S';char determinant;char det;int vel = 0; //Bluetooth Stuff
/*int power = 4;*/
int inApin[2] = {7, 4};  // INA: Clockwise input
int inBpin[2] = {8, 9}; // INB: Counter-clockwise input
int pwmpin[2] = {5, 6}; // PWM input
int cspin[2] = {2, 3}; // CS: Current sense ANALOG input
int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin)

int statpin = 13;

void setup()
{
  Serial.begin(9600);
  
  pinMode(statpin, OUTPUT);
  
  //initialise digital pins as o/p
  for (int i=0; i<2; i++)
  {
    pinMode(inApin[i], OUTPUT);
    pinMode(inBpin[i], OUTPUT);
    pinMode(pwmpin[i], OUTPUT);
  }
  // Initialize braked
  for (int i=0; i<2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], LOW);
  }
  // motorGo(0, CW, 1023);
  // motorGo(1, CCW, 1023);
}

void motorOff(int motor)
{
  // Initialize braked
  for (int i=0; i<2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], LOW);
  }
  analogWrite(pwmpin[motor], 0);
}

void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
{
  if (motor <= 1)
  {
    if (direct <=4)
    {
      // Set inA[motor]
      if (direct <=1)
        digitalWrite(inApin[1], HIGH);
      else
        digitalWrite(inApin[1], LOW);

      // Set inB[motor]
      if ((direct==0)||(direct==2))
        digitalWrite(inBpin[0], HIGH);
      else
        digitalWrite(inBpin[0], LOW);

      analogWrite(pwmpin[0], pwm);
    }
  }
}

void loop()

{    

  det = check();
      
      while (det == 'F')   // F, move forward
       {motorGo(0,1,vel);motorGo(1,1,vel);det = check();}
      
      while (det == 'B')   // B, move back
       {motorGo(0,2,vel);motorGo(1,2,vel);det = check();}

      while (det == 'L')   // L, move wheels left
       {motorGo(0,1,vel/4);motorGo(1,1,vel);det = check();}
       
      while (det == 'R')   // R, move wheels right
       {motorGo(1,1,vel/4);motorGo(0,1,vel);det = check();}
     
      while (det == 'I')   // I, turn right forward
       {motorGo(0,1,vel/2);motorGo(1,1,vel);det = check();}
     
      while (det == 'J')   // J, turn right back
       {motorGo(0,2,vel/2);motorGo(1,2,vel);det = check();}   
      
      while (det == 'G')   // G, turn left forward
       {motorGo(1,1,vel/2);motorGo(0,1,vel);det = check();} 
     
      while (det == 'H')   // H, turn left back
       {motorGo(1,2,vel/2);motorGo(0,2,vel);det = check();}   
      
      while (det == 'S')   // S, stop
       {motorGo(0,3,0);motorGo(1,3,0);det = check();}
       
      //---------------------Toggle switch code------------------//
     /* while (det == 'U')   
      {digitalWrite(power, HIGH);det = check();}
      while (det == 'u')   
      {digitalWrite(power, LOW);det = check();}*/
      //---------------------Mains Power------------------//
      while (det == 'W')   
      {digitalWrite(statpin, HIGH);det = check();}
      while (det == 'w')   
      {digitalWrite(statpin, LOW);det = check();}
}

int check()
{if (Serial.available() > 0) {dataIn = Serial.read();  
        if (dataIn == 'F'){determinant = 'F';}  
        else if (dataIn == 'B'){determinant = 'B';}else if (dataIn == 'L'){determinant = 'L';}
        else if (dataIn == 'R'){determinant = 'R';}else if (dataIn == 'I'){determinant = 'I';}  
        else if (dataIn == 'J'){determinant = 'J';}else if (dataIn == 'G'){determinant = 'G';}    
        else if (dataIn == 'H'){determinant = 'H';}else if (dataIn == 'S'){determinant = 'S';}
        else if (dataIn == '0'){vel = 400;}else if (dataIn == '1'){vel = 380;}
        else if (dataIn == '2'){vel = 340;}else if (dataIn == '3'){vel = 320;}
        else if (dataIn == '4'){vel = 280;}else if (dataIn == '5'){vel = 240;}
        else if (dataIn == '6'){vel = 200;}else if (dataIn == '7'){vel = 160;}
        else if (dataIn == '8'){vel = 120;}else if (dataIn == '9'){vel = 80;}
        else if (dataIn == 'q'){vel = 40;}else if (dataIn == 'U'){determinant = 'U';}
        else if (dataIn == 'u'){determinant = 'u';}else if (dataIn == 'W'){determinant = 'W';}
        else if (dataIn == 'w'){determinant = 'w';}}return determinant;
      
 }

this is to use the monster moto shield with uno and hc-06 to run a bluetooth controlled dual dc motor robot. It seems like there is no input to the serial when data is sent to the module. May I know if the "motorgo(x,y,z)" was used correctly?

        digitalWrite(inApin[1], HIGH);

It hardly makes sense to write to an input pin. If "in" in the name does not mean input, then you need a better name for the variable that does not imply that the pins are input pins.

void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
{
  if (motor <= 1)

And it is isn't?

      // Set inA[motor]
      if (direct <=1)
        digitalWrite(inApin[1], HIGH);
      else
        digitalWrite(inApin[1], LOW);

No, it does not. It sets the state of ONE pin, regardless of which motor is supposed to be controlled.

      // Set inB[motor]
      if ((direct==0)||(direct==2))
        digitalWrite(inBpin[0], HIGH);
      else
        digitalWrite(inBpin[0], LOW);

No, it does not. It sets the state of ONE pin, regardless of which motor is supposed to be controlled.

      analogWrite(pwmpin[0], pwm);

Does it not matter which motor is supposed to be controlled?

      while (det == 'F')   // F, move forward
       {motorGo(0,1,vel);motorGo(1,1,vel);det = check();}

This is too difficult to read.

      while (det == 'F')   // F, move forward
      {
         motorGo(0, 1, vel);
         motorGo(1, 1, vel);
         det = check();
      }

Is FAR easier to read.

Now, tell me why you have to write the same values to the same pins over and over. The motorGo() calls do NOT need to be in the body of the while statement(s).

It seems like there is no input to the serial when data is sent to the module.

Then why are you focusing on the motor control portion of the code? If your data input mechanism isn't providing data, then how to use the (non-existent) data does not matter.

Get the bluetooth device off of the hardware serial pins so that you can use them for debugging.

Appreciate your kind response. actually the code I got from the standard sample monster moto shield coding online and modified the statpin and bluetooth input so that it would change the motorGo settings. The changes mainly focus on the bluetooth input, the rest is not changed much. That is why I am focusing on whether the bluetooth receive and able to communicate.

Thanks for reminding, maybe I should start with debugging on the shield itself first. :sweat_smile: