Thursday, October 1, 2009

Stepper demo



As a prelude to a full scale model of the video read head component of the project, we first needed to show that interaction between the computer and a stepper motor was possible.

In this setup, we use a the computer mouse to control the speed of a single six-wire stepper motor. the stepper motor is connected to an arduino motor shield which connects to my laptop through a USB port and powered by a 12 volt supply. On the arduino we are running a modified version of the motor shield demo code found here. On the laptop we are running a modified version of David A. Mellis's dimmer project developed in Processing.

The Processing code creates a small window with a color gradient, and as you move the mouse from one side to the other, you can decrease or increase the rate of the stepper motor's steps. On a side note, the program seems to fail if the mouse ever leaves the created window. The issue is in all likelihood a trivial fix, but it functions sufficiently as is to demonstrate the viability of this concept.

Laptop Code:

/* Processing code for this example */
// Dimmer - sends bytes over a serial port
// by David A. Mellis

import processing.serial.*;
Serial port;

void setup() {
size(256, 150);

println("Available serial ports:");
println(Serial.list());

// Uses the first port in this list (number 0). Change this to
// select the port corresponding to your Arduino board. The last
// parameter (e.g. 9600) is the speed of the communication. It
// has to correspond to the value passed to Serial.begin() in your
// Arduino sketch.
port = new Serial(this, Serial.list()[2], 9600);

// If you know the name of the port used by the Arduino board, you
// can specify it directly like this.
//port = new Serial(this, "COM1", 9600);
}

void draw() {
// draw a gradient from black to white
for (int i = 0; i < 256; i++) {
stroke(i);
line(i, 0, i, 150);
}

// write the current X-position of the mouse to the serial port as
// a single byte
port.write(mouseX);
}
*/



Arduino Code

/* by David A. Mellis's
* Modified by Nick Smith
*/

#include

AF_Stepper motor(48, 1);

void setup() {
Serial.begin(9600);
motor.setSpeed(10);
//motor.step(100, FORWARD, SINGLE);
motor.release();
delay(1000);

}

void loop() {
byte brightness;
if (Serial.available()) {
brightness = Serial.read();
motor.setSpeed(brightness / 5);
motor.step(10, FORWARD, INTERLEAVE);
//motor.step(100, BACKWARD, MICROSTEP);
Serial.flush();
}
}

No comments:

Post a Comment