Problems with converting a String to a float

Hi! Im trying to display my sensor temperature thats hooked up with the arduino.
The project is basiclly to display temperature in little rectangles with the colors:RGB, but to do that I first have to split my String and then convert the second part of the String to a float while leaving the first part a String and giving it a new name.I have succesfully split my String and list[0] works fine (list[0] being the first part I was talking about), But for some reason whenever i try to use list[1] it keeps returning a ArrayIndexOutOfBoundsExeption: error. Could somebody help me with this? Here is the Code:

Processing code:

1 import processing.serial.*;

2 Serial myPort;
3 int lf = 10;
4 float temp =0.1;
5 String s =null

6 void setup() {
7 myPort = new Serial(this, “COM5”, 9600);
8 myPort.clear();
9 s = myPort.readStringUntil(lf);
}

10 void draw() {
11 while (myPort.available() > 0) {
12 s = myPort.readStringUntil(lf);

13 if (s != null) {
14 String[] list = split(s, “,”);

15 String sen1 = list[0];

16 float temp = float(list[1]);

17 delay(600);
}
}
}

The ERROR occurs at line:16

That is where the error apears.

If you have an out of bounds exception, it probably means that you try to access an element of the array that does not exist.

So my guess is that this line: String[] list = split(s, ","); does not behave the way you expect.

Did you print s in the console to see how it looks like?

Also can you please format your code with the </> icon? Thanks.

The first element index in an array is zero so if s does not contain a comma it will return a single element with the array index 0 so try

float temp = float(list[0]);

Yes! It works when I print: "print(list); " but when I try to convert it to a float the error appears.

Can you please post your code for this?

Since the error is being generated here
16 float temp = float(list[1]);

Then the problem is not caused by the conversion of a String to a float it is because the array length is less than 2 i.e. 0 or 1. It can’t be 0 because the exception would have been thrown by line 15. The only way I can see that happening is if the String s does not contain a comma.

Please show an example of s that generates this exception and the output from print list otherwise we are all working in the dark and having to guess possible causes.

Arduino code:

//

//    FILE: DS18B20_two_sensors.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: demo with two sensors (on two pins)
//
// HISTORY:
// 0.0.1 = 2017-07-25 initial version

#include <OneWire.h>
#include <DS18B20.h>

// numbers chosen to match pin numbers..
#define ONE_WIRE_BUS2   2
#define ONE_WIRE_BUS3   3
#define ONE_WIRE_BUS4   4

OneWire oneWire2(ONE_WIRE_BUS2);
OneWire oneWire3(ONE_WIRE_BUS3);
OneWire oneWire4(ONE_WIRE_BUS4);

DS18B20 sen1(&oneWire2);
DS18B20 sen2(&oneWire3);
DS18B20 sen3(&oneWire4); 


void setup(void)
{
  Serial.begin(9600);
  Serial.println(__FILE__);
  Serial.print("DS18B20 Library version: ");
  Serial.println(DS18B20_LIB_VERSION);

  sen1.begin();
  sen2.begin();
  sen3.begin();

  // different resolution shows nicely the async behavior
  sen1.setResolution(11);
  sen2.setResolution(11);
  sen3.setResolution(11);

  sen1.requestTemperatures();
  sen2.requestTemperatures();
  sen3.requestTemperatures();
}


void loop(void)
{
  // print the temperature when available and request a new reading
  if (sen1.isConversionComplete())
  {
    //Serial.print(" , ");
    Serial.print("sen1");
    Serial.print(",");
    Serial.println(sen1.getTempC(),1);//
    //Serial.print(","); 
    sen1.requestTemperatures();
    delay(500);

  }
  
  if (sen2.isConversionComplete())
  {
    //Serial.print(",");
    Serial.print("sen2");
    Serial.print(",");
    Serial.println(sen2.getTempC(),1);
    sen2.requestTemperatures();
    //Serial.print(" , ");
    delay(500);
  }
  
  if (sen3.isConversionComplete())
  {
   //Serial.print(" , ");
   Serial.print("sen3");
   Serial.print(",");
   Serial.println(sen3.getTempC(),1);
   //Serial.print(" , ");
   sen3.requestTemperatures();
   delay(500);  
   
  }
  delay(1000);
}

Hello,

I can assist if you edit your original code and format it as per:
https://discourse.processing.org/faq#format-your-code

:)

Processing code:

import processing.serial.*;

Serial myPort;
int lf = 10;
float temp =0.1;
String s =null



void setup() {
myPort = new Serial(this, "COM5", 9600);
myPort.clear();
s = myPort.readStringUntil(lf);
}

void draw() {
while (myPort.available() > 0) {
s = myPort.readStringUntil(lf);

if (s != null) {
String[] list = split(s, ",");
 
String sen1 = list[0];

float temp = float(list[1]);


delay(600);
    }
   } 
  }

Do you mean the arduino code that I use?

Arduino code:

  //
//    FILE: DS18B20_two_sensors.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: demo with two sensors (on two pins)
//
// HISTORY:
// 0.0.1 = 2017-07-25 initial version

#include <OneWire.h>
#include <DS18B20.h>

// numbers chosen to match pin numbers..
#define ONE_WIRE_BUS2   2
#define ONE_WIRE_BUS3   3
#define ONE_WIRE_BUS4   4

OneWire oneWire2(ONE_WIRE_BUS2);
OneWire oneWire3(ONE_WIRE_BUS3);
OneWire oneWire4(ONE_WIRE_BUS4);

DS18B20 sen1(&oneWire2);
DS18B20 sen2(&oneWire3);
DS18B20 sen3(&oneWire4); 


void setup(void)
{
  Serial.begin(9600);
  Serial.println(__FILE__);
  Serial.print("DS18B20 Library version: ");
  Serial.println(DS18B20_LIB_VERSION);

  sen1.begin();
  sen2.begin();
  sen3.begin();

  // different resolution shows nicely the async behavior
  sen1.setResolution(11);
  sen2.setResolution(11);
  sen3.setResolution(11);

  sen1.requestTemperatures();
  sen2.requestTemperatures();
  sen3.requestTemperatures();
}


void loop(void)
{
  // print the temperature when available and request a new reading
  if (sen1.isConversionComplete())
  {
    //Serial.print(" , ");
    Serial.print("sen1");
    Serial.print(",");
    Serial.println(sen1.getTempC(),1);//
    //Serial.print(" , "); 
    sen1.requestTemperatures();
    //delay(400);

  }
  
  //if (sen2.isConversionComplete())
  //{
    //Serial.print(" , ");
    //Serial.print("sen2:\t");
    //Serial.print(" , ");
    //Serial.println(sen2.getTempC(),1);
    //sen2.requestTemperatures();
    //Serial.print(" , ");
    //delay(1000);
  //}
  
  //if (sen3.isConversionComplete())
  //{
   //Serial.print(" , ");
   //Serial.print("sen3:\t");
   //Serial.print(" , ");
   //Serial.println(sen3.getTempC(),1);
   //Serial.print(" , ");
   //sen3.requestTemperatures();
   //delay(1000);  
   
  //}
  delay(500);
}
   


Hello,

Take a look at this:

Arduino
void setup(void)
  {
  Serial.begin(9600);
  Serial.println("DS18B20 Library version: ");
  }

void loop(void)
  {
  Serial.print("sen1");
  Serial.print(",");
  Serial.println(float (millis()));
  delay(1000);
  }
Processing
import processing.serial.*;

Serial myPort;
int lf = 10;
float temp =0.1;
String s = null;

void setup() 
  {
  // List all the available serial ports:
  printArray(Serial.list()); 
  
  myPort = new Serial(this, "COM6", 9600);
  //myPort.clear();
  s = myPort.readStringUntil(lf);
  //println("s0:", s);
  }

void draw() 
  {
  while (myPort.available() > 0) 
    {
    s = myPort.readStringUntil(lf);
    //println("s1:", s);

    if (s != null) 
      {
      println("s2:", s);
      String[] list = trim(split(s, ","));
      //String[] list = split(s, ",");
      //String sen1 = list[0];
      //println("sen1: ", sen1);
      //printArray(list);
      
      if(list.length == 2)
        {
        printArray(list);
        float temp = float(list[1]);
        println("temp: ", temp);
        }
      //delay(600);
      }
    }
  }

I wrote some test code on Arduino that I know the behavior of from the Arduino serial monitor.

I added some println() statements and other code to the Processing side to monitor incoming data.

trim() will removed the “linefeed” from the string; this may not be necessary if the float conversion can remove it. I still remove it in all code I write.

You can comment and uncomment as you desire to see what is going on when testing your code.

I also checked for an expected and valid array length before trying to read data from it.

My first pass at this with some tweaks to your code:

Also, I just worked through your code… there are other ways to do this.

I prefer to use:
serialEvent() \ Language (API) \ Processing 3+

Please format your first post in this topic; it is messy.

:)

1 Like

Thank you very much! This worked!

2 Likes