Programming Help

Hello,

I am taking a Robotics class and I’m trying to program a little Omni wheel robot. I have a wicked device arduino uno and a motor shield. I have two servo motors connected. The goal here is to have the robot move around on its own and when it gets too close to an object, the motors would change direction to avoid it. This is what I have so far:

#include <Wicked_DCMotor.h>

void printCurrentSensing(void);

int num_motors = 2;
Wicked_DCMotor motor1(M1);
Wicked_DCMotor motor2(M2);

int sensorPin=0;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print(F(“Wicked Motor Shield Library version “));
Serial.print(WickedMotorShield::version());
Serial.println(F(”- DC Motors”));
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
for(int ii = 0; ii < num_motors; ii++){
Serial.println(F("================================"));
int val=analogRead(sensorPin);
Serial.println(val);

if(val<180)

{
// m[ii]->setDirection(DIR_CW); // clockwise
// m[ii]->setBrake(BRAKE_OFF); // no brake applied

Serial.print(F(“Turning on Motor M”));
Serial.print(ii+1);
Serial.println(F(", Full Speed, Clockwise"));
// m[ii]->setSpeed(255); // full speed
}
if(val>180)

{
// m[ii]->setDirection(DIR_CCW); // counter clockwise
// m[ii]->setBrake(BRAKE_OFF); // no brake applied

Serial.print(F(“Turning on Motor M”));
Serial.print(ii+1);
Serial.println(F(", Full Speed, Counter Clockwise"));
//m[ii]->setSpeed(255); // full speed
delay(2000);

}
}
}

The motors don’t turn on. When I check the serial monitor it shows that the motors are turning off and on as the sensor values change (my hand moving closer/further from the sensor.) What am I doing wrong here? They motors DO work with other code, just not as written above.

Thank you

You have all the motor control lines commented out, for example:

xsamarax:

// m[ii]->setDirection(DIR_CW); // clockwise

//  m[ii]->setBrake(BRAKE_OFF);  // no brake applied

When you put the // in front of a line of code that makes the compiler ignore that line so you would need to uncomment all those lines. For example:

m[ii]->setDirection(DIR_CW); // clockwise
m[ii]->setBrake(BRAKE_OFF);  // no brake applied

Please use code tags(</> button on the toolbar) when you post code or error/warning messages.

When your code uses a library that must be installed like Wicked_DCMotor it’s a good idea to post the URL where the library can be downloaded or say if you used Library Manager to install it.

Please do Tools > Auto Format on your code before posting. This will make it easier for you to spot bugs and easier for us to read it

Thank you so much for your help. I commented out the motor lines b/c they were giving me errors when I compiled it. Any ideas how to rewrite them so they actually work?

xsamarax:
compiled it. Any ideas how to rewrite them so they actually work?

If you look at File > Examples > WickedMotorShield > DC_Motor(and I’m sure your other working code which you should have posted), you’ll notice at line 11:

Wicked_DCMotor *m[] = {&motor1, &motor2, &motor3, &motor4};

Then if you look at the code you posted there’s nothing like it so of course it’s not going to work. Since you only have 2 motors you need to add:

Wicked_DCMotor *m[] = {&motor1, &motor2};

So your sketch with the fixes I’ve recommended is:

#include <Wicked_DCMotor.h>

void printCurrentSensing(void);

int num_motors = 2;
Wicked_DCMotor motor1(M1);
Wicked_DCMotor motor2(M2);
Wicked_DCMotor *m[] = {&motor1, &motor2};

int sensorPin = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.print(F("Wicked Motor Shield Library version "));
  Serial.print(WickedMotorShield::version());
  Serial.println(F("- DC Motors"));
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int ii = 0; ii < num_motors; ii++) {
    Serial.println(F("================================"));
    int val = analogRead(sensorPin);
    Serial.println(val);

    if (val < 180)

    {
      m[ii]->setDirection(DIR_CW); // clockwise
      m[ii]->setBrake(BRAKE_OFF);  // no brake applied

      Serial.print(F("Turning on Motor M"));
      Serial.print(ii + 1);
      Serial.println(F(", Full Speed, Clockwise"));
      m[ii]->setSpeed(255);        // full speed
    }
    if (val > 180)
    {
      m[ii]->setDirection(DIR_CCW); // counter clockwise
      m[ii]->setBrake(BRAKE_OFF);   // no brake applied

      Serial.print(F("Turning on Motor M"));
      Serial.print(ii + 1);
      Serial.println(F(", Full Speed, Counter Clockwise"));
      m[ii]->setSpeed(255);         // full speed
      delay(2000);
    }
  }
}

You see how much better that looks with the Auto Format and the code tags?

Yes that works, thanks so much for your help!