Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

  • Sound gets distorted when runned in processing

    Hello World, I have written a processing code and have implemented a sound file. When I play it on a common player, a cleat woman voice comes out. If I run my code processing is distorting her voice into a slow man version... I am runnin processing on a MacBook Pro with the latest Java version.

    import processing.serial.*;
    import processing.sound.*;
    
    
    int S1_WAITING_FOR_PEOPLE  = 1;
    int S2_PEOPLE_ENTERED      = 2;
    int S3_LIGHT_FADE_OUT      = 3;
    int S4_DARK                = 4;
    int S5_MUSIC               = 5;
    int S6_LIGHT_FADE_IN       = 6;
    // make sure this is the last!!!!
    int TERMINATE              = 7;
    
    int mode = S1_WAITING_FOR_PEOPLE;
    
    
    
    
    int no_one_when_distance_greater_then = 160; // cm
    int time_before_dim = 5000; // ms
    
    SoundFile file;
    boolean is_playing = false;
    
    Serial myPort;  
    
    int dist;
    
    boolean someone_present = false;
    int someone_present_since_time;
    
    
    int music_playing_since;
    int music_duration;
    
    int in_mode_since_ms;
    
    void setup() {
      size(640, 360);
    
      printArray(Serial.list()); // "/dev/cu.usbmodem1411" cu.usb!!!
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      //myPort.readStringUntil('\n');
      myPort.bufferUntil('\n');
    
      // Load a soundfile from the data folder of the sketch and play it back in a loop
      file = new SoundFile(this, "03.mp3");
      //file.loop();
      music_duration = (int) file.duration() * 1000;
      music_duration += 2000;
    
    }      
    
    void draw() {
      background(0);
      fill(255);
      text("frameCount: "+frameCount, 50, 25);
      text("dist: "+dist, 50, 50);
      text("mode: "+mode, 50, 75);
    
    
      if (frameCount == 180) {
        turn_on();
      } else if (frameCount > 180) {
    
        if ( mode == S1_WAITING_FOR_PEOPLE) {
    
          if (someone_present == false) {
            if (dist < no_one_when_distance_greater_then) {
              someone_present = true;
              someone_present_since_time = millis();
              change_mode(mode + 1);
            }
          }
        } else if (mode == S2_PEOPLE_ENTERED) {
          text("millis: "+millis(), 50, 100);
          text("someone_present_since_time: "+someone_present_since_time, 50, 125);
          text("time_before_dim: "+time_before_dim, 50, 150);
          if (millis() - someone_present_since_time > time_before_dim) {
            change_mode(mode + 1);
          }
        } else if (mode == S3_LIGHT_FADE_OUT) {
          turn_off();
          change_mode(mode + 1);
        } else if (mode == S4_DARK) 
        {
          change_mode(mode + 1);
        } else if (mode == S5_MUSIC) 
        {
          if (is_playing == false) {
            file.play();
            is_playing = true;
            music_playing_since = millis();
          }
          if (millis() - music_playing_since > music_duration) {
            is_playing = false;
            file.stop();
            change_mode(mode + 1);
          }
        } else if (mode == S6_LIGHT_FADE_IN) 
        {
          // todo
          if (millis() - in_mode_since_ms > 1000) {
            change_mode(mode + 1);
          }
    
        } else if (mode == TERMINATE) 
        {
          change_mode(S1_WAITING_FOR_PEOPLE);
          someone_present = false; // reset
          println("turn_on();");
          turn_on();
        }
      }
    }
    
    
    void change_mode(int m) {
      mode = m;
      in_mode_since_ms = millis();
    }
    
    
    
    void serialEvent(Serial p) { 
      String s = p.readString();
      if (s != null) {
        s = s.replace("\r\n", "");
        if (s.contains("Distance Measured")) {
          String[] tokens = split(s, "=");
          dist = int(tokens[1]);
        }
      }
    } 
    
    
    void turn_on() {
      myPort.write("on");
    }
    
    void turn_off() {
      myPort.write("off");
    }
    
    void keyPressed() {
      if (key == 'a') {
        turn_on();
      }
      if (key == 's') {
        turn_off();
      }
    }
    

    Thanks for your help!

  • ...put two pieces of code together

    @Chrisir Thanks for your proposal. I have implement it into my code and wanted to upload it. Unfortunately it was not possible because there is not enough ram space. I have tried to use the Serial.print(F('')), to get rid of the problem, which was not enough. This is the code by now:

    int URECHO = 3;         // PWM Output 0-25000US,Every 50US represent 1cm
    int URTRIG = 5;         // PWM trigger pin
    int sensorPin = A0;     // select the input pin for the potentiometer
    int sensorValue = 0;    // variable to store the value coming from the sensor
    
    const int LEDPin = 13; // the pin that the LED is attached to
    const int LEDValueMax = 255; // the maximum value of the LED Brightness
    const int LEDValueMin = 0; // the minimum value of the LED Brightness
    int LEDValue = 255; // the start value of the LED Brightness
    int speedValue = 10; // the dimming speed value
    
    unsigned int distanceMeasured = 0;
    
    
    boolean activity; 
    
    int ledPIN = 13;
    
    
    void setup() {
      Serial.begin(9600);                        // Sets the baud rate to 9600
      pinMode(URTRIG, OUTPUT);                   // A low pull on pin COMP/TRIG
      digitalWrite(URTRIG, HIGH);                // Set to HIGH
      pinMode(URECHO, INPUT);                    // Sending Enable PWM mode command
      delay(500);
      Serial.println(F("Init the sensor"));
    
      pinMode(ledPIN, OUTPUT);
    }
    
    boolean weAreDimming = false;
    
    void loop() {
    
      PWM_Mode();
    
      if (weAreDimming) {
    
        // we dim
    
        // Decrease the LED Brightness untill it reaches minimum brightness
    
        analogWrite(ledPIN, LEDValue);
        delay(speedValue);
        LEDValue = LEDValue - 1; // in short LEDValue--;
    
        // Did we reach the minimum?
        if (LEDValue <= LEDValueMin) {
          weAreDimming = false; 
          digitalWrite(ledPIN, LOW); // set to LOW ??? Probably not necessary
        }
      } // big if 
      // -----
    
      else {
    
        // we don't dim
    
        String line = Serial.readStringUntil('\r');
        Serial.println (line);
    
        if (line.equals("on")) {
          digitalWrite(ledPIN, HIGH); //I put HIGH here instead of LOW
        } else if (line.equals("off")) {
          // turning it slowly off, aka dimming 
          // Init the dimming process 
          LEDValue = 100; // reset 
          weAreDimming = true;
        }
      } // big else
    } //function loop
    
    
    
    void PWM_Mode()                              // a low pull on pin COMP/TRIG  triggering a sensor reading
    {
    
      digitalWrite(URTRIG, LOW);
      digitalWrite(URTRIG, HIGH);               // reading Pin PWM will output pulses
    
      unsigned long LowLevelTime = pulseIn(URECHO, LOW) ;
    
      if (LowLevelTime >= 45000)              // the reading is invalid.
      {
        Serial.print(F("Invalid"));
      }
      else {
        Serial.print(F("Distance Measured="));
        distanceMeasured = LowLevelTime / 50;  // every 50us low level stands for 1cm
        Serial.print(distanceMeasured);
        Serial.println(); 
    
        if (distanceMeasured > 20) {
          activity = false;
        }
        else {
          activity = true;
        }
    
      }
    }
    

    Do you have an idea? Thanks again! F

  • ...put two pieces of code together

    in these lines, I am not sure why you say LOW when the command is "on" and vice versa:

      if (line.equals("on")) {
        digitalWrite(led, LOW);
      }
      else if (line.equals("off")) {
        digitalWrite(led, HIGH);
      }
    

    My Proposal

    My Proposal would be to have two distinct states in loop, either

    • where we dim (1) or
    • where we read the commands and switch the led on or start the dimming process (2).

    I also got rid off your while loop and instead use the looping of loop() itself

    not tested

    boolean weAreDimming = false; 
    
    void loop() {
    
    
      PWM_Mode();
    
    
    
      if (weAreDimming) {
    
        // we dim
    
        // Decrease the LED Brightness untill it reaches minimum brightness
    
        analogWrite(LEDPin, LEDValue);
        delay(speedValue);
        LEDValue = LEDValue - 1; // in short LEDValue--;
    
    
        // Did we reach the minimum?
        if (LEDValue <= LEDValueMin) {
          weAreDimming = false; 
          digitalWrite(led, LOW); // set to LOW ??? Probably not necessary
        }
      } // big if 
      // -----
    
      else {
    
        // we don't dim
    
        String line = Serial.readStringUntil('\r');
        Serial.println(line);
    
        if (line.equals("on")) {
          digitalWrite(led, HIGH);  // I put HIGH here instead of LOW
        } else if (line.equals("off")) {
          // turning it slowly off, aka dimming 
          // Init the dimming process 
          LEDValue = 100; // reset 
          weAreDimming = true;
        }
      } // big else
    } //function loop
    
  • communicate between two macs in processing

    Hello world, I would like to know if it is possible to communicate between two macs in processing. What I mean:

    Mac1 sends a signal to mac2 -> mac2 plays an audio file -> mac 2 waits for another signal sent by mac1 (loop)

    In the code I have written I would love to send a signal to the second mac (mac2) at "int S5_MUSIC = 5;". So that there will be two sounds played at the same time. I need the second mac for a second pair of boxes, to create a surround sound....

    This is my processing code:

    import processing.serial.*;
    import processing.sound.*;
    
    
    int S1_WAITING_FOR_PEOPLE  = 1;
    int S2_PEOPLE_ENTERED      = 2;
    int S3_LIGHT_FADE_OUT      = 3;
    int S4_DARK                = 4;
    int S5_MUSIC               = 5;
    int S6_LIGHT_FADE_IN       = 6;
    // make sure this is the last!!!!
    int TERMINATE              = 7;
    
    int mode = S1_WAITING_FOR_PEOPLE;
    
    
    
    
    int no_one_when_distance_greater_then = 160; // cm
    int time_before_dim = 5000; // ms
    
    SoundFile file;
    boolean is_playing = false;
    
    Serial myPort;  
    
    int dist;
    
    boolean someone_present = false;
    int someone_present_since_time;
    
    
    int music_playing_since;
    int music_duration;
    
    int in_mode_since_ms;
    
    void setup() {
      size(640, 360);
    
      printArray(Serial.list()); // "/dev/cu.usbmodem1411" cu.usb!!!
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      //myPort.readStringUntil('\n');
      myPort.bufferUntil('\n');
    
      // Load a soundfile from the data folder of the sketch and play it back in a loop
      file = new SoundFile(this, "Ex.wav");
      //file.loop();
      music_duration = (int) file.duration() * 1000;
      music_duration += 2000;
    
    }      
    
    void draw() {
      background(0);
      fill(255);
      text("frameCount: "+frameCount, 50, 25);
      text("dist: "+dist, 50, 50);
      text("mode: "+mode, 50, 75);
    
    
      if (frameCount == 180) {
        turn_on();
      } else if (frameCount > 180) {
    
        if ( mode == S1_WAITING_FOR_PEOPLE) {
    
          if (someone_present == false) {
            if (dist < no_one_when_distance_greater_then) {
              someone_present = true;
              someone_present_since_time = millis();
              change_mode(mode + 1);
            }
          }
        } else if (mode == S2_PEOPLE_ENTERED) {
          text("millis: "+millis(), 50, 100);
          text("someone_present_since_time: "+someone_present_since_time, 50, 125);
          text("time_before_dim: "+time_before_dim, 50, 150);
          if (millis() - someone_present_since_time > time_before_dim) {
            change_mode(mode + 1);
          }
        } else if (mode == S3_LIGHT_FADE_OUT) {
          turn_off();
          change_mode(mode + 1);
        } else if (mode == S4_DARK) 
        {
          change_mode(mode + 1);
        } else if (mode == S5_MUSIC) 
        {
          if (is_playing == false) {
            file.play();
            is_playing = true;
            music_playing_since = millis();
          }
          if (millis() - music_playing_since > music_duration) {
            is_playing = false;
            file.stop();
            change_mode(mode + 1);
          }
        } else if (mode == S6_LIGHT_FADE_IN) 
        {
          // todo
          if (millis() - in_mode_since_ms > 1000) {
            change_mode(mode + 1);
          }
    
        } else if (mode == TERMINATE) 
        {
          change_mode(S1_WAITING_FOR_PEOPLE);
          someone_present = false; // reset
          println("turn_on();");
          turn_on();
        }
      }
    }
    
    
    void change_mode(int m) {
      mode = m;
      in_mode_since_ms = millis();
    }
    
    
    
    void serialEvent(Serial p) { 
      String s = p.readString();
      if (s != null) {
        s = s.replace("\r\n", "");
        if (s.contains("Distance Measured")) {
          String[] tokens = split(s, "=");
          dist = int(tokens[1]);
        }
      }
    } 
    
    
    void turn_on() {
      myPort.write("on");
    }
    
    void turn_off() {
      myPort.write("off");
    }
    
    void keyPressed() {
      if (key == 'a') {
        turn_on();
      }
      if (key == 's') {
        turn_off();
      }
    }
    

    Thanks for your effort. Faffie

  • ...put two pieces of code together

    Hello world, for my bachelor project I am busy with an audio installation, the interface is done in processing (surprise) and arduino. Enclosed you will find three pieces of code all working well on their own.

    The first part consists of two codes one is arduino and one is processing. Function: light on -the sensor catches a person comming into the room-> light out-> music on-> light on-> end/loop

    The Arduinocode:

    int URECHO = 3;         // PWM Output 0-25000US,Every 50US represent 1cm
    int URTRIG = 5;         // PWM trigger pin
    int sensorPin = A0;     // select the input pin for the potentiometer
    int sensorValue = 0;    // variable to store the value coming from the sensor
    
    
    unsigned int distanceMeasured = 0;
    
    
    boolean activity;
    
    int led = 13;
    
    
    void setup() {
      Serial.begin(9600);                        // Sets the baud rate to 9600
      pinMode(URTRIG, OUTPUT);                   // A low pull on pin COMP/TRIG
      digitalWrite(URTRIG, HIGH);                // Set to HIGH
      pinMode(URECHO, INPUT);                    // Sending Enable PWM mode command
      delay(500);
      Serial.println("Init the sensor");
    
      pinMode(led, OUTPUT);
    }
    
    void loop() {
      PWM_Mode();
    
    
    
      String line = Serial.readStringUntil('\r');
      Serial.println(line);
    
      if (line.equals("on")) {
        digitalWrite(led, LOW);
      }
      else if (line.equals("off")) {
        digitalWrite(led, HIGH);
      }
    
    
    
    
      //delay(100);
    
    //  if (activity) {
    //    digitalWrite(led, HIGH);
    //  }
    //  else {
    //    digitalWrite(led, LOW);
    //  }
    }
    
    
    void PWM_Mode()                              // a low pull on pin COMP/TRIG  triggering a sensor reading
    {
    
      digitalWrite(URTRIG, LOW);
      digitalWrite(URTRIG, HIGH);               // reading Pin PWM will output pulses
    
      unsigned long LowLevelTime = pulseIn(URECHO, LOW) ;
    
      if (LowLevelTime >= 45000)              // the reading is invalid.
      {
        Serial.print("Invalid");
      }
      else {
        Serial.print("Distance Measured=");
        distanceMeasured = LowLevelTime / 50;  // every 50us low level stands for 1cm
        Serial.print(distanceMeasured);
        Serial.println(); 
    
        if (distanceMeasured > 20) {
          activity = false;
        }
        else {
          activity = true;
        }
    
      }
    }
    

    The Processing code:

    import processing.serial.*;
    import processing.sound.*;
    
    
    int S1_WAITING_FOR_PEOPLE  = 1;
    int S2_PEOPLE_ENTERED      = 2;
    int S3_LIGHT_FADE_OUT      = 3;
    int S4_DARK                = 4;
    int S5_MUSIC               = 5;
    int S6_LIGHT_FADE_IN       = 6;
    // make sure this is the last!!!!
    int TERMINATE              = 7;
    
    int mode = S1_WAITING_FOR_PEOPLE;
    
    
    
    int no_one_when_distance_greater_then = 160; // cm
    int time_before_dim = 5000; // ms
    
    SoundFile file;
    boolean is_playing = false;
    
    Serial myPort;  
    
    int dist;
    
    boolean someone_present = false;
    int someone_present_since_time;
    
    
    int music_playing_since;
    int music_duration;
    
    int in_mode_since_ms;
    
    void setup() {
      size(640, 360);
    
      printArray(Serial.list()); // "/dev/cu.usbmodem1411" cu.usb!!!
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      //myPort.readStringUntil('\n');
      myPort.bufferUntil('\n');
    
      // Load a soundfile from the data folder of the sketch and play it back in a loop
      file = new SoundFile(this, "Ex.wav");
      //file.loop();
      music_duration = (int) file.duration() * 1000;
      music_duration += 2000;
    
    }      
    
    void draw() {
      background(0);
      fill(255);
      text("frameCount: "+frameCount, 50, 25);
      text("dist: "+dist, 50, 50);
      text("mode: "+mode, 50, 75);
    
    
      if (frameCount == 180) {
        turn_on();
      } else if (frameCount > 180) {
    
        if ( mode == S1_WAITING_FOR_PEOPLE) {
    
          if (someone_present == false) {
            if (dist < no_one_when_distance_greater_then) {
              someone_present = true;
              someone_present_since_time = millis();
              change_mode(mode + 1);
            }
          }
        } else if (mode == S2_PEOPLE_ENTERED) {
          text("millis: "+millis(), 50, 100);
          text("someone_present_since_time: "+someone_present_since_time, 50, 125);
          text("time_before_dim: "+time_before_dim, 50, 150);
          if (millis() - someone_present_since_time > time_before_dim) {
            change_mode(mode + 1);
          }
        } else if (mode == S3_LIGHT_FADE_OUT) {
          turn_off();
          change_mode(mode + 1);
        } else if (mode == S4_DARK) 
        {
          change_mode(mode + 1);
        } else if (mode == S5_MUSIC) 
        {
          if (is_playing == false) {
            file.play();
            is_playing = true;
            music_playing_since = millis();
          }
          if (millis() - music_playing_since > music_duration) {
            is_playing = false;
            file.stop();
            change_mode(mode + 1);
          }
        } else if (mode == S6_LIGHT_FADE_IN) 
        {
          // todo
          if (millis() - in_mode_since_ms > 1000) {
            change_mode(mode + 1);
          }
    
        } else if (mode == TERMINATE) 
        {
          change_mode(S1_WAITING_FOR_PEOPLE);
          someone_present = false; // reset
          println("turn_on();");
          turn_on();
        }
      }
    }
    
    
    void change_mode(int m) {
      mode = m;
      in_mode_since_ms = millis();
    }
    
    
    
    void serialEvent(Serial p) { 
      String s = p.readString();
      if (s != null) {
        s = s.replace("\r\n", "");
        if (s.contains("Distance Measured")) {
          String[] tokens = split(s, "=");
          dist = int(tokens[1]);
        }
      }
    } 
    
    
    void turn_on() {
      myPort.write("on");
    }
    
    void turn_off() {
      myPort.write("off");
    }
    
    void keyPressed() {
      if (key == 'a') {
        turn_on();
      }
      if (key == 's') {
        turn_off();
      }
    }
    

    The second part is an arduino code for dimming an LED light:

    const int LEDPin = 9; // the pin that the LED is attached to
    const int LEDValueMax = 255; // the maximum value of the LED Brightness
    const int LEDValueMin = 0; // the minimum value of the LED Brightness
    int LEDValue = 255; // the start value of the LED Brightness
    int speedValue = 10; // the dimming speed value
    
    void setup() {
    
      pinMode(LEDPin, OUTPUT); // the PWM Output 
    
    }
    
    void loop() {
    
        do // Decrease the LED Brightness untill it reaches minimum brightness
        {
           analogWrite(LEDPin, LEDValue);
           delay(speedValue);
           LEDValue = LEDValue - 1;
    
        } while (LEDValue > LEDValueMin);
    
    }
    

    What I would love to fix is that in the first part where the light is turned off (int S3_LIGHT_FADE_OUT = 3;) it is going to dim the brightness as in the the code above. I am not sure how to get these two programs to talk to each other/ switching from int S3 to arduino dimming code.

    Thanks for your efforts! Faffie

  • How to make a P3D program run faster

    I'm having a problem with my sketch running slow also. I'm making a 3D game with a lot of different objects in the game like enemies, obstacles and things like that. The issue is, once I start adding more and more things to the game, it runs really, really slow. Is there a way I can make it run faster. Here's my code:

    import peasy.*;
    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor, tempSensor, mic;
    int jX, jY;
    int aX, aY, aZ;
    int jButton = 1;
    
    boolean test = false; // new
    
    PeasyCam cam;
    int x=75,y=0,z=175;
    int xMov = 0;
    int yMov = 0;
    int zMov = 0;
    Tent tent1;
    Tent tent2;
    Tent tent3;
    Tent tent4;
    Tent tent5;
    Tent tent6;
    Tent tent7;
    Tent tent8;
    Tent tent9;
    Tent tent10;
    Tent tent11;
    Tent tent12;
    Tent tent13;
    Tent tent14;
    Tent tent15;
    Tent tent16;
    Tent tent17;
    ArrayList<Tree> aForest;
    int cY = 0, cZ = 0;
    float ccy=0,ccz=0;
    float xAngle = 0;
    void setup(){
     //fullScreen(P3D);
     size(683,384,P3D);
     frameRate(120);
     if (test) {
        frameRate(2); // slow everything down
      } else {
        String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
        myPort = new Serial(this, portName, 300);
      }
    
    
     //cam.setSuppressRollRotationMode();
    
    aForest = new ArrayList<Tree>();        /*
    for (int z = -100; z < 300; z+=10){
       for (int x = 175; x < 195; x+=10){ 
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }
     for (int z = 55; z < 75; z+=10){
       for (int x = -20; x < 130; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }                                                                    //THIS AREA MAKES IT RUN SLOW
     for (int z = -100; z < -50; z+=10){
       for (int x = -20; x < 175; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }
     for (int z = -100; z < 75; z+=10){
       for (int x = -50; x < -20; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }        */
    
    
    tent1 = new Tent(#f4a460,130,0,100,0);
    tent2 = new Tent(#f4a460,100,0,100,0);
    tent3 = new Tent(#f4a460,70,0,100,0);
    tent4 = new Tent(#f4a460,40,0,100,0);
    tent5 = new Tent(#f4a460,10,0,100,0);
    tent6 = new Tent(#f4a460,130,0,140,0);
    tent7 = new Tent(#f4a460,100,0,140,0);
    tent8 = new Tent(#f4a460,70,0,140,0);
    tent9 = new Tent(#f4a460,40,0,140,0);
    tent10 = new Tent(#f4a460,10,0,140,0);
    tent11 = new Tent(#f4a460,150,0,200,1);
    tent12 = new Tent(#f4a460,150,0,230,1);
    tent13 = new Tent(#f4a460,90,0,240,0);
    tent14 = new Tent(#f4a460,60,0,240,0);
    tent15 = new Tent(#f4a460,120,0,300,0);
    tent16 = new Tent(#f4a460,90,0,300,0);
    tent17 = new Tent(#f4a460,60,0,300,0);
    }
    void draw(){
     background(105); 
      println(frameRate);
     cc();
     //println(cam.getPosition());
     //directionalLight(255,255,255,0,1,0);
    
     translate(width/2,height+1,0);
     fill(0,255,0);                       //(height/2)/tan(PI/6)
     stroke(0);                        //translate(x,y,z);
     box(width,-1,width);            //box(width,height,depth)
     fill(255,0,0);
    
     tent1.display();
     tent2.display();
     tent3.display();
     tent4.display();
     tent5.display();
     tent6.display();
     tent7.display();
     tent8.display();
     tent9.display();
     tent10.display();
     tent11.display();
     tent12.display();
     tent13.display();
     tent14.display();
     tent15.display();
     tent16.display();
     tent17.display();
    
     for(Tree singleTree: aForest){  //Check: <a href="https://processing.org/reference/for.html" target="_blank" rel="nofollow">https://processing.org/reference/for.html</a>;
       singleTree.display();
    }
    
    
     translate(x,y-2.5,z);
    // rotateY(turn);
     fill(0);
     stroke(255,0,0);
     sphere(5);
    
     x += xMov;
     y += yMov;
     z += zMov;
     println(x,",",y,",",z);
    }
    
    class Tent{
      color c;
      float tX;
      float tY;
      float tZ;
      float d;
     Tent(color tempC, float tempX, float tempY, float tempZ, float tempD){
       d = tempD;
       c = tempC;
       tX = tempX;
       tY = tempY;
       tZ = tempZ;
       }
       void display(){
         if(d == 0){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY-10,tZ-10); //top rear
         vertex(tX,tY-10,tZ+10); //top front
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX-10,tY,tZ-10); //left rear
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY-10,tZ-10); //top rear
         vertex(tX,tY-10,tZ+10); //top front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX,tY-10,tZ-10); //top rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX,tY-10,tZ+10); //top front
         endShape(CLOSE);
         }else if(d == 1){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY-10,tZ); //top left
         vertex(tX+10,tY-10,tZ); //top right
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX-10,tY,tZ-10); //left rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY-10,tZ); //top left
         vertex(tX+10,tY-10,tZ); //top right
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX-10,tY,tZ+10); //left front
         endShape(CLOSE);
         beginShape();
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX+10,tY-10,tZ); //top right
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY-10,tZ); //top left
         endShape(CLOSE);
         }else if(d == 2){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ); //right
         vertex(tX,tY,tZ+10); //front
         vertex(tX+10,tY,tZ); //left
         vertex(tX,tY,tZ-10); //rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ);     //right
         vertex(tX,tY,tZ-10);     //rear
         vertex(tX-5,tY-10,tZ-5); //right rear top
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY,tZ+10);     //front
         vertex(tX+10,tY,tZ);     //left
         vertex(tX+5,tY-10,tZ+5); //left front top
         endShape(CLOSE); 
         beginShape();
         vertex(tX-10,tY,tZ); //right
         vertex(tX,tY,tZ+10); //front
         vertex(tX+5,tY-10,tZ+5); //left front top
         vertex(tX-5,tY-10,tZ-5); //right rear top
         endShape(CLOSE);
         beginShape();
         vertex(tX+10,tY,tZ); //left
         vertex(tX,tY,tZ-10); //rear
         vertex(tX-5,tY-10,tZ-5); //right rear top
         vertex(tX+5,tY-10,tZ+5); //left front top
         endShape(CLOSE);
         }
     }
    }
    
    class Tree {
     float h;
     float tS;
     float lS;
     color lC;
     color tC;
     float treeX,treeY,treeZ;
       Tree (float trunkHeight, float trunkSize, float leafSize, color leafColor, color trunkColor, int treex, int treey, int treez){
         h = trunkHeight;
         tS = trunkSize;
         lS = leafSize;
         lC = leafColor;
         tC = trunkColor;
         treeX = treex;
         treeY = treey;
         treeZ = treez;
       }
       void display(){         
       fill(tC);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       fill(lC);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       }
    }
    void keyPressed() {
      if ((key == 'w')) { 
        yMov = -1;
      } else if ((key == 'a')) {  
        xMov = -1;                                   //WASD CONTROLS
      } else if ((key == 'd')) {
        xMov = 1;
      } else if ((key == 's')) {
        yMov = 1;
      } else if((key == 'e')){
       zMov = -1; 
      }else if((key == 'c')){
       zMov = 1; 
      }
    }  
    void keyReleased() {
      if ((key == 'w') || (key == 's')) {
        yMov = 0;                                           //WASD CONTROLS
      } else if ((key == 'a') || (key == 'd')) {
        xMov = 0;
      }else if((key == 'e' ) || (key == 'c')){
       zMov = 0; 
      }
    }
    void cc() {
    
      String val = getData();
      if (val != null) {
        parseData(val);
      }
    
      if (jX >= 15) {
        x-=1;
      } else if (jX <= -15) {
        x+=1;
      }
      if (jY >= 15) {
        z += 1;
      } else if (jY <= -15) {
        z -= 1;
      }
    
    
      cY = 21;
      cam = new PeasyCam(this, (width/2)+x, (height-50)+y, z+15, 50);
      // println(cX,cY,cZ);
      ccy = map(cY, -100, 100, -3, 3);
      cam.setRotations(ccy, 0, 0);
      // println(cX,cY,cZ);
    }
    void parseData(String val) {
      String[] data = split(val, ",");
      if (data == null || data.length != 14) {
      } else {
        button1 = int(data[0]);
        button2 = int(data[1]);
        button3 = int(data[2]);
        button4 = int(data[3]);
        linPot = int(data[4]);
        liteSensor = int(data[5]);
        tempSensor = int(data[6]);
        mic = int(data[7]);
        jButton = int(data[8]);
        jX = int(data[9]);
        jY = int(data[10]);
        aX = int(data[11]);
        aY = int(data[12]);
        aZ = int(data[13]);
      }
    }
    String getData() {
      if (test) {
        return "1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135";
      } else if ( myPort.available() > 0) {
        val = myPort.readStringUntil('\n');
        return val;
      } else {
        return null;
      }
    }
    

    As you can see, there is a lot of code here and i just started, if I note out the area shown, it runs fine, as is, it runs faster, but then its missing a large chunk of the game. There is still more that needs to be added so if there is a way I can get it to run faster, that would be greatly appreciated.

  • serial.available() responds to no input

    I've got my laptop connected via Com5 to an Arduino Mega. When strings are sent from the Mega, Processing is responding correctly. However, Processing is also reading "nulls" when it shouldn't. The following code is filling the string with nulls--it seems to me that if (serial.available>0), then the string should have something other than null in it.

    if(myPort.available() > 0) {               // If data is available,
      val = myPort.readStringUntil('\n');   }  // read it and store it in val
    if(val != null) {
      print(val);                            //print it out in the console
      val = null ; }
    

    Two questions: First, I'm wondering why the first if statement is being entered and second, am I using "null" correctly or do I need single or double quotes around it? Thanks

  • How can you make your program run faster?

    I'm having a problem with my sketch running slow also. I'm making a 3D game with a lot of different objects in the game like enemies, obstacles and things like that. The issue is, once I start adding more and more things to the game, it runs really, really slow. Is there a way I can make it run faster. Here's my code:

    import peasy.*;
    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor, tempSensor, mic;
    int jX, jY;
    int aX, aY, aZ;
    int jButton = 1;
    
    boolean test = false; // new
    
    PeasyCam cam;
    int x=75,y=0,z=175;
    int xMov = 0;
    int yMov = 0;
    int zMov = 0;
    Tent tent1;
    Tent tent2;
    Tent tent3;
    Tent tent4;
    Tent tent5;
    Tent tent6;
    Tent tent7;
    Tent tent8;
    Tent tent9;
    Tent tent10;
    Tent tent11;
    Tent tent12;
    Tent tent13;
    Tent tent14;
    Tent tent15;
    Tent tent16;
    Tent tent17;
    ArrayList<Tree> aForest;
    int cY = 0, cZ = 0;
    float ccy=0,ccz=0;
    float xAngle = 0;
    void setup(){
     //fullScreen(P3D);
     size(683,384,P3D);
     frameRate(120);
     if (test) {
        frameRate(2); // slow everything down
      } else {
        String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
        myPort = new Serial(this, portName, 300);
      }
    
    
     //cam.setSuppressRollRotationMode();
    
    aForest = new ArrayList<Tree>();        /*
    for (int z = -100; z < 300; z+=10){
       for (int x = 175; x < 195; x+=10){ 
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }
     for (int z = 55; z < 75; z+=10){
       for (int x = -20; x < 130; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }                                                                    //THIS AREA
     for (int z = -100; z < -50; z+=10){
       for (int x = -20; x < 175; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }
     for (int z = -100; z < 75; z+=10){
       for (int x = -50; x < -20; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }        */
    
    
    tent1 = new Tent(#f4a460,130,0,100,0);
    tent2 = new Tent(#f4a460,100,0,100,0);
    tent3 = new Tent(#f4a460,70,0,100,0);
    tent4 = new Tent(#f4a460,40,0,100,0);
    tent5 = new Tent(#f4a460,10,0,100,0);
    tent6 = new Tent(#f4a460,130,0,140,0);
    tent7 = new Tent(#f4a460,100,0,140,0);
    tent8 = new Tent(#f4a460,70,0,140,0);
    tent9 = new Tent(#f4a460,40,0,140,0);
    tent10 = new Tent(#f4a460,10,0,140,0);
    tent11 = new Tent(#f4a460,150,0,200,1);
    tent12 = new Tent(#f4a460,150,0,230,1);
    tent13 = new Tent(#f4a460,90,0,240,0);
    tent14 = new Tent(#f4a460,60,0,240,0);
    tent15 = new Tent(#f4a460,120,0,300,0);
    tent16 = new Tent(#f4a460,90,0,300,0);
    tent17 = new Tent(#f4a460,60,0,300,0);
    }
    void draw(){
     background(105); 
      println(frameRate);
     cc();
     //println(cam.getPosition());
     //directionalLight(255,255,255,0,1,0);
    
     translate(width/2,height+1,0);
     fill(0,255,0);                       //(height/2)/tan(PI/6)
     stroke(0);                        //translate(x,y,z);
     box(width,-1,width);            //box(width,height,depth)
     fill(255,0,0);
    
     tent1.display();
     tent2.display();
     tent3.display();
     tent4.display();
     tent5.display();
     tent6.display();
     tent7.display();
     tent8.display();
     tent9.display();
     tent10.display();
     tent11.display();
     tent12.display();
     tent13.display();
     tent14.display();
     tent15.display();
     tent16.display();
     tent17.display();
    
     for(Tree singleTree: aForest){  //Check: <a href="https://processing.org/reference/for.html" target="_blank" rel="nofollow">https://processing.org/reference/for.html</a>;
       singleTree.display();
    }
    
    
     translate(x,y-2.5,z);
    // rotateY(turn);
     fill(0);
     stroke(255,0,0);
     sphere(5);
    
     x += xMov;
     y += yMov;
     z += zMov;
     println(x,",",y,",",z);
    }
    
    class Tent{
      color c;
      float tX;
      float tY;
      float tZ;
      float d;
     Tent(color tempC, float tempX, float tempY, float tempZ, float tempD){
       d = tempD;
       c = tempC;
       tX = tempX;
       tY = tempY;
       tZ = tempZ;
       }
       void display(){
         if(d == 0){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY-10,tZ-10); //top rear
         vertex(tX,tY-10,tZ+10); //top front
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX-10,tY,tZ-10); //left rear
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY-10,tZ-10); //top rear
         vertex(tX,tY-10,tZ+10); //top front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX,tY-10,tZ-10); //top rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX,tY-10,tZ+10); //top front
         endShape(CLOSE);
         }else if(d == 1){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY-10,tZ); //top left
         vertex(tX+10,tY-10,tZ); //top right
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX-10,tY,tZ-10); //left rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY-10,tZ); //top left
         vertex(tX+10,tY-10,tZ); //top right
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX-10,tY,tZ+10); //left front
         endShape(CLOSE);
         beginShape();
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX+10,tY-10,tZ); //top right
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY-10,tZ); //top left
         endShape(CLOSE);
         }else if(d == 2){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ); //right
         vertex(tX,tY,tZ+10); //front
         vertex(tX+10,tY,tZ); //left
         vertex(tX,tY,tZ-10); //rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ);     //right
         vertex(tX,tY,tZ-10);     //rear
         vertex(tX-5,tY-10,tZ-5); //right rear top
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY,tZ+10);     //front
         vertex(tX+10,tY,tZ);     //left
         vertex(tX+5,tY-10,tZ+5); //left front top
         endShape(CLOSE); 
         beginShape();
         vertex(tX-10,tY,tZ); //right
         vertex(tX,tY,tZ+10); //front
         vertex(tX+5,tY-10,tZ+5); //left front top
         vertex(tX-5,tY-10,tZ-5); //right rear top
         endShape(CLOSE);
         beginShape();
         vertex(tX+10,tY,tZ); //left
         vertex(tX,tY,tZ-10); //rear
         vertex(tX-5,tY-10,tZ-5); //right rear top
         vertex(tX+5,tY-10,tZ+5); //left front top
         endShape(CLOSE);
         }
     }
    }
    
    class Tree {
     float h;
     float tS;
     float lS;
     color lC;
     color tC;
     float treeX,treeY,treeZ;
       Tree (float trunkHeight, float trunkSize, float leafSize, color leafColor, color trunkColor, int treex, int treey, int treez){
         h = trunkHeight;
         tS = trunkSize;
         lS = leafSize;
         lC = leafColor;
         tC = trunkColor;
         treeX = treex;
         treeY = treey;
         treeZ = treez;
       }
       void display(){         
       fill(tC);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       fill(lC);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       }
    }
    void keyPressed() {
      if ((key == 'w')) { 
        yMov = -1;
      } else if ((key == 'a')) {  
        xMov = -1;                                   //WASD CONTROLS
      } else if ((key == 'd')) {
        xMov = 1;
      } else if ((key == 's')) {
        yMov = 1;
      } else if((key == 'e')){
       zMov = -1; 
      }else if((key == 'c')){
       zMov = 1; 
      }
    }  
    void keyReleased() {
      if ((key == 'w') || (key == 's')) {
        yMov = 0;                                           //WASD CONTROLS
      } else if ((key == 'a') || (key == 'd')) {
        xMov = 0;
      }else if((key == 'e' ) || (key == 'c')){
       zMov = 0; 
      }
    }
    void cc() {
    
      String val = getData();
      if (val != null) {
        parseData(val);
      }
    
      if (jX >= 15) {
        x-=1;
      } else if (jX <= -15) {
        x+=1;
      }
      if (jY >= 15) {
        z += 1;
      } else if (jY <= -15) {
        z -= 1;
      }
    
    
      cY = 21;
      cam = new PeasyCam(this, (width/2)+x, (height-50)+y, z+15, 50);
      // println(cX,cY,cZ);
      ccy = map(cY, -100, 100, -3, 3);
      cam.setRotations(ccy, 0, 0);
      // println(cX,cY,cZ);
    }
    void parseData(String val) {
      String[] data = split(val, ",");
      if (data == null || data.length != 14) {
      } else {
        button1 = int(data[0]);
        button2 = int(data[1]);
        button3 = int(data[2]);
        button4 = int(data[3]);
        linPot = int(data[4]);
        liteSensor = int(data[5]);
        tempSensor = int(data[6]);
        mic = int(data[7]);
        jButton = int(data[8]);
        jX = int(data[9]);
        jY = int(data[10]);
        aX = int(data[11]);
        aY = int(data[12]);
        aZ = int(data[13]);
      }
    }
    String getData() {
      if (test) {
        return "1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135";
      } else if ( myPort.available() > 0) {
        val = myPort.readStringUntil('\n');
        return val;
      } else {
        return null;
      }
    }
    

    As you can see, there is a lot of code here and i just started, if I note out the area shown, it runs fine, as is, it runs faster, but then its missing a large chunk of the game. There is still more that needs to be added so if there is a way I can get it to run faster, that would be greatly appreciated.

  • How to split incoming data from arduino

    All right guys, thanks for all the help. I finally figured it out...with a little help from my dad. Here's the final code:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor, tempSensor, mic;
    int jX, jY;
    int aX, aY, aZ;
    int jButton = 1;
    
    boolean test = false; // new
    
    void setup() {
      if (test) {
        frameRate(2); // slow everything down
      } else {
        String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
        myPort = new Serial(this, portName, 300);
      }
    }
    
    void draw() {
      String val = getData();
      if (val != null) {
        parseData(val);
      }
    }
    
    void parseData(String val) {
      String[] data = split(val, ",");
      if (data == null || data.length != 14) {
        println("error: not enough data");
      } else {
        println("REPORT: length="+data.length);
        println("Incoming data content:");
        println("|"+val+"|");
        button1 = int(data[0]);
        button2 = int(data[1]);
        button3 = int(data[2]);
        button4 = int(data[3]);
        linPot = int(data[4]);
        liteSensor = int(data[5]);
        tempSensor = int(data[6]);
        mic = int(data[7]);
        jButton = int(data[8]);
        jX = int(data[9]);
        jY = int(data[10]);
        aX = int(data[11]);
        aY = int(data[12]);
        aZ = int(data[13]);
        println("buttons:", button1, button2, button3, button4);
        println("others:", linPot, liteSensor, tempSensor, mic);
        println("j:", jX, jY);
        println("a:", aX, aY, aZ);
        println("jbutton:", jButton);
      }
    }
    
    // gets the data (test or from serial)
    String getData() {
      if (test) {
        // test, return test String
        println("Testing");
        return "1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135";
      } else if ( myPort.available() > 0) {
        // return data from port
        val = myPort.readStringUntil('\n');
        println("Val: [" + val + "]");
        return val;
      } else {
        return null;
      }
    }
    

    As you can probably see, the biggest difference is that I should have been using int()to change data points into an integer rather than Integer.parseInt(). So for anyone else trying to split a string of data into a bunch of data points, this is how you do it.

  • How to split incoming data from arduino

    What you need to test, is to check how many tokens you get after you apply the split() function on your data. Because it is less than 14 doesn't mean you have all the tokens. In your case, if you want to process 14 tokens, your condition should read data.length==14. If you do data.length<14 then it is not guaranteed you have 14 tokens available. So for instance, if split returns 6 tokens, then you won't succeed in tempSensor = Integer.parseInt(data[6]); What I want to convince you (and convince us as we can't test your code) is that you are receiving consistently 14 tokens. If you are not, then at least we can proceed to think of the proper approach. I will strongly suggest you check previous arduino code here in the forum as they manage single data arrival (yes, you need to change your ino code for this). When you get a simple code working, then you can test some other sensor in a one to one approach.

    Problems that I can see:

    • You are sending an float value as integer
    • Your sensors might not be doing what you think they should be doing
    • The data rate you are sending is too high

    Stuff we don't know...

    Another approach is this:

    import processing.serial.Serial;
    
    static final int PORT_INDEX = 0, BAUDS = 9600;
    
    int[] vals;
    //float[] vals;
    
    void setup() {
      noLoop();
      final String[] ports = Serial.list();
      printArray(ports);
      new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
    }
    
    void draw() {
      println(vals);
    }
    
    void serialEvent(final Serial s) {
      val = myPort.readStringUntil('\n');
      println(val);
    
      if(val!=null){
        String[] data = split(val, ",");
        println("REPORT: length="+data.length);
      }
    
      redraw = true;
    }
    

    What do you get from this code. Can you post few lines of this output? As you see, here I remove the overhead of processing the data as this part is ok. You need to focus in your raw data.

    Kf

  • How to split incoming data from arduino

    this is how i would do this: do one thing at a time.

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor, tempSensor, mic;
    int jX, jY;
    int aX, aY, aZ;
    int jButton = 1;
    
    boolean test = true; // new
    
    void setup() {
      if (test) {
        frameRate(2); // slow everything down
      } else {
        String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
        myPort = new Serial(this, portName, 9600);
      }
    }
    
    void draw() {
      String val = getData();
      if (val != null) {
        parseData(val);
      }
    }
    
    void parseData(String val) {
      String[] data = split(val, ",");
      if (data == null || data.length < 14) {
        println("error: not enough data");
      } else {
        println("REPORT: length="+data.length);
        println("Incoming data content:");
        println(val);
        button1 = Integer.parseInt(data[0]);
        button2 = Integer.parseInt(data[1]);
        button3 = Integer.parseInt(data[2]);
        button4 = Integer.parseInt(data[3]);
        linPot = Integer.parseInt(data[4]);
        liteSensor = Integer.parseInt(data[5]);
        tempSensor = Integer.parseInt(data[6]);
        mic = Integer.parseInt(data[7]);
        jX = Integer.parseInt(data[8]);
        jY = Integer.parseInt(data[9]);
        aX = Integer.parseInt(data[10]);
        aY = Integer.parseInt(data[11]);
        aZ = Integer.parseInt(data[12]);
        jButton = Integer.parseInt(data[13]);
        println("buttons:", button1, button2, button3, button4);
        println("others:", linPot, liteSensor, tempSensor, mic);
        println("j:", jX, jY);
        println("a:", aX, aY, aZ);
        println("jbutton:", jButton);
      }
    }
    
    // gets the data (test or from serial)
    String getData() {
      if (test) {
        // test, return test String
        println("Testing");
        return "1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135";
      } else if ( myPort.available() > 0) {
        // return data from port
        val = myPort.readStringUntil('\n');
        println("Val: [" + val + "]");
        return val;
      } else {
        return null;
      }
    }
    

    i've added test variable. when this is true then it'll parse a known string. use this to debug the parsing of the incoming string without worrying about the arduino.

    WHEN THIS WORKS then you can set test to false to get the real data from the arduino. by this time you know the parsing works (assuming your test data is correct) and can concentrate on the incoming data.

    and don't skimp on the debug. just printing out a bunch of values without labels helps nobody.

  • How to split incoming data from arduino

    That still doesn't help me though. When i println() the data values, they all come out as 1's and 0's and then they don't even change. Ill give you the Arduino code also. Arduino Code:

    #include <Esplora.h>
    
    void setup() {
      Serial.begin(9600);
    while (!Serial);
    }
    
    void loop() {
      if(Serial.available()){
       parseCommand();
      }
      dumpInputs();
     }
    
    void parseCommand(){
      char cmd = Serial.read();
      switch (cmd) {
        case 'D':
          //dumpInputs();
          break;
        case 'R':
          setRed();
          break;
        case 'G':
          setGreen();
          break;
        case 'B':
          setBlue();
          break;
        case 'T':
          setTone();
          break;
      }
    }
    void dumpInputs() {
      Serial.print(Esplora.readButton(SWITCH_1));
      Serial.print(',');
      Serial.print(Esplora.readButton(SWITCH_2));
      Serial.print(',');
      Serial.print(Esplora.readButton(SWITCH_3));
      Serial.print(',');
      Serial.print(Esplora.readButton(SWITCH_4));
      Serial.print(',');
      Serial.print(Esplora.readSlider());
      Serial.print(',');
      Serial.print(Esplora.readLightSensor());
      Serial.print(',');
      Serial.print(Esplora.readTemperature(DEGREES_C));
      Serial.print(',');
      Serial.print(Esplora.readMicrophone());
      Serial.print(',');
      Serial.print(Esplora.readJoystickSwitch());
      Serial.print(',');
      Serial.print(Esplora.readJoystickX());
      Serial.print(',');
      Serial.print(Esplora.readJoystickY());
      Serial.print(',');
      Serial.print(Esplora.readAccelerometer(X_AXIS));
      Serial.print(',');
      Serial.print(Esplora.readAccelerometer(Y_AXIS));
      Serial.print(',');
      Serial.print(Esplora.readAccelerometer(Z_AXIS));
      Serial.println();
      delay(30);
    }
    void setRed() {
      Esplora.writeRed(Serial.parseInt());
    }
    
    void setGreen() {
      Esplora.writeGreen(Serial.parseInt());
    }
    
    void setBlue() {
      Esplora.writeBlue(Serial.parseInt());
    }
    
    void setTone() {
      Esplora.tone(Serial.parseInt());
    }
    

    Processing Code:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
        String[] data = split(val, ",");
        //if(data != null && data.length == 14){ 
          if(data.length > 14){
          println("REPORT: length="+data.length);
          println("Incoming data content:");
          println(val);
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    
    //println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    
  • How to split incoming data from arduino

    println(data);still gives me issues. When I put println(val)in two different places before your code, it only works in one spot:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    String[] data;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
        println(val);       //HERE IT PRINTS FINE
        if(data!=null&&data.length==14){
          println(val);         //HERE NOTHING PRINTS AT ALL
          data = split(val, ",");
          println("REPORT: length="+data.length);
          println("Incoming data content:");
          println(val);
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    
    //println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    

    I have been looking all over and I cant find anything that works.

  • How to split incoming data from arduino
    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    String[] data;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
    
        if(data!=null&&data.length==14){
          data = split(val, ",");
          println("REPORT: length="+data.length);
          println("Incoming data content:");
          println(data);
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
     // println(val);
    } 
    
    //println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    

    So i put in the code that you gave me and changed the >14 to ==14 and noted out my print() and nothing happens. The only thing that happens is a yellow line under println(data); with the warning of "Type String[] of the last argument to method println(Object...) doesn't exactly match the vararg parameter type. cast to Object[] to conform the non-varargs invocation, or pass individual arguments of type Object for a varargs invocation," Help

    EDIT: When i change println(data); to println(val); the warning goes away.

  • How to split incoming data from arduino

    So i switched lines 25 and 26 and then it gave me an error on the new line 25 which is the line with if(data!=null&&data.length>14){ It says "data" doesn't exist so i initialized String[] data; at the beginning and no longer got that error. Then i run the sketch. i didn't get any errors but all the data still comes out as 1's and 0's and doesn't change at all.

    The updated code with lines 25 and 26 switched:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    String[] data;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){        
        if(data!=null&&data.length>14){
          data = split(val, ",");
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    
  • How to split incoming data from arduino

    So i switched lines 25 and 26 and then it gave me an error on the new line 25 which is the line with if(data!=null&&data.length>14){ It says "data" doesn't exist so i initialized String[] data; at the beginning and no longer got that error. Then i run the sketch. i didn't get any errors but all the data still comes out as 1's and 0's and doesn't change at all.

    The updated code with lines 25 and 26 switched:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    String[] data;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
    
        if(data!=null&&data.length>14){
          data = split(val, ",");
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    
  • How to split incoming data from arduino

    Im not sure if I put it in the right spot in my code because when I println() all the new values they are either 1's or 0's and nothing changes. Here's the code with the new lines added:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
        String[] data = splitTokens(val, ",");
        if(data!=null&&data.length>14){             //ADDED IT HERE
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    

    EDIT: It sometimes will give the right data but in the wrong order and still nothing changes.

  • How to split incoming data from arduino

    Thx @kfrajer, works great. EDIT: It works great sometimes. I added the rest of the information:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
        String[] data = splitTokens(val, ",");
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
    
      }
    } 
    println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",
    tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    

    But I get a new error that iv never seen before, and, like the NPE I looked it up and didn't understand how to fix it. The error is NumberFormatException on a random line that has Integer.parseInt(data[]) in it. Please help me fix this.

    EDIT AGIAN: this is the error that it gives me: NumberFormatException: For input string: "135" The number 135 is actually anywhere between 134 to 136.

  • How to split incoming data from arduino

    Yes, you need to make sure you don't have a null object, like this:

    val = myPort.readStringUntil('\n');
    if(val!=null){
       //...your code here
    }
    

    Also make sure you have 4 tokens after parsing....

    Kf

  • How to split incoming data from arduino

    So, I tried the split() function and it runs for a moment, then it gives me the NullPointerExeption error. Here's the code:

        import processing.serial.*;
    
        Serial myPort;  // Create object from Serial class
        String val;     // Data received from the serial port
        void setup()
        {
          String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
          myPort = new Serial(this, portName, 9600);
        }
        void draw()
        {
          if ( myPort.available() > 0) 
          {  // If data is available,
          val = myPort.readStringUntil('\n');         // read it and store it in val
          String[] data = split(val, ",");
          int button1 = Integer.parseInt(data[0]);
          int button2 = Integer.parseInt(data[1]);
          int button3 = Integer.parseInt(data[2]);
          int button4 = Integer.parseInt(data[3]);
        } 
        println(val); //print it out in the console
        }
    

    I get the NPE error on the line String[] data = split(val, ",");. How do I fix this. I looked at some pages on the error and I cant figure it out.