Applying "Example" Code to other example codes, Merrr!

SO THIS IS THE EXAMPLE MOTOR CODE:
AND ID LIKE TO APPLY BUTTON CODE TO ACTIVATE THE EXAMPLE MOTOR CODE from “for (int i = 0; i <= 400; i++)”(Full Throttle) to “for (int i = 400; i >= 0; i--)” (Full Stop), VIA BUTTON BY HOLDING THE BUTTON. THEN RELEASING THE BUTTON TO ACTIVATE “for (int i = 0; i >= -400; i--)” (full reverse) to “for (int i = -400; i <= 0; i++)” (full stop).

#include "DualVNH5019MotorShield.h"
 
DualVNH5019MotorShield md;
 
void stopIfFault()
{
  if (md.getM1Fault())
  {
    Serial.println("M1 fault");
    while(1);
  }
  if (md.getM2Fault())
  {
    Serial.println("M2 fault");
    while(1);
  }
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Dual VNH5019 Motor Shield");
  md.init();
}
 
void loop()
{
  for (int i = 0; i <= 400; i++)
  {
    md.setM1Speed(i);
    stopIfFault();
    if (i%200 == 100)
    {
      Serial.print("M1 current: ");
      Serial.println(md.getM1CurrentMilliamps());
    }
    delay(2);
  }
   
  for (int i = 400; i >= 0; i--)
  {
    md.setM1Speed(i);
    stopIfFault();
    if (i%200 == 100)
    {
      Serial.print("M1 current: ");
      Serial.println(md.getM1CurrentMilliamps());
    }
    delay(2);
  }
//(RELEASE OF BUTTON CODE HERE)

  for (int i = 0; i >= -400; i--)
  {
    md.setM1Speed(i);
    stopIfFault();
    if (i%200 == 100)
    {
      Serial.print("M1 current: ");
      Serial.println(md.getM1CurrentMilliamps());
    }
    delay(2);
  }

 for (int i = -400; i <= 0; i++)
  {
    md.setM1Speed(i);
    stopIfFault();
    if (i%200 == 100)
    {
      Serial.print("M1 current: ");
      Serial.println(md.getM1CurrentMilliamps());
    }
    delay(2);
  }

Question:
how do i stop the motor at a certain distance? should i use this code instead:
And if so how do i apply it?

// first, include the library :)

#include <CheapStepper.h>

// next, declare the stepper
// and connect pins 8,9,10,11 to IN1,IN2,IN3,IN4 on ULN2003 board

CheapStepper stepper (8,9,10,11); 

 // let's create a boolean variable to save the direction of our rotation

boolean moveClockwise = true;


void setup() {

  // let's set a custom speed of 20rpm (the default is ~16.25rpm)
  
  stepper.setRpm(20); 
  /* Note: CheapStepper library assumes you are powering your 28BYJ-48 stepper
   * using an external 5V power supply (>100mA) for RPM calculations
   * -- don't try to power the stepper directly from the Arduino
   * 
   * accepted RPM range: 6RPM (may overheat) - 24RPM (may skip)
   * ideal range: 10RPM (safe, high torque) - 22RPM (fast, low torque)
   */

  // now let's set up a serial connection and print some stepper info to the console
  
  Serial.begin(9600); Serial.println();
  Serial.print(stepper.getRpm()); // get the RPM of the stepper
  Serial.print(" rpm = delay of ");
  Serial.print(stepper.getDelay()); // get delay between steps for set RPM
  Serial.print(" microseconds between steps");
  Serial.println();
  
  // stepper.setTotalSteps(4076);
  /* you can uncomment the above line if you think your motor
   * is geared 63.68395:1 (measured) rather than 64:1 (advertised)
   * which would make the total steps 4076 (rather than default 4096)
   * for more info see: http://forum.arduino.cc/index.php?topic=71964.15
   */
}

void loop() {

    // let's do a clockwise move first
  
    moveClockwise = true;

    // let's move the stepper clockwise to position 2048
    // which is 180 degrees, a half-turn (if using default of 4096 total steps)
    
    stepper.moveTo (moveClockwise, 2048);

    // now let's print the stepper position to the console
    
    Serial.print("step position: ");  
    Serial.print(stepper.getStep()); // get the current step position
    Serial.print(" / 4096");
    Serial.println();

    // now let's wait one second
    
    delay(1000); // wait a sec

    // and now let's move another 90 degrees (a quarter-turn) clockwise

    stepper.moveDegrees (moveClockwise, 90);
    // stepper.moveDegreesCW (90); <--- another way to do a clockwise 90 degree turn

    // let's print the stepper position to the console again
    
    Serial.print("step position: ");
    Serial.print(stepper.getStep());
    Serial.print(" / 4096");
    Serial.println();

    // and wait another second
    
    delay(1000);

    // ok, now let's reverse directions (to counter-clockwise)
    
    moveClockwise = false;

    // and move back to the start position (0 degree)
   
    stepper.moveToDegree (moveClockwise, 0);
    // moveClockwise is now false, so move counter-clockwise back to start

    // let's print the position to the console once again
    
    Serial.print("step position: ");
    Serial.print(stepper.getStep());
    Serial.print(" / 4096");
    Serial.println();

    // and wait another second before starting loop() over
    
    delay(1000);  
}

Hello.

Based on your description of how you want to control your motors’ movements using a button, it sounds like you want the motors to only run when the button is not pressed. I suggest looking at the button tutorial on the Arduino’s website to get a better understanding of how to listen for a button event and execute the desired behavior (e.g. running or stopping your motors).

I am not familiar with the stepper motor library you are using, so I cannot say if that code will suffice in what you want to do. However, from your questions, it sounds like you want to implement position control in your system, so I suggest looking into encoders or potentiometer feedback for your motors.

If you can provide more details about your setup and what it is you are trying to do, I might be able to provide a better suggestion. Also, if you run into problems when trying to implement the button control feature in your code, please post your entire code here, and I would be happy to take a look at it.

By the way, I added code tags to your post to make your code more readable. Please use this method to post code in the future. You can see the modification by click on the pencil icon in the upper right-hand corner of the edited post and view the raw source.

- Amanda

thank you, ill look into this now.

i forgot to mention:
im using DC brushed motors. they are 3 Amp motors each.

The second code example in your first post will not work with your DC motors; that example is intended to be used with stepper motors, specifically for the 28BYJ-48 stepper motor using the ULN2003 driver board. You should read the documentation on the CheapStepper Arduino library’s GitHub page to fully understand how the library works.

- Amanda

I got the code to compile with one of the button press and hold codes (“pushbuttonexample” first example in it). Woo!

But the part “for (int i = 0; i <= 400; i++)” is this 0, & 400 the amount of time the DC motors spin? Or is this the ramping time?


oh hey! i found this code to work with these pins that arent being used on the Arduino, for a DC Motor Brake.

const int 
PWM_A   = 5,
DIR_A   = ??,  <-----Except this one....im stuck on this one......
BRAKE_A = 11,
SNS_A   = A2;" 
unlike the ones used in the original code below Except the "DIR_A   = 12,". is there any pin on the Arduino i can use in substitute for the "DIR_A" instead of pin 12? Perhaps Pin 1 or 0?

const int 
PWM_A   = 3,
DIR_A   = 12,
BRAKE_A = 9,
SNS_A   = A0;


void setup() {
  // Configure the A output
  pinMode(BRAKE_A, OUTPUT);  // Brake pin on channel A
  pinMode(DIR_A, OUTPUT);    // Direction pin on channel A

  // Open Serial communication
  Serial.begin(9600);
  Serial.println("Motor shield DC motor Test:\n");
}

void loop() {

// Set the outputs to run the motor forward

  digitalWrite(BRAKE_A, LOW);  // setting brake LOW disable motor brake
  digitalWrite(DIR_A, HIGH);   // setting direction to HIGH the motor will spin forward

  analogWrite(PWM_A, 255);     // Set the speed of the motor, 255 is the maximum value

  delay(5000);                 // hold the motor at full speed for 5 seconds
  Serial.print("current consumption at full speed: ");
  Serial.println(analogRead(SNS_A));

// Brake the motor

  Serial.println("Start braking\n");
  // raising the brake pin the motor will stop faster than the stop by inertia
  digitalWrite(BRAKE_A, HIGH);  // raise the brake
  delay(5000);

// Set the outputs to run the motor backward

  Serial.println("Backward");
  digitalWrite(BRAKE_A, LOW);  // setting againg the brake LOW to disable motor brake
  digitalWrite(DIR_A, LOW);    // now change the direction to backward setting LOW the DIR_A pin

  analogWrite(PWM_A, 255);     // Set the speed of the motor

  delay(5000);
  Serial.print("current consumption backward: ");
  Serial.println(analogRead(SNS_A));

  // now stop the motor by inertia, the motor will stop slower than with the brake function
  analogWrite(PWM_A, 0);       // turn off power to the motor

  Serial.print("current brake: ");
  Serial.println(analogRead(A0));
  Serial.println("End of the motor shield test with DC motors. Thank you!");


  while(1);
}

I am glad that you were able to implement a button control feature into your code; thanks for letting us know.

In the DualVNH5019MotorShield library’s Demo example, the values that are iterated in the for loops represent the PWM duty cycle, where the value 400 is the maximum duty cycle (100%) and corresponds to the motor receiving the full motor supply voltage. The value -400 would be 100% duty cycle in the opposite direction, and 0 would be 0% duty cycle. Each for loop in main() ramps the speed up or down for each motor channel by varying the PWM duty cycle.

By the way, if you do not already know, the DualVNH5019MotorShield Arduino library contains brake functions: setM1Brake, setM2Brake, and setBrakes. You can see the definition for those functions in DualVNH5019MotorShield.cpp on the DualVNH5019MotorShield library’s GitHub page. Also, you might find it helpful to read this post about how the brake functions work in the DualVNH5019MotorShield library.

- Amanda