Control stepper with motor shield and Processing

I want to control a stepper motor for a project using a stand alone app created with Processing. I have an arduino duemilanove, motor shield and a 4 wire bipolar stepper motor. I have it running with an Arduino sketch, but can't figure out how to run it from Processing. Can someone help me out with this. I've searched all over, but maybe I'm just too new to this to understand what is involved.

Thanks,

Mark

Start with telling us what you CAN do. Can you program the Arduino to control the stepper motor? That is, can you make the stepper motor step one whole rotation, with a small delay between each step? Can you make the stepper motor step one whole rotation the other way?

Once you can do that, having Processing send the Arduino commands to step the motor one way or the other some number of steps should be easy.

It all depends on what you currently can do.

Hi,

I have the stepper working OK with the Arduino software. It is connected to a Motor Shield. Here is the code that I use for one of the sketches:

#include <AFMotor.h>
AF_Stepper motor(400, 2);
void setup() {
  Serial.begin(9600);
  motor.setSpeed(150);
  motor.step(936, BACKWARD, DOUBLE); 
}
void loop() {
}

What I'm having problems with is figuring out how to operate the stepper from a Processing sketch. I'd like to create a program with Processing as a stand alone executable so that I could just click a couple of buttons to go forward or backward the set number of steps. From what I can tell, I'll need to use serial communication between the Processing sketch and the Arduino/Motor Shield, but I can't figure out the right coding.

Thanks,

Mark

First, you need to define a command set. For instance, you might want commands to set the direction, to set the speed, and to actually step the motor. You might have:

B (backwards)
F (forewards)
R xxx (rpm)
S xxx (step)

You need delimiters for the commands, so the Arduino knows when a command starts and ends. For instance, the Processing sketch might send >B< to tell the Arduino that the next motion command is to make the motor go backwards, followed by >R150< to set the speed, and then >S900< to step 900 times.

On the Arduino side, you'd use Serial.begin(), Serial.available(), and Serial.read to read the serial data. Collect the data when you see a > and stop collecting when you see a <.

The amount of data to read will be defined by the first letter. You only need to store one character and one integer, using a scheme like this.

The use of the serial port in a Processing sketch is pretty simple, once you know which port to talk to. The examples in Processing will show you how to figure that out.

Then, use begin and print to enable and perform serial communication.

If you run into trouble, come on back.

Paul, thanks for the feedback! I've been reading your post and Igoe's examples and think I finally figured out the concept of serial communication. I've got some code working to simply run the stepper briefly using Processing. Problem is that I can't get it to stop advancing unless I hit the reset button. It just keeps on repeating the steps.

In Arduino:

#include <AFMotor.h>

AF_Stepper motor(400, 2);

int i = 0;

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  motor.setSpeed(150);
  establishContact();  // send a byte to establish contact until receiver responds 
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    motor.step(936, BACKWARD, DOUBLE);
    delay(1000);  
  }
  else {
        motor.step(0, BACKWARD, DOUBLE);
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A', BYTE);   // send a capital A
    delay(300);
  }
}

In Processing:

// Processing sketch to run SetFilterWheelAdvance in Arduino

import processing.serial.*;

Serial myPort;                       // The serial port
int i = 1;

void setup() {
  println(Serial.list());
  String portName = Serial.list()[1];
  println(portName);
  myPort = new Serial(this, portName, 9600);
}

void draw() {
}

void serialEvent(Serial myPort) {
  // Send a capital A to advance wheel
  myPort.write('A');
}

Once I get this basic stuff working, I'll want to use more of your tips and use Processing to send different kinds of commands to control the stepper in various ways.

Thanks,

Mark

Serial data is like mail. Let me explain what is happening using that analogy.

In the Arduino sketch, you send an 'A' (some mail) to the processing sketch. The processing sketch sees that it got mail, and sends some mail back.

Then, the Arduino checks to see if it has mail. It does, so, without removing the mail from the mailbox, turns the motor on, and waits while it spins for one second.

Then, loop ends, and starts over. The Arduino looks to see if it has mail. It does, so, without removing the mail from the mailbox, turns the motor on, and waits while it spins for one second.

It repeats this pattern until you get tired.

The Serial.read() function removes the first piece of mail from the mailbox, reducing the number of pieces left in the mailbox. If you were to actually call this function, eventually all the mail in the mailbox would be removed, and the motor would stop.

Actually, it would happen pretty quickly, since there is only one piece of mail in your mailbox.

Thanks. I got it working!

Mark