• 0

UNO32 motor shield buttons


Question

I am new to all this.  I want to run two dc motors but I want to start them after I press button btn1 and stop after btn2 is pressed.

 

This is my first try.

 

#include <MotorShield.h>     
// the setup routine runs once when you press reset:
void setup()
{
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop()
{
  // read the input pin:
  int buttonState = readButton(btn1);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

 

it doesn't like bnt or readButton.

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Hi joesanford,
 
Welcome to our forum! 
 
The MotorShield library has a specific set of functions that it has available to use. Unfortunately, we do not have any documentation specific towards this library, so I'll be sure to amend this in the near future.
 
But back to your question. The reason that MPIDE is not liking readButton is because the MotorShield library has its own set of functions that it uses. If we looked through the MotorShield.cpp library, we would see that one of the functions that it uses a function called "readbuttons" to read the state of the two buttons through I2C. This function passes the two measured values of button 1 and button 2 (respectively) to the two global variables that will need to be declared in your sketch. I'll post a snippet of code later on in this post with comments so you can see what I'm referring to.
 
As for the other part of your question, the reason that your code didn't like btn1 is because it was not initialized as a variable anywhere in your code. The library (at least in this particular library) does not initialize the btn1 for you, despite it being labeled on the Motor Shield as "btn1". This is because there is a strong chance that somebody who is using the library might want to call it a different name besides "btn1" so it is up to the user to initialize the variable themselves, effectively allowing them to name it themselves.
 
Here is a bit of example code that I modified from the example sketch that comes with the MotorShield library download.

 

--Beginning of the code (since it apparently didn't stay in the quote block) --

 

 

//The required libraries for using the MotorShield
#include <Wire.h> 
#include <MotorShield.h>
 
//Needed constructor for the MotorShield class
MotorShield example; //this instance of the Motorshield class I decided to call example
 
//Global variables
bool btn1;      // button 1 initialization
bool btn2;      // button 2 initialization
 
 
//setup function
void setup()
{
  Serial.begin(9600);    //starting up the serial monitor
  example.begin();       //needed function to start the I2C interface on the motor shield
}//END of setup()
 
void loop()
{
  //the readButtons function changes the values of btn1 and btn2 to match their measured state
  example.readButtons(&btn1, &btn2);        //a value of 1 means the button is pressed, a 0 means the button is not pressed
 
  Serial.print("The state of btn1 is: ");
  Serial.println(btn1);
  Serial.print("The state of btn2 is: ");
  Serial.println(btn2);
  
  if(btn1 == 1){
    //code to turn on the motors would go here
    //see more of the post for more detail
  }
  
  if(btn2 == 1){
    //code to turn off the motors would go here
  }
  
  delay(1);    //a small delay
 
}//END of loop()
 

--End of the code--

 

 

As for the code to drive the motors, unfortunately it seems that the person who wrote the library did not actually get around to write any code to drive the motors... not sure what they were thinking. This will be another thing that I will need to amend for this library.
 
The reference manual for the Motor Shield does talk a bit about how to drive the motors on page 3.
 
I'd also recommend checking out the Digilent Learn Modules if you would like a structured approach to using MPIDE if you're interested.
 
Please let me know if you have any more questions.
 
Thanks,
JColvin

Link to post
Share on other sites
  • 0

oh, oh, I guess I got some files misplaced?   My compile results:

 

In file included from C:UsersjoeDocumentsArduinolibrariesWireutilitytwi.c:23:0:
C:ChipKitmpide-0023-windows-20140821mpide-0023-windows-20140821hardwarepic32corespic32/avr/io.h:4:2: error: #error ******** This sketch or library uses AVR-specific code that may not work with the chipKIT platform. See this forum for more information on porting code to chipKIT [www.chipkit.org/forum/viewforum.php?f=7] ********
In file included from C:UsersjoeDocumentsArduinolibrariesWireutilitytwi.c:24:0:
C:ChipKitmpide-0023-windows-20140821mpide-0023-windows-20140821hardwarepic32corespic32/avr/interrupt.h:4:2: error: #error ******** This sketch or library uses AVR-specific code that may not work with the chipKIT platform. See this forum for more information on porting code to chipKIT [www.chipkit.org/forum/viewforum.php?f=7] ********
C:UsersjoeDocumentsArduinolibrariesWireutilitytwi.c:25:24: fatal error: compat/twi.h: No such file or directory
compilation terminated.
 

Link to post
Share on other sites
  • 0

Weird. It seems (from my personal guess) that its trying to pull the Wire library from a non-chipKIT Wire library source, since the code I posted compiles on my end without complaint. I'm personally not sure how to fix this off the top of my head, so I'll contact our support team about this and get back to you.

 

Thanks,

JColvin

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now