How To Make A Program Adafruit Motor Shield V2 With Button S

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

How To Make A Program Adafruit Motor Shield V2 With Button S

Post by Inra_Juntak »

Hallo,

I want to create a project, to drive 2 stepper motors using Adafruit Motor shield V2 + 2 button switch (start and stop) but I do not know to make the program and how to wiring button switch with Adafruit motor shield

My plan, when the switch button (start) is pressed motor ON and when the switch button (stop) is pressed again the motor does OFF.

Please help Me,

Regards,

BANNED.S
Attachments
This button switches which I will use
This button switches which I will use
images.jpg (5.52 KiB) Viewed 1925 times

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by adafruit_support_bill »

Do you have your motor connected and tested? That is the place to start.
Then connect your switch and make sure you can read the switch state reliably.
This tutorial explains how: https://learn.adafruit.com/adafruit-ard ... tal-inputs

User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by Inra_Juntak »

Yes
how to declare motor stepper in program, whether directly int myStepper1 or else

regards

BANNED.S

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by adafruit_support_bill »

Instructions for driving stepper motors are here: https://learn.adafruit.com/adafruit-mot ... per-motors
Use the StepperTest example from the library to make sure that your motor is connected properly and working.

User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by Inra_Juntak »

Hallo I've tried it, but i dont know to declare motor stepper in program

example :

Code: Select all

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonApin = 2 ;
int buttonBpin = 3 ;
int stepperMotor; // how to declare the motor 

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
  Serial.begin(9600);           
  Serial.println("Stepper test!");
  pinMode(stepperMotor, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);

  AFMS.begin();  
    
  myMotor->setSpeed(10);     
}

User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by Inra_Juntak »

Hallo,

can you make sample program to me about adafruit motor shield with button

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by adafruit_support_bill »

Hallo I've tried it, but i dont know to declare motor stepper in program
Just like in the example code:

Code: Select all

Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
can you make sample program to me about adafruit motor shield with button
Does the motor work with the example code?

User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by Inra_Juntak »

i use example code stepper test, but i dont know to combine the code with button switch,

Code: Select all

/* 
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>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonPin1 = 2 ;
int buttonPin2 = 3 ;

int myStepper ;

// 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); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
  pinMode (buttonPin1, INPUT_PULLUP);
  pinMode (buttonPin2, INPUT_PULLUP);
  Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  myMotor->setSpeed(10);  // 10 rpm   
}

void loop() {
  
  if (digitalRead(buttonPin1) == HIGH)
  {
    digitalWrite(myStepper, HIGH);
  }
  if (digitalRead(buttonPin2) == LOW)
  {
    digitalWrite(myStepper, LOW);
  }
}
  
Whether the program above is correct
How to declare Motor Stepper at Void Setup and Void Loop
i really not understand, please give me instruction step by step.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by adafruit_support_rick »

You want the stepper to start running when you press button 1, and you want it to stop when you press button 2. So, you need to check both buttons in loop(). LOW means that the button is pressed. If button 1 is pressed, set a variable to say that the motor should be running. If button 2 is pressed, set the variable to say that the motor should be stopped:

Code: Select all

  if (digitalRead(buttonPin1) == LOW)   //if start button is pressed
  {
    runState = true;      //start running stepper
  }
  
  if (digitalRead(buttonPin2) == LOW)   //if stop button is pressed
  {
    runState = false;    //stop running stepper
  }
Now, if the variable says that the motor should be running, advance the motor by one step:

Code: Select all

  if (runState)  //if the stepper should be running
  {
    myMotor->step(1, FORWARD, SINGLE);    //advance it one step.
  }
Here's the entire sketch:

Code: Select all

/* 
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>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonPin1 = 2 ;
int buttonPin2 = 3 ;

bool runState = false;

// 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); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
  pinMode (buttonPin1, INPUT_PULLUP);
  pinMode (buttonPin2, INPUT_PULLUP);
  Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  myMotor->setSpeed(10);  // 10 rpm   
}

void loop() {
  
  if (digitalRead(buttonPin1) == LOW)   //if start button is pressed
  {
    runState = true;      //start running stepper
  }
  
  if (digitalRead(buttonPin2) == LOW)   //if stop button is pressed
  {
    runState = false;    //stop running stepper
  }
  
  if (runState)  //if the stepper should be running
  {
    myMotor->step(1, FORWARD, SINGLE);    //advance it one step.
  }
}
  

User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by Inra_Juntak »

thank you very much

so, to run 2 motors at once, what should be added in the program

sorry before, I inquired continue

regards

BANNED.S

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by adafruit_support_bill »

so, to run 2 motors at once, what should be added in the program
Assuming that you want them both to do the same thing. Simply define another motor:

Code: Select all

Adafruit_StepperMotor *myOtherMotor = AFMS.getStepper(200, 1);
And every place you move one motor, move the other one as well:

Code: Select all

myMotor->step(1, FORWARD, SINGLE);    //advance it one step.
myOtherMotor->step(1, FORWARD, SINGLE);    //advance it one step.

User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by Inra_Juntak »

thank you very much :-)

User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by Inra_Juntak »

Hallo,

I want to ask how to set the speed of the motor using a potentiometer

Thanks

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by adafruit_support_bill »

Assuming that you have a potentiometer connected to analog 0, this code should do it:

Code: Select all

  
  int speed = map(analogRead(0), 0, 1024, 0, 255);  // Map the potentiometer input intoa motor speed
  motor.setSpeed(speed);

User avatar
Inra_Juntak
 
Posts: 68
Joined: Thu Sep 04, 2014 3:26 am

Re: How To Make A Program Adafruit Motor Shield V2 With Butt

Post by Inra_Juntak »

I've tried the code themselves but it does not work

My plan to drive the motor using the button. Where speed is set by potentiometer


Where the code i will type in the sketch

Error "motor" not declared

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”