Control steppers motors stop and move

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
yan101711
 
Posts: 8
Joined: Wed Dec 07, 2011 10:19 am

Control steppers motors stop and move

Post by yan101711 »

I was trying to do something like CNC machine(but not actually write words or drawing patten, just moving the pen by motors and draw some lines.)
I am trying to use face detection(by processing) and distance sensor, that means if someone face's detected, and the distance is close enough, the motors will move and make the pen drawing. And when there is no one face detect(let's assume that people finish watching the machine, and he/she walk away), the motors stop. The motor shield that I am using is this(http://www.ladyada.net/make/mshield/index.html). And my stepper motors are unipolar.

The distance sensor and the face detection parts I have already done, the problem is I don't know how to control the stepper motors stop and start and stop and start, looping. Now my coding can just let the motor run, but it won't stop until I cut off the power supply.

Here is my code, please help me:(

The main problem is in the void loop() part.

Code: Select all

#include <AFMotor.h>

int incoming;
unsigned int EchoPin = 2;
unsigned int TrigPin = 13;
unsigned long Time_Echo_us = 0;
unsigned long Len_mm = 0;
AF_Stepper motor(400, 1);




void setup(){
  Serial.begin(9600);
  pinMode(EchoPin, INPUT);
  pinMode(TrigPin, OUTPUT);

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


void loop(){

  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(50);
  digitalWrite(TrigPin, LOW);
  Time_Echo_us = pulseIn(EchoPin, HIGH);
  if((Time_Echo_us < 60000) && (Time_Echo_us > 1))
  {
    Len_mm = (Time_Echo_us * 34/100)/2;
  }

  if (Serial.available()>0){
    incoming = Serial.read();
    
    //here is the main problem
    //incoming mean the value that processing passed to arduino,
    //and 'H' mean someone face has been detected,
    //and the Len_mm is mean the distance sensor value.
   //I can do the motor.step here, but I just don't know how to make it stop...

    if(incoming == 'H' && Len_mm < 400){
      motor.step(4000, FORWARD, DOUBLE); 
    }


    if(incoming == 'L'){
     //suppose here is let the motor stop, 
     //but I don't know what should I type in
    }


  }

}


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

Re: Control steppers motors stop and move

Post by adafruit_support_bill »

How does the processing side work? Does it send "H" jsut once? Or continuously as long as a face is detected?

Code: Select all

          motor.step(4000, FORWARD, DOUBLE);
This will go 4000 steps each time it sees an "H". You might try just one step at a time:

Code: Select all

          motor.step(1, FORWARD, DOUBLE);

yan101711
 
Posts: 8
Joined: Wed Dec 07, 2011 10:19 am

Re: Control steppers motors stop and move

Post by yan101711 »

Thanks for help!!I can stop the motor when no face detected after change the code as you suggest:)

But I still have one problem that, when the stepper motor run, it go left right left right(ref to the video: http://www.youtube.com/watch?v=pVGssQRz ... e=youtu.be)...but it never go toward or backward..
I am using a 0.9degree unipolar stepper motor. It there any error the lead this result in the coding?
(this is the data sheet of the motor:http://www.alltronics.com/mas_assets/acrobat/28M053.pdf)


and here is the processing code for reference:)

Code: Select all

import hypermedia.video.*;
import processing.serial.*;
import java.awt.Rectangle;

OpenCV opencv;
Serial port;
boolean detect = false;
int moving = 0;

void setup() {
  size( 320, 240 );
  println(Serial.list());
  port = new Serial(this, Serial.list()[0], 9600);
  opencv = new OpenCV(this);
  opencv.capture( width, height );
  opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
}


void draw() {
  background(0);
  // grab a new frame
  // and convert to gray
  opencv.read();
  opencv.convert( GRAY );

  // proceed detection
  Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );

  // display the image
  image( opencv.image(), 0, 0 );

  // draw face area(s)
  noFill();
  stroke(255, 0, 0);

  for ( int i=0; i<faces.length; i++ ) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height ); 
    detect = true;
    port.write('H');
    moving += 1;
  } 
  for ( int i=0; i==faces.length; i++ ) { 
    detect=false;
    port.write('L');
  }
}

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

Re: Control steppers motors stop and move

Post by adafruit_support_bill »

It could be because you are using double-step mode. The library assumes that all moves begin on a full-step. Try increasing the number of steps to 2.
motor.step(2, FORWARD, DOUBLE);

yan101711
 
Posts: 8
Joined: Wed Dec 07, 2011 10:19 am

Re: Control steppers motors stop and move

Post by yan101711 »

It doesn't change when I change to 2 : (
I have try "motor.step(2, FORWARD, SINGLE); "
and "motor.step(1, FORWARD, SINGLE); "
and "motor.step(2, FORWARD, DOUBLE); "
But the results are all the same......

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

Re: Control steppers motors stop and move

Post by adafruit_support_bill »

Did it do this when you had 4000 steps?

yan101711
 
Posts: 8
Joined: Wed Dec 07, 2011 10:19 am

Re: Control steppers motors stop and move

Post by yan101711 »

yes..I have changed all the value but it doesn't change : (

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

Re: Control steppers motors stop and move

Post by adafruit_support_bill »

OK then, it is probably a wiring problem. You will need to change the wiring so that the coils are energized in the correct sequence. If you don't have a wiring diagram for you motor, check out some of the links here for how to reverse-engineer it: http://www.ladyada.net/make/mshield/resources.html

yan101711
 
Posts: 8
Joined: Wed Dec 07, 2011 10:19 am

Re: Control steppers motors stop and move

Post by yan101711 »

OMG you have remind me the wire part!!
I have change connection of different wire, and it works finally!!
Thank you so much!!!!

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

Re: Control steppers motors stop and move

Post by adafruit_support_bill »

Great! :D

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

Return to “Arduino Shields from Adafruit”