Ardunio Motor Shield Rev3 with Arduino Uno - Single DC Motor Programming Help

Hello, I have modified a code that was operating two Motors and I was trying to simplify it to only run a single motor. There are no error codes when checking the program, but when uploaded to the Motor shield nothing happens.

Anyone suggestions on what is wrong with the code? I could use the dual motor code with just one motor, but trying to learn a little of the programming so I would like to understand why this code will not run (Sketch also attached).

const int MotorPin = 12;
const int MotorSpeedPin = 3;
const int MotorBrakePin = 9;

const int CW = HIGH;
const int CCW = LOW;

const int showComments = 1;// show comments in serial monitor

void setup() {
// motor A pin assignment
pinMode(MotorPin, OUTPUT);
pinMode(MotorSpeedPin, OUTPUT);
pinMode(MotorBrakePin, OUTPUT);

Serial.begin(9600);// serial monitor initialized

}

void loop() {
brake(0); // release brake
moveMotor(CCW, 100);// motor A rotate CCW at 100 PWM value
delay(3000);
brake(1);
brake(0);
moveMotor(CW, 255);
delay(5000);
brake(1);

}// loop end

/*
*

  • moveMotor controls the motor
    @param motor is char A or B refering to motor A or B.
    @param dir is motor direction, CW or CCW
    @speed is PWM value between 0 to 255

Example 1: to start moving motor A in CW direction with 135 PWM value
moveMotor('A', CW, 135);

*/

void moveMotor(int dir, int speed)
{
int motorPin;
int motorSpeedPin;

digitalWrite(motorPin, dir);// set direction for motor
analogWrite(motorSpeedPin, speed);// set speed of motor
}//moveMotor end

/*

  • brake, stops the motor, or releases the brake
  • @param motor is character A or B
  • @param brk if 1 brake, if 0, release brake
  • example of usage:
  • brake('A', 1);// applies brake to motor A
  • brake('A', 0);// releases brake from motor A
    */
    void brake(int brk)
    {
    digitalWrite(MotorBrakePin, brk);// brake
    delay(1000);
    }

Motor_Test_05-02-20_Experimental2.ino (1.72 KB)

void moveMotor(int dir, int speed)
{
  int motorPin;
  int motorSpeedPin;
  
   digitalWrite(motorPin, dir);// set direction for motor
   analogWrite(motorSpeedPin, speed);// set speed of motor   
}//moveMotor end

Now think about this..

you set up variable motorPin. It has, who knows what sitting in there as a value.

Then..

digitalWrite(motorPin, dir); You use it to write out to a motor? On random motorPin?

motorSpeedPin.. Same story. You allocate it, but never acutlly set it to anything.

Then..

analogWrite(motorSpeedPin, speed); You use it to set value out to it.

Don't you think these pin variables should correspond to the pins you hook your motor controller to?

-jimlee

It might be a good idea to use the MotorPin and MotorSpeed that you defined at the top of the program instead of making new ones with similar names. NOTE: MotorPin and motorPin are NOT the same...case matters.

Steve

Thank you jimlee and Steve. I am new to programming so I am watching many online programming videos and trying understand code and parallel learning by using/modifying existing code. The above program was written for two motors and I was simplifying it to only run one and obviously left some incorrect commands no longer needed. Both of you hints got me into a running program.

A couple of follow-up questions for anyone that may have articles input on how why certain commands are being used as I have not yet found references yet.

Question 1: Sequence of code execution. The void loop program starts with the desired programming and references two functions (brake and moveMotor) that is not defined until later on in the code. How does the program get through the 1st loop without an error by trying to use a undefined function. I would think the moveMotor and brake would be defined ahead of the void loop program.

Question 2: After the digitalWrite(MotorBrakePin, brk) command, there is a delay(1000) command. I was surprised that this command executes to everytime brake is executed; i didn't realize it worked that way so, everytime the brake is called up, it assigned the brake parameter to the brake pin and then executes all the commands after the brake function - I assume it works that way as the two lines were part of one loop?

Below is the updated code. Thank you.

void brake(int brk)
{
digitalWrite(MotorBrakePin, brk);// brake
delay(1000);
}

Full Updated Code
/*

*/
const int MotorPin = 12;
const int MotorSpeedPin = 3;
const int MotorBrakePin = 9;

const int CW = HIGH;
const int CCW = LOW;

const int showComments = 1;// show comments in serial monitor

void setup() {
// motor A pin assignment
pinMode(MotorPin, OUTPUT);
pinMode(MotorSpeedPin, OUTPUT);
pinMode(MotorBrakePin, OUTPUT);

Serial.begin(9600);// serial monitor initialized

}

void loop() {
brake(0); // release brake
moveMotor(CCW, 100);// motor A rotate CCW at 100 PWM value
delay(2000);
brake(1);
brake(0);
moveMotor(CW, 255);
delay(2000);
brake(1);

}// loop end

/*
*

  • moveMotor controls the motor
    @param motor is char A or B refering to motor A or B.
    @param dir is motor direction, CW or CCW
    @speed is PWM value between 0 to 255

Example 1: to start moving motor A in CW direction with 135 PWM value
moveMotor('A', CW, 135);

*/

void moveMotor(int dir, int speed)
{

digitalWrite(MotorPin, dir);// set direction for motor
analogWrite(MotorSpeedPin, speed);// set speed of motor
}//moveMotor end

/*

  • brake, stops the motor, or releases the brake
  • @param motor is character A or B
  • @param brk if 1 brake, if 0, release brake
  • example of usage:
  • brake('A', 1);// applies brake to motor A
  • brake('A', 0);// releases brake from motor A
    */
    void brake(int brk)
    {
    digitalWrite(MotorBrakePin, brk);// brake
    delay(1000);
    }

"void loop program"

Vocabulary..

void <-- Means the loop() Function does not return a value.
loop() <-- is a function, not a program. And not a void.
The entire thing is a program.
code is never plural.
delay() <-- Can be thought of as a red newbie flag. See code with a lot of those? Just move along.

The reason you can mix up the order of your functions is that the Arduino IDE, Your programming environment, hides the stuff that defines all your functions and global variables. But it only does this in your .ino file. .cpp files are not monkeyed with as much.

Questing #2 see delay() above.

Oh! And a word of warning. Start using code tags when posting code. The "regulars" get all spun up in a tizzy if you don't. They will text harshly at you.

-jim lee

And to work out what commands are executed when you need to take note of the {} braces. The code executed when a function like brake() is called will be everything between the opening { and closing } braces. So the two lines are part of one function[/] not one loop.
That's all basic C/C++ programming and you'll see the same {} construct with things like if statements and for loops defining how much code is controlled by those functions.
In this programming thing every single character generally means something specific. A misplaced ( , . or { can stop everything working.
Steve