to send the serial value to the board I use the serial monitor interface of the arduino (for now). I am thinking about processing in a further development but for the sake of my learning curve i need to take it one easy modification at a time.
I need to control the engines so that each of the three moves one step at a time each time I send the corresponding letter and I need to be able to :
- change direction if I send the uppercase corresponding letter
- safestop everything in its tracks if i send the letter a
- move multiple motors combination contemporary
please advice me with some code i can read. The final idea is to modify the library into some kind of Easycnc.h and Easycnc.cpp so that everyone that will face this same challenge can make it easy and clean.
details are as follows (if i miss any kind of informations for you to be able to help me, bear with me I am a n00b, please feel free to ask I will provide):
arduino program version= 18
arduino board used = mega
i am forced to use a vmware ubuntu installation instead of the native gentoo because the rxtx-2 serial bug still affects me when I try to use
the mega board (not the duemilanove) and i need pins.
got the easydriver library for arduino here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1251509480:
Easydriver.cpp
/*
EasyDiver.cpp - Arduino Library for EasyDriver 3.0 stepper driver board from Sparkfun.com
Based on:
Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
Original library (0.1) by Tom Igoe.
Two-wire modifications (0.2) by Sebastian Gassner
Combination version (0.3) by Tom Igoe and David Mellis
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
I wanted This library to be a simple drop-in replacement for the standard stepper lib.
just change some defines and go!!
Maken "at" maken.org 8/28/09
*/
#include "WProgram.h"
#include "EasyDriver.h"
/*
* EasyDriver constructor.
* Sets Direction and Step Pins.
*/
Stepper::Stepper(int number_of_steps, int dir_pin, int step_pin)
{
this->step_number = 0; // which step the motor is on
this->speed = 0; // the motor speed, in revolutions per minute
this->direction = 0; // motor direction
this->last_step_time = 0; // time stamp in ms of the last step taken
this->number_of_steps = number_of_steps; // total number of steps for this motor
// Arduino pins for the motor control connection:
this->dir_pin = dir_pin;
this->step_pin = step_pin;
// setup the pins on the microcontroller:
pinMode(this->dir_pin, OUTPUT);
pinMode(this->step_pin, OUTPUT);
// When there are only 2 pins, set the other two to 0:
//this->motor_pin_3 = 0;
//this->motor_pin_4 = 0;
// pin_count is used by the stepMotor() method:
//this->pin_count = 2;
}
/*
Sets the speed in revs per minute
*/
void Stepper::setSpeed(long whatSpeed)
{
this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
}
/*
Moves the motor steps_to_move steps. If the number is negative,
the motor moves in the reverse direction.
*/
void Stepper::step(int steps_to_move)
{
int steps_left = abs(steps_to_move); // how many steps to take
// determine direction based on whether steps_to_mode is + or -:
if (steps_to_move > 0) {this->direction = 1;}
if (steps_to_move < 0) {this->direction = 0;}
// decrement the number of steps, moving one step each time:
while(steps_left > 0) {
// move only if the appropriate delay has passed:
if (millis() - this->last_step_time >= this->step_delay) {
// get the timeStamp of when you stepped:
this->last_step_time = millis();
// increment or decrement the step number,
// depending on direction:
if (this->direction == 1) {
this->step_number++;
if (this->step_number == this->number_of_steps) {
this->step_number = 0;
}
}
else {
if (this->step_number == 0) {
this->step_number = this->number_of_steps;
}
this->step_number--;
}
// decrement the steps left:
steps_left--;
// step the motor to step number 0, 1, 2, or 3:
stepMotor(this->direction);
}
}
}
/*
* Moves the motor forward or backwards.
*/
void Stepper::stepMotor(int thisDir)
{
digitalWrite(dir_pin, thisDir);
delayMicroseconds(100);
digitalWrite(step_pin, LOW);
delayMicroseconds(100);
digitalWrite(step_pin, HIGH);
delayMicroseconds(100);
}
/*
version() returns the version of the library:
*/
int Stepper::version(void)
{
return .01;
}
Easydriver.h
/*
EasyDriver.h - Arduino Library for EasyDriver 3.0 stepper driver board from Sparkfun.com
Based on:
Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4
Original library (0.1) by Tom Igoe.
Two-wire modifications (0.2) by Sebastian Gassner
Combination version (0.3) by Tom Igoe and David Mellis
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
I wanted This library to be a simple drop-in replacement for the standard stepper lib.
just change some defines and go!!
Makenb "at" gmail.com 8/28/09
*/
// ensure this library description is only included once
#ifndef EasyDriver_h
#define EasyDriver_h
// library interface description
class Stepper {
public:
// constructors:
Stepper(int number_of_steps, int dir_pin, int step_pin);
// speed setter method:
void setSpeed(long whatSpeed);
// mover method:
void step(int number_of_steps);
int version(void);
private:
void stepMotor(int this_step);
int direction; // Direction of rotation
int speed; // Speed in RPMs
unsigned long step_delay; // delay between steps, in ms, based on speed
int number_of_steps; // total number of steps this motor can take
// int pin_count; // whether you're driving the motor with 2 or 4 pins
int step_number; // which step the motor is on
// motor pin numbers:
int dir_pin;
int step_pin;
long last_step_time; // time stamp in ms of when the last step was taken
};
#endif
arduino code:
#include <Easydriver.h>
int x = 0;
int y = 0;
int z = 0;
char val = 'a';
void setup(){
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
Serial.begin(9600);
while (val == 'x') {
// while(x < 100){
// do something repetitive 4000 times
Stepper step1(1, 2, 3);
step1.setSpeed(100);
step1.step(1);
// x++;
// }
Serial.print('x');
Serial.end();
}
while (val == 'y') {
// while(y < 100){
// do something repetitive 4000 times
Stepper step1(1, 4, 5);
step1.setSpeed(100);
step1.step(1);
//y++;
//}
Serial.print('y');
Serial.end();
}
while (val == 'z' ) {
//while(z < 100){
// do something repetitive 4000 times
Stepper step1(1, 6, 7);
step1.setSpeed(100);
step1.step(1);
// }
Serial.print('z');
Serial.end();
}
}
}