positioning String

edited July 2015 in Arduino

hi everyone, I do not know how to change the position of the variable " val " to be in the position I want, this is my code..

 import controlP5.*;
 import processing.serial.*;
 
 
 ControlP5 cP5a;
 ControlP5 cP5b;
 ControlP5 cP5c;
 ControlP5 cP5d;
 ControlP5 cP5e;
 ControlP5 cP5f;
 ControlP5 cP5g;
 ControlP5 cP5h;
 
 Serial arduino;
 String val= "0";
 
 void setup() 
 {
   size(785, 520);
   println(Serial.list());
   String portName = Serial.list()[0];
   arduino = new Serial(this, portName, 9600);
 
 
   cP5a = new ControlP5(this);
   cP5a.addSlider("ATUR_KECEPATAN_MOTOR_DC", 0, 255, 0, 30, 155, 350, 50);
   cP5b = new ControlP5(this);
   cP5b.addButton("FORWARD",1,30,220,100,40);
   cP5c = new ControlP5(this);
   cP5c.addButton("REVERSE",0,280,220,100,40);
   cP5d = new ControlP5(this);
   cP5d.addButton("BUKA_GERBANG",1,30,300,100,40);
   cP5e = new ControlP5(this);
   cP5e.addButton("TUTUP_GERBANG",1,30,355,100,40);
   cP5f = new ControlP5(this);
   cP5f.addSlider("SUDUT_SERVO", 0, 180, 0, 30, 435, 350, 50);
   cP5g = new ControlP5(this);
   cP5g.addButton("MASTER_ON",1,30,80,100,35);
   cP5h = new ControlP5(this);
   cP5h.addButton("MASTER_OFF",0,160,80,100,35);
 }
 
 void draw() 
 {
   background(#614DED);
   textSize(20);
   fill(#F5ED00);
   text("PENGENDALI MOTOR DC MOTOR STEPPER DAN MOTOR SERVO", 100, 30);
   textSize(18);
   text("KECEPATAN MOTOR DC :", 500, 140);
   textSize(18);
   text("FIANSYAH AFANDI RAKHMAN, TEKNIK ELEKTRO, UNIVERSITAS SILIWANGI TASIKMALAYA", 10,60);
   textSize(15);
   text("A. MOTOR DC", 15,140);
   textSize(15);
   text("B. MOTOR STEPPER", 15,285);
   textSize(15);
   text("C. MOTOR SERVO", 15,420);
   textSize(13);
   fill(#020108);
   text("created by: Fiansyah Afandi Rakhman", 545, 500);
   text("107002025/elektro2010", 617, 515);
 
   if (arduino.available() >0){
     delay(100);
   val=arduino.readString();}  
   println(val);
 
 text(val, width/2, height/2);
 }
 void controlEvent(ControlEvent theEvent) {
   if(theEvent.isController()) {
 
   print("control event from : "+theEvent.controller().name());
   println(", value : "+theEvent.controller().value());
 
  if(theEvent.controller().name()=="ATUR_KECEPATAN_MOTOR_DC") {
    int val1 = int(theEvent.getController().getValue());
    arduino.write("a" + val1);
  }
  if(theEvent.controller().name()=="FORWARD") {
    int val2 = int(theEvent.getController().getValue());
   arduino.write("b" + val2 );
  }
  if(theEvent.controller().name()=="REVERSE") {
    int val3 = int(theEvent.getController().getValue());
    arduino.write("c" + val3);
     }
  if(theEvent.controller().name()=="BUKA_GERBANG") { 
     int val4 = int(theEvent.getController().getValue());
    arduino.write("d" + val4 );
  }
  if(theEvent.controller().name()=="TUTUP_GERBANG") { 
     int val5 = int(theEvent.getController().getValue());
   arduino.write("e" + val5 );
  }
  if(theEvent.controller().name()=="SUDUT_SERVO") {
    int val6 = int(theEvent.getController().getValue());
   arduino.write("f" + val6);  
  }
  if(theEvent.controller().name()=="MASTER_ON") {
    int val7 = int(theEvent.getController().getValue());
   arduino.write("g" + val7);
  }
  if(theEvent.controller().name()=="MASTER_OFF") {
    int val8 = int(theEvent.getController().getValue());
  arduino.write("h" + val8);
    }
   }
 }

I want to ask is how to adjust the position of " val " to be positioned in the position I wanted.

 if (arduino.available() >0){
     delay(100);
     val=arduino.readString();
 }  
 println(val);
 
 text(val, width/2, height/2);

Answers

  • edited July 2015

    you use:

    text(val, width/2, height/2);

    you mean where the text is written on the screen?

    just use

    text(val, 30, 30);
    

    or

    text(val, 230, 40);
    

    etc.

  • edited July 2015

    Thank you verymuch , Crishir . in that way

    "text ( val , 30 , 30 ) "

    the original figures appear , be nothing . I 've tried like this ,

    " text ( val , width/2 , height/2 , 30 , 30 "

    and as

    "text ( val , 30 , 30 , width/2 , height/2 "

    but it does not make what I want to happen . Can you give me any other suggestions ?? thankyou verymuch.

  • the original figures appear , be nothing .

    Do you mean that no characters are displayed at all, or "0" is being displayed?

  • if (arduino.available() >0){
         delay(100);
         val=arduino.readString();
     }  
     println(val + " length >>  " + val.length()); // Display the number of characters in the string
    
     text(val, width/2, height/2);
    

    If the output is
    length >> 0
    or
    length >> 1

    then that is the problem. If 0 then the arduino is returning an empty string. In the second case it is returning a non-displayable character, probably the end of line character.

    In both cases there is nothing wrong with your code, it just means thatval has no displayable characters.

    Try this and tell us what happened.

  • in the other thread

    http://forum.processing.org/two/discussion/11518/displaying-string-from-arduino#latest

    you said, the problem (getting data from arduino) is solved (by using trim(val) )

    do you use trim() now?

    • when println works and text doesn't work that is a hint for us to follow to find the error.

    How text() works

    As for the text():

    usage is

    text ( TextToDisplay, whereX, whereY );

    OR

    displaying text in a rectangle: we can tell text() how the width and height of the text rect must be

    text ( TextToDisplay, whereX, whereY, width, height );

    other than rect() whereX, whereY with text() can mean something different - afaik.

    I think whereX, whereY means lower left corner (instead of upper left corner with rect())

    Therefore, maybe you display the text with wrong position

    ;-)

  • delay(100);

    better use

    delay(1);

  • edited July 2015

    I wrote

    • when println works and text doesn't work that is a hint for us to follow to find the error.

    is that true?

    does println work and text does not?

    Remark

    also you need a proper fill() to use with text() such as fill(255); when you have a black background(). Also your textsize() must be 22 or so

  • edited July 2015

    please use ctrl-t in the processing IDE

    your code:

    instead of

        if (arduino.available() > 0) {
             delay(100);
             val=arduino.readString();
         }  
         println(val);
    
         text(val, width/2, height/2);
    

    now

    if (arduino.available() > 0) {
    
         delay(2); // why?
    
         val = arduino.readString();
    
         val  = trim (val) ;  // !!!!!!!!!!!!!!!!!!!!!!!!
    
         println(val);
         fill(255);   
         text(val, width/2, height/2);
    
     }  // if 
    
Sign In or Register to comment.