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.
User avatar
adafruit_support_bill
 
Posts: 88093
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 »

I've tried the code themselves but it does not work
You have to integrate it with the code that you have already.
Where the code i will type in the sketch

Error "motor" not declared
Your existing sketch must have a motor declared somewhere. Change "motor" to match the name of the one you have declared.

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 »

Give me the example please

But this code not working, where the code must me type in sketch

In void setup or void loop

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 »

Code: Select all

motor.setSpeed(speed)
or

Code: Select all

myMotor->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 »

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 »

Code above, i use to control motor stepper with 2 button (start and stop).

Now, i want to drive speed motor use potensiometer,

How the code in the sketch

Thanks

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 »

Help me please, i'm confused

User avatar
adafruit_support_bill
 
Posts: 88093
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 »

Please see this post: http://forums.adafruit.com/posting.php? ... 8#pr304524
That code will control the motor with the potentiometer. But you need to integrate it with your existing code. We cannot do this for you.

You need to make the motor variable name match the name you are already using in your code.
If the motor name in the current version of your code is 'myMotor', then change 'motor' to 'myMotor'.

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, but it does not work

No effect when the motor speed potentiometer is rotated

I want to change my plan before, I use the above code to move a stepper motor manually using a 2 button (start and stop) where the rotation of the motor can be determined during the stop button has not been pressed

now, I want to work the motors automatically by adding 1 button where the motor just spins round once then stop

Give me a clue

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 »

How to declare a new button in the code. if I want to work the motors automatically by adding 1 button where the motor just spins round once then stop

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 »

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 »

I've tried to add the 1 button again and declare it, but I was not able to make the motor move one lap and then stop when the button is pressed,

thanks, give me a clue

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 »

Code: Select all

void loop() {
  
  if (digitalRead(buttonPin1) == LOW)   //if start button is pressed
  {
    runState = true;      //start running stepper
  }
  
  if (digitalRead(buttonPin1) == LOW)   //if stop button is pressed
  {
    runState = false;    //stop running stepper
  }
  
  if (runState)  //if the stepper should be running
  {
    myMotor->step(1, ???????????????);    //advance it one step.
  }
}
how to make a code so that the motor moves one lap then stop if button is pressed once

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 »

If i using ADC (Analog To Digital Converter) how to declare the speed,

Please give me the example

User avatar
adafruit_support_bill
 
Posts: 88093
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 »

If i using ADC (Analog To Digital Converter) how to declare the speed,

Please give me the example
What do you have connected to the ADC? We have already given you example code for a potentiometer connected to an analog pin.

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 »

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
  
}

void loop() {
int speed = map(analogRead(0), 0, 1024, 0, 255);  // Map the potentiometer input intoa motor speed
  myMotor->setSpeed(speed);
  
  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.
  }
}

    

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

Return to “Arduino Shields from Adafruit”