controlp5 string out of bound error

edited August 2017 in Arduino

I am writing a control program for a small robot to send gcode command over the serial port to a arduino. The sketch works for sometime and then crashes randomly.. giving the stringoutofbound error.

this is the code i used ( wont work without arduino )

I am new to using controlp5 and it might be a basic mistake but i cant seem to figure it out..Thanks

//@esk 2017

import controlP5.*; import processing.serial.*;

ControlP5 cp5; Textarea myTextarea; String inBuffer; Serial myPort; // The serial port Knob rot_x; Knob rot_y; Knob feed; int feed_value = 500;

String textValue = "";

void setup() { size(710,360);

PFont font = createFont("arial",20);

cp5 = new ControlP5(this);

cp5.addTextfield("commands") .setPosition(10,300) .setSize(270,40) .setFont(createFont("arial",12)) .setAutoClear(false) ;

cp5.addBang("clear") .setPosition(310,300) .setSize(50,40) .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER) ;

myTextarea = cp5.addTextarea("txt") .setPosition(500,10) .setSize(200,330)

              .setLineHeight(14)
              .setColor(color(200))
              .setColorBackground(color(255,100))
              ;   

textFont(font);

cp5.addButton("X_left") .setValue(0) .setPosition(70,120) .setSize(50,50) ; cp5.addButton("X_right") .setValue(0) .setPosition(170,120) .setSize(50,50) ; cp5.addButton("Y_up") .setValue(0) .setPosition(120,70) .setSize(50,50) ; cp5.addButton("Y_down") .setValue(0) .setPosition(120,170) .setSize(50,50) ; //fast buttons

cp5.addButton("X_left_10") .setValue(0) .setPosition(10,120) .setSize(50,50) ; cp5.addButton("X_right_10") .setValue(0) .setPosition(230,120) .setSize(50,50) ; cp5.addButton("Y_up_10") .setValue(0) .setPosition(120,10) .setSize(50,50) ; cp5.addButton("Y_down_10") .setValue(0) .setPosition(120,230) .setSize(50,50) ;

// homes cp5.addButton("X_top") .setValue(0) .setPosition(10,10) .setSize(80,80) ;

cp5.addButton("X_bot")
 .setValue(0)
 .setPosition(10,200)
 .setSize(80,80)
 ;

cp5.addButton("Y_top") .setValue(0) .setPosition(200,10) .setSize(80,80) ;

cp5.addButton("Y_bot")
 .setValue(0)
 .setPosition(200,200)
 .setSize(80,80)
 ;

//REPORT BUTTONS

  cp5.addButton("position")
 .setValue(0)
 .setPosition(310,200)
 .setSize(50,80)
 ;

 cp5.addButton("motor_off")
 .setValue(0)
 .setPosition(310,120)
 .setSize(50,50)
 ; 
   cp5.addButton("motor_on")
 .setValue(0)
 .setPosition(310,10)
 .setSize(50,80)
 ; 

// utilities cp5.addButton("end_stop") .setValue(0) .setPosition(380,300) .setSize(50,40) ; cp5.addButton("sens_val") .setValue(0) .setPosition(440,300) .setSize(50,40) ;

//knobs

rot_x = cp5.addKnob("rot_x") .setRange(-100,100) .setValue(0) .setPosition(390,10) .setRadius(40) .setDragDirection(Knob.HORIZONTAL) ; rot_y = cp5.addKnob("rot_y") .setRange(-100,100) .setValue(0) .setPosition(390,200) .setRadius(40) .setDragDirection(Knob.VERTICAL) ;

feed = cp5.addKnob("feed") .setRange(10,500) .setValue(100) .setPosition(405,120) .setRadius(25) .setDragDirection(Knob.HORIZONTAL) ;

printArray(Serial.list()); // Open the port you are using at the rate you want: myPort = new Serial(this, Serial.list()[0], 57600); background(0); fill(255);

}

void draw() { background(0); fill(255);

while (myPort.available() > 0) { inBuffer = myPort.readString();
if (inBuffer != null) { println(inBuffer); myTextarea.setText(inBuffer); } } //myPort.write("hello"); }

public void clear() { cp5.get(Textfield.class,"textValue").clear(); }

void controlEvent(ControlEvent theEvent) { if(theEvent.isAssignableFrom(Textfield.class)) { println(theEvent.getName()+": "+theEvent.getStringValue() ); }

}

public void commands(String theText) { // automatically receives results from controller input println("a textfield event for controller 'input' : "+theText); myPort.write(theText); //myPort.write(';');

}

// button functions

public void X_left(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G91;"); myPort.write ("G0 X-2"+" "+"F"+feed_value+";"); }

public void X_right(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G91;"); myPort.write ("G0 X2"+" "+"F"+feed_value+";"); }

public void Y_up(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G91;"); myPort.write ("G0 Y2"+" "+"F"+feed_value+";"); }

public void Y_down(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G91;"); myPort.write ("G0 Y-2"+" "+"F"+feed_value+";"); }
// FAST BUTTONS

public void X_left_10(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G91;"); myPort.write ("G0 X-10"+" "+"F"+feed_value+";"); }

public void X_right_10(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G91;"); myPort.write ("G0 X10"+" "+"F"+feed_value+";"); }

public void Y_up_10(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G91;"); myPort.write ("G0 Y10"+" "+"F"+feed_value+";"); println("a button event from X+: "+"G0 Y10"+"F"+feed_value+";"); } public void Y_down_10(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G91;"); myPort.write ("G0 Y-10"+" "+"F"+feed_value+";"); }

// home

public void X_top(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G28;"); //myPort.write ("G0 Y-10;"); }

public void X_bot(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G29;"); //myPort.write ("G0 Y-10;"); }

public void Y_top(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G30;"); //myPort.write ("G0 Y-10;");

}

public void Y_bot(int theValue) { println("a button event from X+: "+theValue); myPort.write ("G31;"); //myPort.write ("G0 Y-10;"); }

// extra

public void motor_off(int theValue) { println("a button event from X+: "+theValue); myPort.write ("M18;"); //myPort.write ("G0 Y-10;"); }

public void motor_on(int theValue) { println("a button event from X+: "+theValue); myPort.write ("M17;"); //myPort.write ("G0 Y-10;"); }

public void position(int theValue) { println("a button event from X+: "+theValue); myPort.write ("M114;"); //myPort.write ("G0 Y-10;"); }

// rotary knobs

void rot_x(int theValue) { int position = theValue;

String toSend = "G0 X" +position+" "+"F"+feed_value+";"; myPort.write("G90;"); myPort.write(toSend);

println("a knob event. setting background to "+toSend); }

void rot_y(int theValue) { int position = theValue; myPort.write("G90;"); myPort.write("G0 Y" +position +" "+"F"+feed_value+";"); println("a knob event. setting background to "+theValue); }

void feed(int theValue) { feed_value = theValue; //myPort.write("G0 Y" +position +"F"+feed+";"); println("a knob event. setting background to "+theValue); }

//utilities

public void sens_val(int theValue) { println("a button event from X+: "+theValue); myPort.write ("M256;"); //myPort.write ("G0 Y-10;"); }

public void end_stop(int theValue) { println("a button event from X+: "+theValue); myPort.write ("M119;"); //myPort.write ("G0 Y-10;"); }

this is the error i keep getting

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1967) at controlP5.ControlFont.calculateHeight(Unknown Source) at controlP5.ControlFont.adjust(Unknown Source) at controlP5.Label$MultilineLabel.draw(Unknown Source) at controlP5.Label.draw(Unknown Source) at controlP5.ControllerGroup.draw(Unknown Source) at controlP5.ControllerGroup.drawControllers(Unknown Source) at controlP5.ControllerGroup.draw(Unknown Source) at controlP5.ControlWindow.draw(Unknown Source) at controlP5.ControlWindow.draw(Unknown Source) at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1427) at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1420) at processing.core.PApplet.handleMethods(PApplet.java:1614) at processing.core.PApplet.handleDraw(PApplet.java:2450) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Answers

  • Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format it. Make sure there is an empty line above and below your code.

    Kf

Sign In or Register to comment.