3d Geocube code – upgrade

Cleaned up and improved the code significantly for the 3d geocube (working title). Thought I’d post it, mostly for my own records. Note that the newer Arduino code uses the String library to format the xyz coordinate strings. I hear it’s going to be in core in Arduino v019.

Arduino:

#include < WString.h >

/* 3D grid object placement
 * V.05 - two buttons sending xyz coordinates w/ debouncing
 * Ben Leduc-Mills
*/

//number of buttons/nodes in our physical cube
const int numOfButtons = 2;

// pin assignments won't change
// each point has a switch, an led, and an xyz coordinate
const int button1 = 2;
const int button2 = 3;
int buttons[] = {button1, button2};

//array of xyz coordinates
String coord1 = "10,30,40;";
String coord2 = "20,20,20;";
String coord[] = {coord1, coord2};

void setup() {

 //start serial communitcation
 Serial.begin(9600);

 //init button pins as input
 pinMode(button1, INPUT);
 pinMode(button2, INPUT);

}

void loop() {

 for (int i = 0; i < numOfButtons; i++) {

  int reading1 = digitalRead(buttons[i]);

  delay(50);

  int reading2 = digitalRead(buttons[i]);

  if (reading1 == reading2 && reading2 == HIGH ) {

      Serial.print(coord[i]);
  } 
 }

 Serial.print('\n');

}

Processing:

/* grid 3D mapper
 * v.04 - multiple switch inputs, and cleaner code
 * by Ben Leduc-Mills
*/

import processing.serial.*;

Serial myPort; // the serial port
float buttonId; // the button id we get from arduino if a button is switched

int gridSize = 10; //size of grid
int spacing = 10; //distance between pixels

void setup() {

 size(800, 600, P3D);
 //size(400, 300);

 println(Serial.list()); // list available serial ports

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

 myPort.bufferUntil('\n');

 background(255); //set initial background color

}

void draw() {

  translate(width/2, height/2 -50, 200);
  rotateY(frameCount * 0.01);

}

void serialEvent (Serial myPort) {

 background(255);

 int xpos = 0;
 int ypos = 0;
 int zpos = 0;

 //get the button ID
 String inString = myPort.readStringUntil('\n');

 if (inString != null) {

  //trim whitespace
  inString = trim(inString);

  String coord[] = split(inString, ';');

 pushMatrix();

 //draw active points on grid

  for (int i = 0; i < coord.length -1; i++ ) {

    int subCoord[] = int(split(coord[i], ','));
    strokeWeight(3);
    point(subCoord[0], subCoord[1], subCoord[2] );

  }

  //draw rest of grid

     for (int i = 0; i < gridSize; i++) {
        for (int j = 0; j < gridSize; j++) {
          for( int k = 0; k < gridSize; k++) {
                strokeWeight(1);
                point(xpos, ypos, zpos);
                xpos += spacing;
          }
          xpos = 0;
          ypos += spacing;   
        }
        xpos = 0;
        ypos = 0;
        zpos += spacing; 
     }

  popMatrix();

  } 

}