How to transform String in float : float value = float(cm); ? Help

edited December 2015 in Arduino

Hi everyone,

Currently I make a project for learn how to discuss between sensor and processing. So Processing read correctly the port of arduino ( I can see the value with processing)

My problem it's a cannot use this data for make something ( example I want to hide or show like influenced the opacity of an picture). For influenced the opacity I use a white rect but maybe is not a good idea.

Because ( maybe) I've string informations and for make a "if" I use a float . I search differents things but i'm lost with the differents method to are not very clear. I search to transform "cm" in float var1 for use my if. After I thinking to make a map for transform the data.

        // the code with proximity sensor and "cm" like string value. 


        import cc.arduino.*;
        import org.firmata.*;
        import processing.serial.*;

        Serial myPort;   
        String cm;
        float var1;
         PImage fonds;


        void setup() {

           size(displayWidth, displayHeight); //full screen
          println(Serial.list());
          myPort = new Serial(this, Serial.list()[6], 9600);  // go to read the COM of arduino
         myPort.bufferUntil('\n');
          fonds = loadImage("testu.jpg");
           image(fonds, 0, 0,displayWidth,displayHeight); 

        }

        void draw() {


        while (myPort.available() > 0) { //Continue usb serial
            String cm = myPort.readStringUntil('\n'); // Read port analog
            if(cm!=null) {
             cm = trim(cm);

              float var1 = float(cm); // The problem is here how to transform this value

               var1 = map (var1, 0, 700, 0, 255);
            println(cm); // affichage
            println(var1);

            }                       
        }
        if (var1 > 100){                 // currently he don't read this part
         noStroke();
         fill(255,0);
        rect(0, 0,displayWidth,displayHeight);
        smooth();
        }

        else { 
           noStroke();
         fill(255,10);
        rect(0, 0,displayWidth,displayHeight);
        smooth();
        } 

        }

Sorry for my english if i make some mistake. Thank's Kinds regards

Answers

  • go back edit your post (click on the gear and then on edit) please

    then format your code correctly - empty line before and after, highlight / select the entire code and hit ctrl-o

  • first use your println() to see what values cm / var1 you get from arduino - are they like 0.4, 0.6 etc. or 10,40,60 ?

    next make a second sketch without arduino to test your fill().

    this complete sketch doesn't work:

    size(displayWidth, displayHeight);
    
    noStroke(); 
    fill(255, 0);   //  !!!!!!!!!!!!!!!!!!!!!!!!!!!  you need fill(255);  or fill(255, 78); 
    rect(0, 0, displayWidth, displayHeight); 
    smooth(); 
    

    you need to change the fill() line

    you can use a println ("here 12"); within the if-clause to see whether he is getting there at all. When he gets there, and your fill() is wrong, you'll still see nothing

  • Currently I have my background picture and just after send play the rect begin progressively full white, I change fill(255,95) without arduino its the same.

    The cm value is between 6 and 80 cm so more for value like 10,40,60

    I have this message NotaNumber archi

  • edited December 2015

    this part is outside the while-statement

    if (var1 > 100){  
    

    hence you are testing only the last var1 not all of them

    Hint

    in processing (not in the forum) hit ctrl-t. It gives you automatic better indents

  • Hep its better but I cannot control the fill of the rect again. The picture go slowly full white but don't come back clear.

    I have the same message NaN. Its strange I try to change my map and the if but he make the same speed for add some white.

    var1 = map (var1, 0, 30, 0, 255); if (var1 > 28) { noStroke(); fill(255, 95); rect(0, 0, displayWidth, displayHeight); smooth();

    Thank's for your patience

  • message NaN.

    in which line?

  • use ctrl-t

    then post your entire code please.

    did you put if inside while?

  • edited December 2015

    So now normally the "if" is in the while and for NaN its when he println(var1); So the problem its var1 for NaN.

    import cc.arduino.*;
    import org.firmata.*;
    import processing.serial.*;
    
    Serial myPort;   
    String cm;
    float var1;
    PImage fonds;
    
    
    void setup() {
    
      size(displayWidth, displayHeight); //full screen
      println(Serial.list());
      myPort = new Serial(this, Serial.list()[6], 9600);  // go to read the COM of arduino
      myPort.bufferUntil('\n');
      fonds = loadImage("testu.jpg");
      image(fonds, 0, 0, displayWidth, displayHeight);
    }
    
    void draw() {
    
    
      while (myPort.available() > 0) { //Continue usb serial
        String cm = myPort.readStringUntil('\n'); // Read port analog
        if (cm!=null) {
          cm = trim(cm);
    
          float var1 = float(cm); // MY problem is here how to transform some value
    
          var1 = map (var1, 0, 30, 0, 255);
          println(cm); // affichage
          println(var1);
    
    
          if (var1 > 30) { 
            noStroke();
            fill(255, 95);
            rect(0, 0, displayWidth, displayHeight);
            smooth();
          } else { 
            noStroke();
            fill(255, 0);
            rect(0, 0, displayWidth, displayHeight);
            smooth();
          }
        }
      }
    }
    
  • you wrote:

    NaN its when he println(var1);

    what does println cm give you in this case?

    say in line 26 : if (cm!=null && !cm.equals("") ) {

  • edited December 2015 Answer ✓

    You cannot convert a whole sentence to a number. But if the first part is always the same, you could just remove the first 15 characters from the string and parse the rest.

    // this is the original message
    String message = "Distance en cm :13\n";
    
    // remove line-breaks
    message = trim(message);
    
    // remove the first part
    String valueString = message.substring(16);
    
    // convert to integer
    int value = int(valueString);
    
    println(value);
    

    But i would actually try to change the arduino-code, and send only the values without the "Distance en cm :"

  • edited December 2015

    Indeed my mistake its to combine sentence and data

    So now its functionnal I just need to change some parameters for the map and the background but its ok I go to continue.

    Thanks guys !

    import cc.arduino.*;
    import org.firmata.*;
    import processing.serial.*;
    
    Serial myPort;   
    String cm;
    float var1;
    PImage fonds;
    
    
    void setup() {
    
      size(displayWidth, displayHeight); //full screen
      println(Serial.list());
      myPort = new Serial(this, Serial.list()[6], 9600);  // go to read the COM of arduino
      myPort.bufferUntil('\n');
      fonds = loadImage("testu.jpg");
      image(fonds, 0, 0, displayWidth, displayHeight);
      fonds.resize(displayWidth, displayHeight);
    }
    
    void draw() {
    
      frameRate(10);
      while (myPort.available() > 0) { //Continue usb serial
        String cm = myPort.readStringUntil('\n'); // Read port analog
        if (cm!=null) {
          cm = trim(cm);
    
          float var1 = float(cm); // MY problem is here how to transform some value
    
          var1 = map (var1, 0, 50, 0, 255);
          println(cm); // affichage
          println(var1);
    
    
          if (var1 > 170) { 
            noStroke();
            fill(255, 95);
            rect(0, 0, displayWidth, displayHeight);
            smooth();
          } 
          if (var1 < 170) { 
           background(fonds);
           image(fonds, 0, 0, displayWidth, displayHeight);
          }
        }
      }
    }
    
  • Well done!

Sign In or Register to comment.