Altering DC Motor Shield Program

I'm using an Arduino Microto drive the Adafruit Motorshield V2.3
I got the connections right and I successfully ran the original "DC Motor Test" that came with it in the examples.

I am attempting to alter the code to run one motor in one direction for 5 seconds when a button is pushed.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
  Serial.begin(9600);
  AFMS.begin();
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  pinMode(7, INPUT_PULLUP);
}
void loop() {
  uint8_t i;
  if (digitalRead(7) == HIGH) {
    myMotor->run(FORWARD);
    delay(5000);
  }
}

The code verifies and uploads successfully but the motor just runs whether the button is pushed or not. I'm not familiar with libraries at all. Is it just running initially from the callout in the void setup?

If it would help, I can reply with the code from the original DC motor test.

It looks to me like you start the motor running in the setup routine.
Then in the loop, if the button is pressed, you start the motor running and wait 5 seconds.

Would it help if at some point, you told the motor to stop?

vinceherman:
It looks to me like you start the motor running in the setup routine.
Then in the loop, if the button is pressed, you start the motor running and wait 5 seconds.

Would it help if at some point, you told the motor to stop?

It would...eventually :smiley:

I slashed out the following two lines to test and nothing ran at all...even if I did push the button.

// myMotor->setSpeed(150);
// myMotor->run(FORWARD);

Yes, eventually I'd like the motor to stop after the 5 seconds but I guess I was just trying to get it to even recognize the button function.

I tried moving the setSpeed down to the if statement and it just turned on right away without the button being pushed

#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
  Serial.begin(9600);
  AFMS.begin();
  //myMotor->setSpeed(150);
  //myMotor->run(FORWARD);
  pinMode(7, INPUT_PULLUP);
}
void loop() {
  uint8_t i;
  if (digitalRead(7) == HIGH) {
    myMotor->setSpeed(150);
    myMotor->run(FORWARD);
    delay(5000);
  }
}

Why drop the set speed?
Issue a motor stop after the 5000 delay.

Railroader:
Why drop the set speed?
Issue a motor stop after the 5000 delay.

I put the set seep back into the setup. Then I added "myMotor->run(RELEASE);" after the delay. This is seen in the original DC Motor Test program.

...no change, the motor still turns on without any input from the button.

I'm using the Arduino Micro. I've tried digital pins 5, 7 and 11. I assume I'm calling that out correctly.

Why not let the set speed stay in setup?
Check the motor commands? Is Release the correct command for stopping?
What about set speed(0) as stopping, and then set speed (150) to start later.

Railroader:
Why not let the set speed stay in setup?
Check the motor commands? Is Release the correct command for stopping?
What about set speed(0) as stopping, and then set speed (150) to start later.

I moved the speed back up to set up and converted the if statement to contain an original part of the DC motor test code. I read that the "run" command needs to have something finish after it's been issued (learned something today) so I figured ill just leave the original script in place until I figure out the button trigger code.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
  Serial.begin(9600);
  AFMS.begin();
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  pinMode(11, INPUT_PULLUP);
}
void loop() {
  uint8_t i;
  if (digitalRead(11) == HIGH) {
  }
  myMotor->run(FORWARD);
  for (i = 0; i < 255; i++) {
    myMotor->setSpeed(i);
    delay(5000);
    myMotor->setSpeed(0);
  }

}

After this uploads, all I hear is a whining from the motor. It still isn't responding to the button input.

Here is the code form the original program that I'm trying to alter

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  // turn on motor
  myMotor->run(RELEASE);
}

void loop() {
  uint8_t i;
  
  Serial.print("tick");

  myMotor->run(FORWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i);  
    delay(10);
  }
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i);  
    delay(10);
  }
  
  Serial.print("tock");

  myMotor->run(BACKWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i);  
    delay(10);
  }
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i);  
    delay(10);
  }

  Serial.print("tech");
  myMotor->run(RELEASE);
  delay(1000);
}

Look at Your if (button)..{}..... That makes butto pressing a NOP. Drop the }.
Release seams to be like an execut orders. Try, Set speed, Set direction and then Release to execute.
Set speed(0) seams to be stop.

Railroader:
Look at Your if (button)..{}..... That makes butto pressing a NOP. Drop the }.
Release seams to be like an execut orders. Try, Set speed, Set direction and then Release to execute.
Set speed(0) seams to be stop.

I'm still confused about the RELEASE command. I adjusted the code per your suggestion and I think I made some progress. However, the motor still turns on regardless if the button is pushed or not. My understanding of "IF...ELSE" still seems to be lacking. I'm trying to find more examples but I'm missing something. (Trying hard not to get frustrated with myself).

#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
  Serial.begin(9600);
  AFMS.begin();
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  pinMode(11, INPUT_PULLUP);
}
void loop() {
  uint8_t i;
  if (digitalRead(11) == HIGH) {
    myMotor->setSpeed(150);
    myMotor->run(FORWARD);
    delay(5000);
    myMotor->run(RELEASE);
  }
  else {
    myMotor->setSpeed(0);
    myMotor->run(RELEASE);
  }
}

For kicks, I changed the ==HIGH to LOW and re-uploaded. The motor didn't spin at all so then I changed it back to ==HIGH. If the code is correct, which I'm still not sure if it is, then the board is presumably seeing 5V going to pin 11 somehow. So then I just pulled the button out completely (faulty button...I dunno) and tried again. Same thing. The motor just runs without any input to pin 11.

I'm really trying a lot of things and it feels like I'm close but I'm hoping someone can shed some light on what I'm missing.

Change place between delay(5000) and motor release!
That might do something.

If(....) execute one instruction.
else......

if(...) { several sentencies }
else...... execute according to one sentence or several sentencies.

Change place between delay(5000) and motor release!
That might do something.

If(....) execute one instruction.
else......

if(...) { several sentencies }
else...... execute according to one sentence or several sentencies.

Railroader:
Change place between delay(5000) and motor release!
That might do something.

If(....) execute one instruction.
else......

if(...) { several sentencies }
else...... execute according to one sentence or several sentencies.

If I understand correctly, I should be using the curly brackets since I do have several things after the "IF" statement...which I do. Thank you for clarifying. I wasn't aware of that circumstance.

I moved switched around the release and delay...the result was that the motor didn't run at all but I could feel a subtle click every 5 seconds. This is still happening without any input from the button.

I have the button called out as INPUT_PULLUP. I have a wire going from 11 (Arduino Micro has it labeled as 11 (PWM)) to the momentary pushbutton switch. The other side of the switch is going to common ground. The ground pin from the Micro is also going to common ground. Is this wired correctly? Do I need to post more information?

#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
  Serial.begin(9600);
  AFMS.begin();
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  pinMode(11, INPUT_PULLUP);
}
void loop() {
  uint8_t i;
  if (digitalRead(11) == HIGH) {
    myMotor->setSpeed(150);
    myMotor->run(FORWARD);
    myMotor->run(RELEASE);
    delay(5000);
  }
  else {
    myMotor->setSpeed(0);
    myMotor->run(RELEASE);
  }
}

I can't find any information on the libraries used for this program but I don't believe my problems lie within the lack of understanding of the libraries.

Discussing i/o pins is not my field. Lets hope somebody steps in and clears that issue.
As faar as I see Your code looks ok.
Check the motor library again, look at examples etc. to go deaper.

Railroader:
Discussing i/o pins is not my field. Lets hope somebody steps in and clears that issue.
As faar as I see Your code looks ok.
Check the motor library again, look at examples etc. to go deaper.

I seriously appreciate your help thus far. Even though the solution hasn't come to fruition yet, I'm learning stuff as I try new things.

How do I look at whatever is in a library?

I've been reading through a bunch of other IF..ELSE examples and it seems to make sense. I feel that the nuances associated with this particular library are making this a bit more difficult. Specifically, I'm not sure what is intended within the "RELEASE" command.

I changed my code a bit in order to try simplifying what was happening within the IF portion and I finally got somewhere in my efforts to have this program recognize the button input. When I upload this portion, the motor starts running right away but WHEN I push the button, the motor stops. Unfortunately the motor stops indefinitely and never starts again.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
  Serial.begin(9600);
  AFMS.begin();
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  pinMode(11, INPUT_PULLUP);
}
void loop() {
  uint8_t i;
  if (digitalRead(11) == HIGH) {
    myMotor->run(FORWARD);
  }
  else {
    myMotor->setSpeed(0);
    myMotor->run(RELEASE);
    delay(5000);
  }
}

Going to try a few other things but I feel I'm getting closer!

Move the delay(5000) from the else part to the last of the if part!

Railroader:
Move the delay(5000) from the else part to the last of the if part!

Motor started running right away. I let it run for at least 20 seconds. No initial effect from pushing the button.

Re-uploaded several times and noticed that sometimes the motor would quit sometime after the button was pressed...not a presumed 5 seconds. Other times nothing would happen unless I mashed the button several times or held in the button. All had a various timing affect that eventually shut off the motor. In every attempt, the motor never came back on.

Is there a way I can see what's going on in the library to understand the commands therein? How do you look at a library? Should I even be looking there?

In the early example code there is a delay, some 10mS added after setting direction and/or aftet setting speed. Try that.
After one run the motor ought to stop. Remember that the button is not read during execution of delay, especially not during 5000 mS.
Please add the last code tested. Small things can make a big difference..

#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
  Serial.begin(9600);
  AFMS.begin();
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  myMotor->run(RELEASE);
  pinMode(11, INPUT_PULLUP);
}
void loop() {
  if (digitalRead(11) == HIGH) {
    myMotor->run(FORWARD);
    delay(5000);
  }
  else {
    myMotor->setSpeed(0);
    myMotor->run(RELEASE);
  }
}

This code had the same results as stated above. I added myMotor->run(RELEASE) to set up and I took out that weird "uint8_t i;" that was at the very beginning of the loop (not sure what it is). Neither change affected the outcome. I haven't tried adding the 10ms to different places yet.