Arduino Esplora Visualisation in Processing

Hi,

we're doing a project for school with our Arduino Esplora and try to visualate the axis in Processing based on a cube.
It's not our code and we don't know so much about programming with Processing, so we really need your help. :frowning:
If we run the program, the "cube" won't move. We think the program can't read the axis and it's just a failure in the code.

Here's the processing code:

/********************************************************************************
* ADXL345 Library Examples- pitch_roll.pde                                      *
*                                                                               *
* Copyright (C) 2012 Anil Motilal Mahtani Mirchandani(anil.mmm@gmail.com)       *
*                                                                               *
* License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> *
* This is free software: you are free to change and redistribute it.            *
* There is NO WARRANTY, to the extent permitted by law.                         *
*                                                                               *
*********************************************************************************/

// If you are working with Arduino Mega
// sudo ln -s /dev/ttyACM0 /dev/ttyS8

import processing.serial.*;

Serial fd;

int pitch = 0;
int roll = 0;
int yaw = 0;


void setup () 
{
size(640, 360, P3D); 
//Connect to the corresponding serial port
fd = new Serial(this, "COM6", 9600);
// Defer callback until new line
fd.bufferUntil('\n');
}

void draw () 
{
  //Set background
  background(0.5);

  pushMatrix(); 
  
  translate(width/2, height/2, -30); 

  //Rotate
  rotateX(((float)pitch)*PI/180.0); 
  rotateZ(((float)roll)*PI/180.0); 
  rotateY(((float)yaw)*PI/180.0);

  //Print data
  print("Pitch: ");
  print(pitch);
  print(", Roll: ");
  println(roll);
  print("Yaw: ");
  println(yaw);

  
  scale(90);
  beginShape(QUADS);

  fill(0, 255, 0); vertex(-1,  1,  1);
  fill(0, 255, 0); vertex( 1,  1,  1);
  fill(0, 255, 0); vertex( 1, -1,  1);
  fill(0, 255, 0); vertex(-1, -1,  1);

  fill(0, 255, 255); vertex( 1,  1,  1);
  fill(0, 255, 255); vertex( 1,  1, -1);
  fill(0, 255, 255); vertex( 1, -1, -1);
  fill(0, 255, 255); vertex( 1, -1,  1);


  fill(255, 0, 255); vertex( 1,  1, -1);
  fill(255, 0, 255); vertex(-1,  1, -1);
  fill(255, 0, 255); vertex(-1, -1, -1);
  fill(255, 0, 255); vertex( 1, -1, -1);

  fill(255, 255, 0); vertex(-1,  1, -1);
  fill(255, 255, 0); vertex(-1,  1,  1);
  fill(255, 255, 0); vertex(-1, -1,  1);
  fill(255, 255, 0); vertex(-1, -1, -1);


  fill(255, 0, 0); vertex(-1,  1, -1);
  fill(255, 0, 0); vertex( 1,  1, -1);
  fill(255, 0, 0); vertex( 1,  1,  1);
  fill(255, 0, 0); vertex(-1,  1,  1);


  fill(0, 0, 255); vertex(-1, -1, -1);
  fill(0, 0, 255); vertex( 1, -1, -1);
  fill(0, 0, 255); vertex( 1, -1,  1);
  fill(0, 0, 255); vertex(-1, -1,  1);


  endShape();

  popMatrix(); 
}

void serialEvent (Serial fd) 
{
  // get the ASCII string:
  String rpstr = fd.readStringUntil('\n');
  if (rpstr != null) {
    String[] list = split(rpstr, ':');
    pitch = ((int)float(list[0]));
    roll = ((int)float(list[1]));
    yaw = ((int)float(list[2]));
  }
}

and our Arduino code to only read and print the axis and temperature:

/*
 Verschiedene Temperaturen messen und via LED ausgeben
 */

#include <Esplora.h>

void setup()
{
  Serial.begin(9600);        // Initialisiere Kommunikation mit Computer
} 

void loop()
{
  // Temperatur einlesen
  int celsius = Esplora.readTemperature(DEGREES_C);
  
  // Temperatur ausgeben
  Serial.print("Temperatur betraegt ");
  Serial.print(celsius);
  Serial.println(" Grad Celsius");
  Serial.println("");
  
  delay(500);
  
  // 0 Grad und weniger: Blaue LED
  if (celsius <= 0)
  { Esplora.writeRGB (0,0,205); }
  // Zwischen 0 und 21 Grad: Lila LED
  if (celsius >= 21)  
  { Esplora.writeRGB (255,0,0); }
  // Ab 21 Grad: Rote LED
  else
  { Esplora.writeRGB (153,50,204); }
  
  
  // Achsen erkennen
 
  int xAxis = Esplora.readAccelerometer(X_AXIS);    // X-Achse einlesen
  int yAxis = Esplora.readAccelerometer(Y_AXIS);    // Y-Achse einlesen
  int zAxis = Esplora.readAccelerometer(Z_AXIS);    // Z-Achse einlesen

  //Achsen ausgeben
  
  Serial.print("x: ");      
  Serial.print(xAxis);      
  Serial.print("\ty: ");    
  Serial.print(yAxis);      
  Serial.print("\tz: ");    
  Serial.println(zAxis);    
  Serial.println("");

  delay(500);              
}

Sorry for the german comments. :smiley:

Hope you can help us. :slight_smile:

Your sincerely,
Josh6796

and try to visualate the axis in Processing based on a cube.

The axis of what? You are sending temperature data (in a strange way) to Processing.

fd.bufferUntil('\n');

This means that serialEvent() will be called when a carriage return arrives.

  Serial.print("Temperatur betraegt ");
  Serial.print(celsius);
  Serial.println(" Grad Celsius");
  Serial.println("");

This code is sending strings and TWO carriage returns to Processing. Why?

  Serial.print("x: ");      
  Serial.print(xAxis);      
  Serial.print("\ty: ");    
  Serial.print(yAxis);      
  Serial.print("\tz: ");    
  Serial.println(zAxis);    
  Serial.println("");

Then, you send this stuff. Processing will split the string at the :s. Element 0 will be "x". What, exactly, is that supposed to map to a float as? Element 1 will be something like "123y:". What is that supposed to look like as a float?

Is your Arduino really connected to COM6?