Motor shield + UNO. Stepper motor +photomicrosensor script

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Motor shield + UNO. Stepper motor +photomicrosensor script

Postby oinkoink12 » Mon Mar 12, 2012 11:03 am

Hello,

I want to create a script for a steppermotor that finds it 0point when the Photomicrosensor is activated.
I tried combinding 2 existing working scripts but i failed horribly.

Can any of you point me in the right direction?

Code: Select all
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

#define STEPS 2000

// Connect a stepper motor with variable steps per revolution
// to motor port #2 (M3 and M4)
AF_Stepper motor(STEPS, 2);

int previous = 0;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(30);  // 30 rpm   
}

void loop()
{
  Serial.println("Single coil steps");
  motor.step(100, FORWARD, SINGLE);
  motor.step(100, BACKWARD, SINGLE);

  // get the sensor value
  int val = analogRead(0);

  // move a number of steps equal to the change in the
  // sensor reading
  AF_Stepper.step(val - previous);

  // remember the previous value of the sensor
  previous = val;
}
oinkoink12
 
Posts: 8
Joined: Mon Mar 12, 2012 10:42 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby adafruit_support_bill » Mon Mar 12, 2012 11:35 am

Code: Select all
      motor.step(100, FORWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE);


These two lines go one step forward and one step back. What are you trying to accomplish here?

Code: Select all
      // get the sensor value
      int val = analogRead(0);


What kind of sensor is this? What is the range of outputs?

Code: Select all
      AF_Stepper.step(val - previous);


This is not valid syntax. You need to use:
Code: Select all
      motor.step(val - previous);
User avatar
adafruit_support_bill
 
Posts: 16004
Joined: Sat Feb 07, 2009 9:11 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby oinkoink12 » Tue Mar 13, 2012 2:33 am

I tried to accomplish 100 steps forward, 100 steps backwards, and that worked in the previous script.

The kind of sensor I need to use is http://www.omron.com/ecb/products/pdf/en-ee_sv3_series.pdf

When I change the script to what you suggested i get

Code: Select all
StepperTestX.cpp: In function 'void loop()':
StepperTestX:29: error: no matching function for call to 'AF_Stepper::step(int)'
C:\Users\simteq\Desktop\Tims Rommelhoek\arduino\arduino-1.0\libraries\Simteq/AFMotor.h:90: note: candidates are: void AF_Stepper::step(uint16_t, uint8_t, uint8_t)
oinkoink12
 
Posts: 8
Joined: Mon Mar 12, 2012 10:42 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby oinkoink12 » Tue Mar 13, 2012 4:25 am

Ok, I started over again!

This works:

Code: Select all
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

// Connect a stepper motor with 1000 steps per revolution (0,36 degree)
// to motor port #2 (M3 and M4)
AF_Stepper motor(1000, 2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(20);  // 20 rpm   
}

void loop() {
  Serial.println("Single coil steps");
  motor.step(1000, FORWARD, SINGLE);
  motor.step(1000, BACKWARD, SINGLE);

}


This makes my motor go 1 complete circle forwards and 1 complete circle backwards, over and over again.
What I want is my photomicrosensor (which I connected to Analog0 on the Shield) that it recognizes my 0 point on the sensor and that I can reset my forward/backward based on the position of the 0.

Example:

0 point on sensor is on step 100 so it can go back 100 steps and 900 steps forwards.

I think I need to work with this?

Code: Select all
int sensorPin = A0;    // select the input pin for the photomicrosensor
int sensorValue = 0;  // variable to store the value coming from the sensor

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);   


I will now try this:

Code: Select all
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

// Connect a stepper motor with 1000 steps per revolution (0,36 degree)
// to motor port #2 (M3 and M4)
AF_Stepper motor(1000, 2);

int sensorPin = A0;    // select the input pin for the photomicrosensor
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(20);  // 20 rpm   
}

void loop() {
  Serial.println("Single coil steps");
  motor.step(1000, FORWARD, SINGLE);
  motor.step(1000, BACKWARD, SINGLE);
 
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue, DEC);
}
oinkoink12
 
Posts: 8
Joined: Mon Mar 12, 2012 10:42 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby adafruit_support_bill » Tue Mar 13, 2012 4:55 am

Your sensor is a digital sensor. It senses when the gap is interrupted. I assume that you have some sort of slotted wheel or "flag" attached to the stepper shaft? To detect when the gap is interrupted, you should be using "digitalRead" not "analogRead" to read it.

Typical useage of these with a stepper involves single-stepping toward where "home" should be and stopping when the sensor detects the home flag. For faster performance, you can do a multi-step move to cover most of the distance, the switch to single-steps when you are close to home.
User avatar
adafruit_support_bill
 
Posts: 16004
Joined: Sat Feb 07, 2009 9:11 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby oinkoink12 » Tue Mar 13, 2012 5:17 am

adafruit_support wrote:Your sensor is a digital sensor. It senses when the gap is interrupted. I assume that you have some sort of slotted wheel or "flag" attached to the stepper shaft? To detect when the gap is interrupted, you should be using "digitalRead" not "analogRead" to read it.

Typical useage of these with a stepper involves single-stepping toward where "home" should be and stopping when the sensor detects the home flag. For faster performance, you can do a multi-step move to cover most of the distance, the switch to single-steps when you are close to home.

When I had it on analog it gave me "1023" as result (thats the steps for a full circle) when i switched to digital it gave me "1"
oinkoink12
 
Posts: 8
Joined: Mon Mar 12, 2012 10:42 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby adafruit_support_bill » Tue Mar 13, 2012 5:49 am

When I had it on analog it gave me "1023" as result (thats the steps for a full circle)

No. That sensor does not count steps. 1023 is the full-scale analog reading of 5v - which is equivalent to logic-level '1' or TRUE.
User avatar
adafruit_support_bill
 
Posts: 16004
Joined: Sat Feb 07, 2009 9:11 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby oinkoink12 » Tue Mar 13, 2012 6:34 am

adafruit_support wrote:
When I had it on analog it gave me "1023" as result (thats the steps for a full circle)

No. That sensor does not count steps. 1023 is the full-scale analog reading of 5v - which is equivalent to logic-level '1' or TRUE.


Your right :) (you prolly allready knew that)

Ok, so i change my analogRead to digitalRead. It gives an output of "1" when it arrives there.

I tried this but it just stops after the 2000 steps. How can I get it to stop at the sensorpoint?

Code: Select all
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

// Connect a stepper motor with 1000 steps per revolution (0,36 degree)
// to motor port #2 (M3 and M4)
AF_Stepper motor(1023, 2);

int sensorPin = A0;    // select the input pin for the photomicrosensor
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(20);  // 20 rpm   
  Serial.println("Single coil steps");
  motor.step(2000, FORWARD, SINGLE);
  //motor.step(1023, BACKWARD, SINGLE);
 
  sensorValue = digitalRead(sensorPin);
  Serial.println(sensorValue, DEC);
      if (sensorValue > 0)
{
  motor.setSpeed(0);
}
}

void loop() {


}
oinkoink12
 
Posts: 8
Joined: Mon Mar 12, 2012 10:42 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby adafruit_support_bill » Tue Mar 13, 2012 6:46 am

Assuming that your sensor reads '1' when the motor is in the 'home' position:

Code: Select all
     int homeFlag = digitalRead(sensorPin)
     while (!homeFlag)  // step until home
     {
       motor.step(1, BACKWARD, SINGLE); 
     }
User avatar
adafruit_support_bill
 
Posts: 16004
Joined: Sat Feb 07, 2009 9:11 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby oinkoink12 » Tue Mar 13, 2012 7:05 am

adafruit_support wrote:Assuming that your sensor reads '1' when the motor is in the 'home' position:

Code: Select all
     int homeFlag = digitalRead(sensorPin)
     while (!homeFlag)  // step until home
     {
       motor.step(1, BACKWARD, SINGLE); 
     }


It seems my sensor works the other way around, its a constant 1 and it read '0" when its in the home position.
So that solution doesnt work for me :(
oinkoink12
 
Posts: 8
Joined: Mon Mar 12, 2012 10:42 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby adafruit_support_bill » Tue Mar 13, 2012 7:35 am

If the sensor logic is inverted, you need to invert the logic in the while.

Code: Select all
while (homeFlag)  // step until home
User avatar
adafruit_support_bill
 
Posts: 16004
Joined: Sat Feb 07, 2009 9:11 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby oinkoink12 » Tue Mar 13, 2012 7:45 am

adafruit_support wrote:If the sensor logic is inverted, you need to invert the logic in the while.

Code: Select all
while (homeFlag)  // step until home

From learning all these new things my logic is allready inverted today :) :) Thanks for your patient help

My code now is this:
Code: Select all
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

// Connect a stepper motor with 1000 steps per revolution (0,36 degree)
// to motor port #2 (M3 and M4)
AF_Stepper motor(1023, 2);

int sensorPin = A0;    // select the input pin for the photomicrosensor
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(20);  // 20 rpm   
  Serial.println("Single coil steps");
  motor.step(200, FORWARD, SINGLE);
  //motor.step(1023, BACKWARD, SINGLE);
 
  sensorValue = digitalRead(sensorPin);
  Serial.println(sensorValue, DEC);

}

void loop() {
     int homeFlag = digitalRead(sensorPin);
     while (homeFlag)  // step until home
     {
       motor.step(1, BACKWARD, SINGLE); 
     }
      if (sensorValue < 1)
{
  motor.setSpeed(0);
}
}


It goes 200 steps forward, then gives a 1 as output and then goes backwards forever. It seems to not check the sensor every step. What could that be?
oinkoink12
 
Posts: 8
Joined: Mon Mar 12, 2012 10:42 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby adafruit_support_bill » Tue Mar 13, 2012 7:52 am

My apologies. :oops: Not enough coffee yet this morning.

You need to have the sensor read as part of the while loop. Otherwise it will never change!
Code: Select all
     while (digitalRead(sensorPin))  // step until home
     {
       motor.step(1, BACKWARD, SINGLE);
     }
User avatar
adafruit_support_bill
 
Posts: 16004
Joined: Sat Feb 07, 2009 9:11 am

Re: Motor shield + UNO. Stepper motor +photomicrosensor script

Postby oinkoink12 » Tue Mar 13, 2012 8:05 am

adafruit_support wrote:My apologies. :oops: Not enough coffee yet this morning.

You need to have the sensor read as part of the while loop. Otherwise it will never change!
Code: Select all
     while (digitalRead(sensorPin))  // step until home
     {
       motor.step(1, BACKWARD, SINGLE);
     }


This works!
Tomorrow I will proceed on the next steps!
oinkoink12
 
Posts: 8
Joined: Mon Mar 12, 2012 10:42 am


Return to General Project help

Who is online

Users browsing this forum: Google [Bot] and 10 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [102]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]