COULD NOT RUN THE SKETCH - processing & arduino

Hi everyone, i have some questions about processing. when i tried to run it with arduino, i had somme problems like "Could not run the sketch" "Could not run the sketch (Target VM failed to initialize, for mor information, read revisions.txt and Help -> Troubleshooting.)"

The code : import processing.serial.*;

Serial myport; String portStream;

int B1in = 0; int B2in = 0;

void setup() {

size(500,500); background(255); stroke(160); fill(50); String portName = Serial.list()[2]; myport = new Serial(this, portName, 9600); myport.bufferUntil('\n'); }

void draw() { if(portStream != null) { if (portStream.length() == 6 && portStream.charAt(0) == 'S' && portStream.charAt(3) == '3') { B1in = int(portStream.substring(1,2)); B2in = int(portStream.substring(2,3));

if (B1in == 1) {
  fill(0,255,0);
}
else {
  fill(255,0,0);
}
rect(20,20,160,160);

if (B2in == 1) {
  fill(0,255,0);
}
else {
  fill(255,0,0);
}
rect(200,20,160,160);

} }

} void serialEvent(Serial myPort) { portStream = myPort.readString(); }

I am a beginner for using processing and arduino, Can anyone can tell me what's wrong with this problem ?

Thx !!!!!!

Answers

  • Please format your code. Edit your post, select your code and hit ctrl+o. Ensure there is a line above and below your code.

    Kf

  • Can you run other examples? Your code seems ok.

    Kf

  • edited February 2017
    import processing.serial.*;
    
    Serial myport;
    String portStream;
    
    int B1in = 0;
    int B2in = 0;
    
    void setup() {
    
      size(500,500);
      background(255);
      stroke(160);
      fill(50);
      String portName = Serial.list()[2];
      myport = new Serial(this, portName, 9600);
      myport.bufferUntil('\n');
    }
    
    void draw() {
      if(portStream != null) {
        if (portStream.length() == 6 
          && portStream.charAt(0) == 'S' 
          && portStream.charAt(3) == '3')
        {
          B1in = int(portStream.substring(1,2));
          B2in = int(portStream.substring(2,3));
    
          if (B1in == 1) {
            fill(0,255,0);
          } else {
            fill(255,0,0);
          }
          rect(20,20,160,160);
    
          if (B2in == 1) {
            fill(0,255,0);
          } else {
            fill(255,0,0);
          }
          rect(200,20,160,160);
        }
      }
    }
    
     void serialEvent(Serial myPort) {
       portStream = myPort.readString();
    }
    
  • We need to figure out if it is your code or processing? Can you run any other example? The one that comes with processing through the menu File>>Examples?

    Also can you tell us your OS, Processing version?

    Kf

  • i found this code on the internet for testing my project with processing and arduino. I can run all exemples of processing, but it doesn't work with arduino.

  • Can you post the code that you are loading in your arduino?

    Kf

  • edited February 2017
      const int buttonPin1 = 2;
      const int buttonPin2 = 10;
    
      void setup() {
       Serial.begin(9600);
       pinMode(buttonPin1, INPUT);
       pinMode(buttonPin2, INPUT);
       }
    
      void loop() {
    
     if (digitalRead(buttonPin1) == 1) {
      Serial.println("B1");
     }
    
     if (digitalRead(buttonPin2) == 1) {
     Serial.println("B2");
     }
    
      delay(20);
     }
    
     String normalizeData(int b1, int b2) {
    
      String B1string = String(b1);
      String B2string = String(b2);
    
      String ret = String("S") + B1string + B2string + String("E");
      return ret;
    
     }
    
  • Answer ✓

    attention in your sketch, "E" Arduino and "3" processing (?), I tried to send this by Arduino and your Processing sketch it seems to work

    const int buttonPin1 = 2;
    const int buttonPin2 = 10;
    void setup() {
      Serial.begin(9600);
      pinMode(buttonPin1, INPUT);
      pinMode(buttonPin2, INPUT);
    }
    void loop() {
      String B1string = String(digitalRead(buttonPin1));
      String B2string = String(digitalRead(buttonPin2));
      String ret = String("S") + B1string + B2string + String("3");
      Serial.println(ret);
      delay(200);
    }
    
  • Thx, i have changed the code et it works, but i still dont have image, so strange 1486479366(1

  • Your code is really hard to read. This is the main reason screenshots should not be used in a forum - referring to screenshots of code. This next tip might not solve your problem. Introduce a delay of 1 sec or so at the end of your setup in your Processing sketch.

    Also, what do you get when you do the following change:

    void draw() {
       if(portStream != null) {
         println("Received: "+portStream)
         ...REST OF YOUR CODE HERE...
    

    Kf

  • edited February 2017

    How could you put code with the form 1,2,3,4... ? I have tried another code that my friend has already used, but it doesn't work either. it shows info like " [0] "COM7" _____NULL_______could not run the sketch (Target VM failed to initialize. For more information, read revisions, txt and Help ->Troubleshooting)

    I try to put my code here:

         import processing.serial.*;
    
        ArrayList<Blob> blobs; //tableau des classes nommee comme <<blob>>
        float initRad = 100;//
        int checkreal=0; // variable detecter la reponse de la capteur
        boolean birth=false;// variable qui est a juger si generer une nouvelle class 6       <<blob>>
         int total;//variable definir le nombre totale du class <<blob>>
         int vit=2; // vitesse de la deplacement des objets <<blob>>
        int previous;
        int current;
    
        String inString;
    
       void setup() {
      l'sffichage.
       size(600, 600);
       colorMode(HSB,100);
       background(100);
       previous = 0;
       current = 0;
    
       blobs = new ArrayList<Blob>();
    
       printArray(Serial.list());
       Serial arduino = new Serial(this, Serial.list()[0], 115200); 
       arduino.bufferUntil('\n');
       }
    
        void draw() {
    
        println("__"+inString+"__");
        if (inString.length() != 6 && inString.charAt(0) != '\n')
        {
        current = Integer.parseInt(Character.toString(inString.charAt(0)));
        }
        else {current = 0;}
    
    
        println("integer : " +current);
        total=blobs.size();
        if (previous==0 && current==1){
        birth =true;
        } else {
        birth = false;
        }
    
       if(birth){
       increase();
        }
    
        for (int i = blobs.size()-1; i > 0; i--) {
        Blob b = blobs.get(i);
       b.update();
        b.display();
        }
    
       previous = current;
       }
    
        void increase() {
        float x=random(0,width);
        float y=random(0,height);
       float radius =random(initRad/3, initRad);
       int form= total%4;
        blobs.add(new Blob(new PVector(x,y), new PVector(random(-vit, vit),random(-  
        vit,vit)), radius, color(random(100),100,80,80),form));
         birth=false;
        }
        {
       color c;
      float rad;
      int patt;
      PVector loc, vel, acc;
    
    Blob(PVector loc, PVector vel, float rad, color c, int patt) {
    this.loc = loc;
    this.vel = vel;
    this.rad = rad;
    this.acc = new PVector(0, 0);
    this.patt = patt;
    this.c = c;
      }
    
     void display() {
    
    fill(c);
    noStroke();
    if( patt== 0){
    ellipse(loc.x, loc.y, rad, rad);}
    if (patt == 1){
      triangle( loc.x, loc.y, loc.x-rad, loc.y-rad, loc.x+rad, loc.y-rad);
    };
    if( patt ==2 ){
      rect(loc.x, loc.y, rad, rad);}
       else if(patt ==3){
      quad(loc.x, loc.y, loc.x-rad/2, loc.y-rad/2, loc.x+rad/2, loc.y-rad/2,          
    
       loc.x+rad/2, loc.y);}
        }
    
       void update() {
    
       vel.add(acc);// mettre a jour la vittese 
       loc.add(vel);// mettre a jour les locations des points
       edges();//judger si changer l'orientation des deplacements
        }
    
    
        void edges() {
        if (loc.x > width) {
        loc.x = width;
         vel.x *= -1;
         } else if (loc.x < 0) {
          loc.x = 0;
          vel.x *= -1;
          }
    
          if (loc.y > height) {
          loc.y = height;
          vel.y *= -1;
        } else if (loc.y < 0) {
        loc.y = 0;
        vel.y *= -1;
        }
      }
     }
    
     void serialEvent(Serial p)
      {
       inString = p.readString();
    

    }

  • Please format your code. Edit your post, select your code and hit ctrl+o. Ensure there is a line above and below your code.

    Kf

Sign In or Register to comment.