How to connect these two codes?

edited December 2017 in Arduino

Hi,

I want to connect these two codes, but don't know how to do it. Basically, what I want to do is when mouse is pressed, it changes to the second code.

Here is first code


int a = 0;

void setup(){
  fullScreen();
  
  rectMode(CENTER);
  frameRate(100);
  noCursor();
}

void draw() {
  
  int W = displayWidth;
  int H = displayHeight;
  
  if(a < displayWidth ) {
    background(0);  
    
    translate(W/2, H/2);
    for (float i = -3000; i < W; i += 200){
      if(a+i <= displayWidth){
        
        
        noFill();
        strokeWeight(10);
    
        rect(0, 0, a + i, (a + i) * H / W);
        if(a + i > 0) {
          stroke(255);
        } else {
          noStroke();
        }
      }
    }
    //noFill();
    //strokeWeight(10);

    //rect(0, 0, a, a * H / W);
    //stroke(255);      
    
    //a = a - 10;
    a = a + 1;
  } else {
    a = 0;
  }
}

Here is the second code:


import processing.serial.*;
Serial myPort;

float angleX = 0.0;
float angleY = 0.0;

void setup(){
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);
  fullScreen();
  noFill();
  stroke(255);
  strokeWeight(4);
  
  //frameRate(60);
}

String val;

void draw() {
  
  float startX = 100;
  float startY = 20;
  float x = 200;
  float two = sqrt(2);
  float a = x / two;
  float b = x - a;
  
  if (myPort.available() > 0) {
    String val = myPort.readStringUntil('\n');
    String[] list = split(val, 'a');
    if(val != null) {
      int number = int (list[0].trim());
      int number2 = int (list[1].trim());
      background(0);
      // left
      pushMatrix();
      translate(startX, startY + b);
      rotate(radians(angleX));
      rect(0, 0, b, a);
      popMatrix();
      
      pushMatrix();
      translate(startX, startY + a + b);
      rotate(radians(-angleY));
      rect(0, 0, b, a);
      popMatrix();
      
      pushMatrix();
      translate(startX, startY + 2 * a + b);
      rotate(radians(angleX*3));
      rect(0, 0, b, a);
      popMatrix();
    
      pushMatrix();
      translate(startX, startY + 3 * a + b);
      rotate(radians(-angleY*2));
      rect(0, 0, b, a);
      popMatrix();
      
      // top
      pushMatrix();
      translate(startX + b, startY);
      rotate(radians(angleY*5));
      rect(0, 0, a, b);
      popMatrix();
      
      pushMatrix();
      translate(startX + a + b, startY);
      rotate(radians(-angleX*2));
      rect(0, 0, a, b);
      popMatrix();
        
      // right
      pushMatrix();
      translate(startX + b + a * 2, startY + b);
      rotate(radians(angleY*7));
      rect(0, 0, b, a);
      popMatrix();
      
      pushMatrix();
      translate(startX + b + a * 2, startY + a + b);
      rotate(radians(-angleX*3));
      rect(0, 0, b, a);
      popMatrix();
      
      pushMatrix();
      translate(startX + b + a * 2, startY + 2 * a + b);
      rotate(radians(angleY));
      rect(0, 0, b, a);
      popMatrix();
    
      pushMatrix();
      translate(startX + b + a * 2, startY + 3 * a + b);
      rotate(radians(-angleX*5));
      rect(0, 0, b, a);
      popMatrix();
        
      // bottom
      
      pushMatrix();
      translate(startX + b, startY + a * 4 + b);
      rotate(radians(angleY*3));
      rect(0, 0, a, b);
      popMatrix();
      
      pushMatrix();
      translate(startX + a + b, startY + a * 4 + b);
      rotate(radians(-angleX));
      rect(0, 0, a, b);
      popMatrix();
      
      angleX = number;
      angleY = number2;
      
      // debugging
      
      textSize(32);
      text(number, width/2, height/2 + 20); 
      text(number2, width/2, height/2 - 20);
     
      if(number == 357 && number2 == 357){
        text("done", width/2 + 100, height/2 + 20);
      }
      
    }
  }
}

Answers

  • edited December 2017 Answer ✓

    Okay! You have two codes, and they both have this format:

    // Some global stuff
    
    void setup(){
      fullScreen();
      // ... More stuff here.
    }
    
    void draw(){
      // ... More stuff here.
    }
    

    Right?

    In the first code, rename setup() to setup_first() and remove the call to fullScreen(). Then rename draw() to draw_first().

    In the second code, renamesetup() to setup_second() and remove the call to fullScreen(). Then rename draw() to draw_second().

    Now your merged code looks like this:

    // All the global stuff from both codes here.
    
    int state = 0; // Add this.
    
    void setup(){
      fullScreen();
      setup_first();
      setup_second();
      state = 0;
    }
    
    void draw(){
      if( state == 0 ){
        draw_first();
      } else {
        draw_second();
      }
    }
    
    void setup_first(){
      // ... More stuff here.
    }
    
    void draw_first(){
      // ... More stuff here.
    }
    
    void setup_second(){
      // ... More stuff here.
    }
    
    void draw_second(){
      // ... More stuff here.
    }
    
    void mousePressed(){
      state = 1;
    }
    
Sign In or Register to comment.