Control a RepStrap with Processing

 by marc.cryan
Contest WinnerFeatured

Step 9: Making it a little better


As I make improvements I am going to add steps to the end of this instructable. 

10-11-09

The previus code tracks the ball pretty well but the steppers are driven in a very rocky way.

In this updated code, the Proccessing sketch only sends a signal to the Arduino when there is a change in direction.  So the stepper motors just run in whatever direction they were last set to, until they are told to change.    This allows the motors to run alot faster and smoother. 

But there is still somthing a bit off.  The Arduino seems to miss some of the signals, so the RepStrap ends up with the X-Y stage pushed all the way to one side. 

I will try to fix this. 

The Arduino and Processing code is below.  I have added more comments to help keep things clear.


/////////////////////////////////////////////////
// Arduino code:

// Read data from the serial
//Use data to determine direction of X and Y steppers


#define XstepPin 10
#define XdirPin 7

#define YstepPin 2
#define YdirPin 3

int val; // Data received from the serial port

void setup() {

pinMode(XstepPin, OUTPUT);
pinMode(XdirPin, OUTPUT);
pinMode(YstepPin, OUTPUT);
pinMode(YdirPin, OUTPUT);
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
  int dirX;
  int dirY;
if (Serial.available()) { // If data is available to read,
   val = Serial.read(); // then read it and store value in val
}

if (val == 'H'){        //set the direction of X and Y based on data sent from Processing
    dirX = HIGH;
}
if (val == 'L'){
  dirX = LOW;
}

if (val == 'h'){
    dirY = HIGH;
    }
if (val == 'l'){
    dirY = LOW;
    }

digitalWrite(XdirPin,dirX);    //set X direction
digitalWrite(YdirPin,dirY);    //set Y direction
digitalWrite(XstepPin,HIGH);      //take steps
digitalWrite(YstepPin,HIGH);     
delayMicroseconds(2);
digitalWrite(XstepPin,LOW);
digitalWrite(YstepPin,LOW);
delayMicroseconds(2);


delayMicroseconds(1000); // <<<<<< USE TO CHANGE SPEED <<<<<<<<
}

///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
//Processing Code:
//This outputs the motion of the bouncing ball to the RepRap X and Y axis

import processing.serial.*;

Serial myPort; // Create object from Serial class
int val;

float x= 50.0;
float y = 50.0;
float speedX = 1.0;
float speedY= .4;
float radius = 15.0;
int directionX = 1;
int directionY = 1;
int old_directionX = 1;
int old_directionY = 1;

void setup(){
size(100, 100);
smooth();
noStroke();
ellipseMode(RADIUS);

String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}

void draw(){
fill(0,12);
rect(0,0,width, height);
fill(255);
ellipse(x, y, radius, radius);

x += speedX * directionX;
if((x>width-radius) || (x < radius)) {  //change X direction if the ball hits the side of the box
directionX = -directionX;
}

y += speedY * directionY;
if((y>height-radius) || (y < radius)) {  //change Y direction if the ball hits the side of the box
directionY = -directionY;
}


 
if ((directionX != old_directionX) && (directionX == 1)){ //if X direction changed print H
myPort.write('H');
print('H');
delay(100);
}

if ((directionX != old_directionX) && (directionX == -1)){ //if X direction changed print h
myPort.write('h');
print('h');
delay(100);
}

if ((directionY != old_directionY) && (directionY == 1)){ //if Y direction changed print L
myPort.write('L');
print('L');
delay(100);
}

if ((directionY != old_directionY) && (directionY == -1)){ //if Y direction changed print l
myPort.write('l');
print('l');
delay(100);
}

old_directionX = directionX ;  //stores the directions we just used, as the old direction
old_directionY= directionY;

//delay(100); //uncomment to slow the whole thing down for troubleshooting
}






 
Remove these adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!