Instructables

Step 21: 3D - Follow the Bouncing Ball (this includes Firmata)

Picture of 3D - Follow the Bouncing Ball (this includes Firmata)


Here is the 3D bouncing ball Processing  and Adruino code.

This will run X,Y,Z stepper motors.

The Arduino code follows, this is the  code from Step 13 with the Z axis added.


///////////////////////////////PROCESSING /////////////////////////////

 /////////////////// *********PROCESSING with FIRMATA*********

/**
 * Bounce_3D_0
 *
 * When the shape hits a boundry it reverses its direction.
 There are X,Y, Z bounds
 
 Direction is outputed -
 
     Processsing -> Firmata -> Arduino -> Drivers -> Steppers
 *
 *Modified for 3D
 
 
 NOTE:  0,0,0   - top left corner (facing the computer monitor)
        negative Z axis points 'into' the computer monitor
        positive Y axis points down
        positive X axis points to the right
 */
 
 import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

//Serial myPort;  // Create object from Serial class
      // Data received from the serial port
int ledPin = 13;
int XstepPin = 10;
int XdirPin = 7;
int YstepPin = 2;
int YdirPin = 3;
int ZstepPin = 19;
int ZdirPin = 18;
 
int size = 40;       // Width of the shape
float xpos, ypos, zpos;    // Starting position of shape  
float depth;

float xspeed = 2.5;  // Speed of the shape
float yspeed = 2;  // Speed of the shape
float zspeed = 3;

int xdirection = 1;  // Left or Right
int ydirection = 1;  // Top to Bottom
int zdirection = 1;  //front or back


void setup()
{
  size(200, 200, P3D);
  noStroke();
  frameRate(30);
  smooth();
  // Set the starting position of the shape
  xpos = width/2;
  ypos = height/2;
  zpos = -height/2; //note that Zaxis goes 'into' the screen
  depth = -height;
 
    //setup communication with Arduino
  arduino = new Arduino(this, Arduino.list()[0], 57600); // v1
  arduino.pinMode(ledPin, Arduino.OUTPUT);
  arduino.pinMode(XstepPin, Arduino.OUTPUT);
  arduino.pinMode(XdirPin, Arduino.OUTPUT);
  arduino.pinMode(YstepPin, Arduino.OUTPUT);
  arduino.pinMode(YdirPin, Arduino.OUTPUT);
  arduino.pinMode(ZstepPin, Arduino.OUTPUT);
  arduino.pinMode(ZdirPin, Arduino.OUTPUT);
}

void draw()
{
  background(102);
  lights(); 

//glass box                  //note: line(x1, y1, z1, x2, y2, z2)
stroke(255);
//back
line(0,0,depth,  width,0,depth);           
line(0,height,depth,  width,height,depth);
line(0,0,depth,  0,height,depth);
line(width,0,depth,  width,height,depth);
//corners
line(0,0,0,  0,0,depth);
line(0,height,0, 0,height,depth);
line(width,0,0,  width,0,depth);
line(width,height,0,  width,height,depth);

    
  // Update the position of the shape
  xpos = xpos + ( xspeed * xdirection );
  ypos = ypos + ( yspeed * ydirection );
  zpos = zpos + ( zspeed * zdirection );
 
  // Test to see if the shape exceeds the boundaries of the screen
  // If it does, reverse its direction by multiplying by -1
 
 
  if (xpos > width-size || xpos < 0) {
    xdirection *= -1;
  }
  if (ypos > height-size || ypos < 0) {
    ydirection *= -1;
  }
 
  if (zpos < -height-size || zpos > 0) {  //note that Zaxis goes 'into' the screen
    zdirection *= -1;
  }
 

  // Draw the shape

lights();
translate(xpos, ypos, zpos);

sphere(size);


  // CONTROLL THE STEPPER MOTORS
 
    //  X
  if (xdirection >= 0){
    arduino.digitalWrite(ledPin, Arduino.HIGH);             
    arduino.digitalWrite(XdirPin, Arduino.HIGH);
    arduino.digitalWrite(ZdirPin, Arduino.HIGH);
      }
      else{
        arduino.digitalWrite(ledPin, Arduino.LOW);             
        arduino.digitalWrite(XdirPin, Arduino.LOW);
        arduino.digitalWrite(ZdirPin, Arduino.LOW);
       }

     //  Y 
  if (ydirection >= 0){              
    arduino.digitalWrite(YdirPin, Arduino.HIGH);
      }
      else{          
        arduino.digitalWrite(YdirPin, Arduino.LOW);
       }
     //  Z   
if (zdirection >= 0){             
    arduino.digitalWrite(ZdirPin, Arduino.HIGH);
      }
      else{           
        arduino.digitalWrite(ZdirPin, Arduino.LOW);
       }
}




/////////////////////////////ARDUINO////////////////////////////////////////

// Wiring/Arduino code:// simmple digital firmata
  //Supports as many digital inputs and outputs as possible.

  //This example code is in the public domain.

#include

#define XstepPin 10
#define XdirPin 7

#define YstepPin 2
#define YdirPin 3

#define ZstepPin 19
#define ZdirPin  18

byte previousPIN[2];  // PIN means PORT for input
byte previousPORT[2];

void outputPort(byte portNumber, byte portValue)
{
// only send the data when it changes, otherwise you get too many messages!
    if(previousPIN[portNumber] != portValue) {
        Firmata.sendDigitalPort(portNumber, portValue);
        previousPIN[portNumber] = portValue;
        Firmata.sendDigitalPort(portNumber, portValue);
    }
}

void setPinModeCallback(byte pin, int mode) {
    if(pin > 1) { // don't touch RxTx pins (0,1)
        pinMode(pin, mode);
    }
}

void digitalWriteCallback(byte port, int value)
{
    byte i;
    byte currentPinValue, previousPinValue;

    if(value != previousPORT[port]) {
        for(i=0; i<8; i++) {
            currentPinValue = (byte) value & (1 << i);
            previousPinValue = previousPORT[port] & (1 << i);
            if(currentPinValue != previousPinValue) {
                digitalWrite(i + (port*8), currentPinValue);
            }
        }
        previousPORT[port] = value;
    }
}

void setup()
{
    Firmata.setFirmwareVersion(0, 1);
    Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
    Firmata.attach(SET_PIN_MODE, setPinModeCallback);
    Firmata.begin(57600);
}

void loop()
{
    outputPort(0, PIND &~ B00000011); // pins 0-7, ignoring Rx/Tx pins (0/1)
    outputPort(1, PINB); // pins 8-13
    while(Firmata.available()) {
        Firmata.processInput();
    }

//added stepper sequence to firmata code
digitalWrite(XstepPin,HIGH);    //take steps
digitalWrite(YstepPin,HIGH);
digitalWrite(ZstepPin,HIGH);
delayMicroseconds(2);
digitalWrite(XstepPin,LOW);
digitalWrite(YstepPin,LOW);
digitalWrite(ZstepPin,LOW);
delayMicroseconds(2);
delayMicroseconds(1000); //  <<<<<< USE TO CHANGE STEPPER SPEED???? <<<<<<<<

  
}


 

 
Remove these adsRemove these ads by Signing Up