Using a Force sensor to a Motorshield

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

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
ciorizzo
 
Posts: 14
Joined: Thu Mar 27, 2014 4:50 pm

Using a Force sensor to a Motorshield

Post by ciorizzo »

I have a motor shield connected to my UNO. I already have my stepper motor working. Now i want to use a force sensor to control the stepper motor so whenever its pushed, it will turn. I just purchased the adafruit square force sensor from the website. How can i do this? and if its possible can you help me with the code.

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

Re: Using a Force sensor to a Motorshield

Post by adafruit_support_bill »

It is always best to take these one step at a time. I'd start with the basic test circuit and code here: http://learn.adafruit.com/force-sensiti ... ing-an-fsr

Once you have verified that it is connected and working properly, we can show you how to add in the motor control code.

ciorizzo
 
Posts: 14
Joined: Thu Mar 27, 2014 4:50 pm

Re: Using a Force sensor to a Motorshield

Post by ciorizzo »

OK I got the sensor to work with just the UNO. I took some pics if you want to see it. Showing the wiring and serial monitor results for the force. . Now I would like to connect the sensor to my motorshield. Using the force sensor to trigger the stepper motor. How can i do this and maybe help me with the code.

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

Re: Using a Force sensor to a Motorshield

Post by adafruit_support_bill »

OK. Now you need to combine your stepper code with the sensor code:

You will need to add these lines to the top of your sketch to include the libraries and define a stepper motor on port 2 (same as in the StepperTest example)

Code: Select all

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

// 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);
In your setup() function, you will need to add some code to initialize the shield and set the motor speed:

Code: Select all

  AFMS.begin();  // create with the default frequency 1.6KHz
  myMotor->setSpeed(10);  // 10 rpm   
Then you can add some code to your loop() function run the motor when the sensor is pressed.

Code: Select all

void loop(void)
{
  fsrReading = analogRead(fsrAnalogPin);
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
 
  if (fsrReading > 500)
  {
     myMotor->step(100, FORWARD, DOUBLE); 
  }
  else
  {
     delay(100);
  }
}

ciorizzo
 
Posts: 14
Joined: Thu Mar 27, 2014 4:50 pm

Re: Using a Force sensor to a Motorshield

Post by ciorizzo »

ok everything is hooked up and I put the code in

Code: Select all

    #include <Wire.h>
    #include <Adafruit_MotorShield.h>
    #include "utility/Adafruit_PWMServoDriver.h"

    // Create the motor shield object with the default I2C address
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();

    // 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);
      AFMS.begin();  // create with the default frequency 1.6KHz
      myMotor->setSpeed(10);  // 10 rpm   
void loop(void)
{
  fsrReading = analogRead(fsrAnalogPin);
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
 
  if (fsrReading > 500)
  {
     myMotor->step(100, FORWARD, DOUBLE);
  }
  else
  {
     delay(100);
  }
}
these were my errors

sketch_mar30b:11: error: expected constructor, destructor, or type conversion before '.' token
sketch_mar30b:12: error: expected constructor, destructor, or type conversion before '->' token
sketch_mar30b.ino: In function 'void loop()':
sketch_mar30b:15: error: 'fsrReading' was not declared in this scope
sketch_mar30b:15: error: 'fsrAnalogPin' was not declared in this scope


Do I have to set the fsrReading to A0 ? well I tried that and still got a error
Last edited by adafruit_support_bill on Mon Mar 31, 2014 5:46 am, edited 1 time in total.
Reason: please use the code button when submitting code. press [Code], then paste your code between the [code] [/code] tags.

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

Re: Using a Force sensor to a Motorshield

Post by adafruit_support_bill »

Where is your setup() function?
In your setup() function, you will need to add some code to initialize the shield and set the motor speed:

CODE: SELECT ALL
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(10); // 10 rpm

ciorizzo
 
Posts: 14
Joined: Thu Mar 27, 2014 4:50 pm

Re: Using a Force sensor to a Motorshield

Post by ciorizzo »

Ok great I got it to communicate with the sensor. thank you. Now I would like to finish this senior project. I'm doing a pill dispenser for Alzheimer's patients so they wont forget to take there medication and or won't take to many. Using the stepper motor to rotate a disk to drop the medication.I would like for someone to be notified when that sensor goes off. what can I do ? email or maybe text for the notification. I notice the ethernet shield but with my motor shield , I'm not sure if its possible. If I can't do that option 2 maybe set a alarm to go off when the sensor is forced ( or alarm before the medication needs to be taken to remind them) I know this last part will be the icing on the cake if i accomplish this. Anything from the adafruit website that I would need for this ill buy.

here the code that I fixed with your help

Code: Select all

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.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);

// 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);
int value=0;
int fsrReading;
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  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(void)
    {
      fsrReading = analogRead(A0);
      Serial.print("Analog reading = ");
      Serial.println(fsrReading);
    
      if (fsrReading > 500)
      {
         myMotor->step(100, FORWARD, DOUBLE);
      }
      else
      {
         delay(100);
      }
    }
Last edited by adafruit_support_bill on Mon Mar 31, 2014 4:47 pm, edited 1 time in total.
Reason: please use the code button when submitting code. press [Code], then paste your code between the [code] [/code] tags.

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

Re: Using a Force sensor to a Motorshield

Post by adafruit_support_bill »

Simply sounding an attached alarm would be pretty straightforward. If you want to send email, there is the Ethernet Shield or the CC3000 WiFi shield. People have had difficulty stacking the Ethernet shield, so I would recommend the CC3000: http://www.adafruit.com/index.php?main_ ... 000+shield

To stack a shield, you would need to build one of them with stacking headers. http://www.adafruit.com/products/85
Since your motor shield is already built, it would have to be the second shield. If you want to stack the CC3000 under a motor shield, you should go with the external antenna version.
http://www.adafruit.com/products/1534
http://www.adafruit.com/products/852
http://www.adafruit.com/products/944

ciorizzo
 
Posts: 14
Joined: Thu Mar 27, 2014 4:50 pm

Re: Using a Force sensor to a Motorshield

Post by ciorizzo »

Adafruit CC3000 WiFi Shield with uFL Connector for Ext Antenna

Ok because I have the blue ports ( M1,M2) two by the jumper and (M3,M4) be in the way.

would the two library's conflict ?

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

Re: Using a Force sensor to a Motorshield

Post by adafruit_support_bill »

There is not interference with the motor terminals because the motor shield would be on top. There should be no conflict between the libraries. Memory may be tight because the CC3000 library is relatively large. But your motor code is fairly simple, so it should not be a problem.

ciorizzo
 
Posts: 14
Joined: Thu Mar 27, 2014 4:50 pm

Re: Using a Force sensor to a Motorshield

Post by ciorizzo »

OK will order it. If I have any trouble with the code , would you mind if i ask for your assistance again? by the way, you helped out tremendously. Thank you

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

Re: Using a Force sensor to a Motorshield

Post by adafruit_support_bill »

You are welcome. We are here to assist. :D

ciorizzo
 
Posts: 14
Joined: Thu Mar 27, 2014 4:50 pm

Re: Using a Force sensor to a Motorshield

Post by ciorizzo »

OK just got the CC3000 Shield in the mail and soldered it to my UNO. Downloaded the library and its a little overwhelming. A lot of code in the examples.Wondering if you can put me in the right direction to send a email when my force sensor is activated.

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

Re: Using a Force sensor to a Motorshield

Post by adafruit_support_bill »


ciorizzo
 
Posts: 14
Joined: Thu Mar 27, 2014 4:50 pm

Re: Using a Force sensor to a Motorshield

Post by ciorizzo »

OK went to save the wireless garden library. Everything went find but when I open up my arduino this message appeared " The library " wireless gardening arduino master" cannot be used. Library names must contain only basic letters and numbers. ( ASCII only and no spaces, and it cannot start with a number"

I change the name to and the message still appeared. But when I (x) the message out and continue to open my arduino it does appear in the examples next to my other ones ( motorshield, CC3000 etc... )

I'll try the 2nd link that you posted and see how that goes.

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

Return to “Arduino Shields from Adafruit”