0

Motor Shield, Contolling one motor DC with three buttons
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Motor Shield, Contolling one motor DC with three buttons

by pixeltx on Mon Sep 02, 2019 6:08 pm

I'm trying something that seems simple but my code is missing something, I have a motor shield V2 connected to a DC motor and have three buttons that I would like to use to drive the motor , button 1 run forward, button 2, run backward and button 3 stop. When I upload the sketch the motor start running as if the buttons were pressed (HIGH) even they are not, what am I missing in my code?, The buttons are wired with a pull down resistor. The Serial monitor prints Tick, Tack, Tock, even though I'm not pressing any buttons. I will appreciate any feedback.


Code: Select all | TOGGLE FULL SIZE
#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);
const int button1Pin = 2;  // pushbutton 1 pin
const int button2Pin = 3;  // pushbutton 2 pin
const int button3Pin = 5;  // pushbutton 3 pin

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
 
  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(200);
  myMotor->run(FORWARD);
  // turn on motor
  myMotor->run(RELEASE);
}

void loop() {
  uint8_t i;
 
   int button1State, button2State, button3State;  // variables to hold the pushbutton states
   button1State = digitalRead(button1Pin);
   button2State = digitalRead(button2Pin);
   button3State = digitalRead(button3Pin);
   
   if (button1State == HIGH);
   Serial.print("tick");
   
  myMotor->run(FORWARD);
 {
    myMotor->setSpeed(250); 
    delay(1000);
  }

  if (button2State == HIGH);
 
  Serial.print("tock");

  myMotor->run(BACKWARD);
 {
    myMotor->setSpeed(250); 
    delay(1000);
 
 if (button3State == HIGH);

 
  }
 Serial.print("tack");
 
  myMotor->run(RELEASE);
  delay(1000);
}
Last edited by adafruit_support_bill on Mon Sep 02, 2019 7:36 pm, edited 1 time in total.
Reason: Please use the [code] button when posting code to the forums.

pixeltx
 
Posts: 4
Joined: Wed Feb 06, 2013 2:21 am

Re: Motor Shield, Contolling one motor DC with three buttons

by adafruit_support_bill on Mon Sep 02, 2019 7:37 pm

How are the buttons wired? Do you have pulldown resistors?

adafruit_support_bill
 
Posts: 74361
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor Shield, Contolling one motor DC with three buttons

by pixeltx on Mon Sep 02, 2019 8:19 pm

Yes, the buttons have a pull down resistor, one each per button, button 1 is connected to pin 2, button 2 is connected to pin 3 and button 3 is connected to pin 5 (I understand pin 4 is used by the shield to drive the DC motor)

pixeltx
 
Posts: 4
Joined: Wed Feb 06, 2013 2:21 am

Re: Motor Shield, Contolling one motor DC with three buttons

by adafruit_support_bill on Tue Sep 03, 2019 5:48 am

button 1 is connected to pin 2, button 2 is connected to pin 3 and button 3 is connected to pin 5 (I understand pin 4 is used by the shield to drive the DC motor)

Are you talking about analog pins or digital pins?

If you are using an Arduino UNO, analog pin 4 is the SDA pin and is used to communicate with the shield. All of the digital pins are available for switches. Your code is written to use digital pins.

adafruit_support_bill
 
Posts: 74361
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor Shield, Contolling one motor DC with three buttons

by pixeltx on Tue Sep 03, 2019 8:11 am

I'm using the digital pins. Am I missing to declare the status of the pins at start, like declare them LOW? Thank you!

pixeltx
 
Posts: 4
Joined: Wed Feb 06, 2013 2:21 am

Re: Motor Shield, Contolling one motor DC with three buttons

by kcl1s on Tue Sep 03, 2019 8:39 am

The Serial monitor prints Tick, Tack, Tock, even though I'm not pressing any buttons. I will appreciate any feedback.

That shows your pins are reading high. I think a photo of your wiring will help clear this up.

Fellow hobbyist
Keith

kcl1s
 
Posts: 1256
Joined: Tue Aug 30, 2016 12:06 pm

Please be positive and constructive with your questions and comments.