-1

I'm doing this project where I'm using an Arduino Board to read the heartbeat pulses, so I want to send from the Arduino to Processing only one number (the BPM of the person); so that I can use that information into my Processing code to manipulate some visual image that I have created in Processing. My problem at the moment is that that information is in String(), so I can't use it as a variable - Int() or float() - and I'm having difficulties in making this conversion, from String to Int().

This is my code at the moment:

import processing.serial.*;
Serial myPort;  // Create object from Serial class
String val;

void setup () {
  size(600,600);
  noStroke();
  String portName = Serial.list()[0]; 
  myPort = new Serial(this, portName, 115200);
} 
float t=0;

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

  if ( myPort.available() > 0) {  // If data is available,
  val = myPort.readStringUntil('\n');  // read it and store it in val
  println(val);
  }

  translate(width/2, height/2);
  beginShape ();
  //add some vertices...
  for (float theta =0; theta <= 2*PI; theta += 0.01){
   float rad = r(theta,
     2, //a
     2, //b
     6, //m
     1, //n1
     cos(t)*0.9+0.7  , //n2
     sin(t)*0.9+0.7  //n3
     );

   float x = rad * cos(theta)*100;
   float y = rad * sin(theta)*100;
   vertex (x,y);
}
  endShape(CLOSE);
  t+=0.04;
}
float r(float theta, float a, float b, float m, float n1, float n2, float n3 ) {
  return pow(pow(abs(cos(m* theta/4.0) /a), n2) + pow(abs(sin(m* theta/4.0) /b), n3), -1.0/n1);
} 

I have tried this to convert the String() to Int() but it doesn´t seem to work:

 Serial myPort;
   String val;
   int valInt;

 void draw ();
   if ( myPort.available() > 0)  {  
   val = myPort.readStringUntil('\n');         
   int valInt = Integer.parseInt(val);
   println(valInt);
}
  • 1
    Have you tried using the `int()` function? Can you please post a [mcve]? – Kevin Workman Nov 26 '19 at 18:10
  • I don't know what you mean. Please edit your post. – AndiCover Nov 26 '19 at 18:12
  • @AndiCover Read the tag info <[processing](https://stackoverflow.com/tags/processing/info)>. [Processing](https://processing.org/) is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. – Rabbid76 Nov 26 '19 at 18:29
  • @Rabbid76 That does not make the question more readable. The question is hard to read and that is all I am criticising. – AndiCover Nov 26 '19 at 19:10
  • @AndiCover I see your point. – Rabbid76 Nov 26 '19 at 19:11
  • @KevinWorkman, I have tried to change it to int() function but it doesn't work. – Andreia Pereira Nov 26 '19 at 19:46
  • @AndreiaPereira Can you please be more specific than saying it doesn't work? What does it do instead? What error do you see? Can you please post a [mcve] that we can run to see the problem ourselves? – Kevin Workman Nov 26 '19 at 19:47
  • @KevinWorkman, just post my current code and also what I tried to do to fix my problem. I apologise for the confusion. – Andreia Pereira Nov 26 '19 at 20:53
  • @AndreiaPereira Have you tried [debugging your code](https://happycoding.io/tutorials/processing/debugging)? What is the value of `val` before you try to convert it? What error are you getting? – Kevin Workman Nov 26 '19 at 22:20
  • @KevinWorkman before I try to convert it the programme runs fine, and I can see on my screen both of the image I code and the information that is coming from the Arduino. And I don't know how to answer what is the value of `val`. – Andreia Pereira Nov 26 '19 at 23:32
  • @AndreiaPereira Did you read the link I just posted? You can debug your program using print statements or the Processing debugger. – Kevin Workman Nov 26 '19 at 23:41
  • @KevinWorkman, yes I have tried to use the Processing debugger, but I don´t know if I did anything wrong or if it is meant to take a long time because it didn't show me any results. – Andreia Pereira Nov 27 '19 at 00:02
  • @AndreiaPereira To use the debugger, you need to set a breakpoint and then step through the code. Or you could add a print statement. – Kevin Workman Nov 27 '19 at 00:30

4 Answers4

2

In Processing there is the built-in function int, which can convert a string (or a floating point value) to an integral number:

String s = "123";
int x = int(s);
print(x);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • My String is not just one "statement"; the values are always changing since the data is coming live from the Arduino board. Would it still work this example you just gave me? – Andreia Pereira Nov 26 '19 at 19:49
  • @AndreiaPereira What do you mean by *"statement"* in this context. Do you've a variable which contains the string? If yes, then you can use `int()` to convert the string to a integral number or [`float()`](https://processing.org/reference/floatconvert_.html), to convert to a floating point number. By the way, some code would improve your question. – Rabbid76 Nov 26 '19 at 19:57
  • I appolagize for the confusion I just edit my question with my code there. – Andreia Pereira Nov 26 '19 at 20:38
  • What do you mean? Just by adding `int(val)` and then changing on the `myPort.readStringUntil('\n');`? – Andreia Pereira Nov 26 '19 at 20:50
  • @AndreiaPereira `val = myPort.readStringUntil('\n');` reads a string and stores the string in `val`. `int valInt = int(val);` converts the string to an integral value. – Rabbid76 Nov 26 '19 at 20:54
  • "NullPointerException" is the message that it gives me when I do that. – Andreia Pereira Nov 26 '19 at 21:05
  • @AndreiaPereira You've to set `val` before `int(val)`. `String val;` is not initialized. – Rabbid76 Nov 26 '19 at 21:11
  • How can I fix it? Because the problem seems to be what you have said, about the `String val` not being initialised, but I don't know what to put as value for `val.` – Andreia Pereira Nov 26 '19 at 23:59
  • @Pereira Init `val`, e.g.`String val = "";`. But why do you convert (`int(val)`) When nothig is assigned to it!?!? Init `intVal`, e.g. `int intVal = 0;` and chang it if you get a new `val` – Rabbid76 Nov 27 '19 at 05:11
  • @Rabbit76, I have done that but it keeps saying "NullPointerException" because of this line `int valInt = int(val) ;` , even after Init all. – Andreia Pereira Nov 27 '19 at 12:52
0

As Per mine understanding of your question, i have following solution You can use following code to convert String value to int.

int a = Integer.parseInt("51");

51 is string value which you can convert to int.

user9626527
  • 106
  • 9
  • That's not the way to do this in [Processing](https://processing.org/reference/). In Processing there is the built-in function [`int()`](https://processing.org/reference/intconvert_.html) – Rabbid76 Nov 26 '19 at 18:23
  • Hi Rabbid76 may i know what is meant by processing and int() function. I thought of program processing, is it something different? – user9626527 Nov 26 '19 at 18:27
  • 1
    Please read the tag info <[processing](https://stackoverflow.com/tags/processing/info)>. [Processing](https://processing.org/) is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. The meaning of "processing" is not in a general way. – Rabbid76 Nov 26 '19 at 18:28
  • Thanks Rabbid76 to share – user9626527 Nov 26 '19 at 18:28
-1
String str = "123";
int i = Integer.parseInt(str);
println(i); // 123
Community
  • 1
  • 1
-1
          String str="22";
          Int  value= Integer.valueOf(str);
Subrata Das
  • 135
  • 1
  • 12
  • My String is not just one "statement"; the values are always changing since the data is coming live from the Arduino board. Would it still work this example you just gave me? – Andreia Pereira Nov 26 '19 at 19:55
  • https://stackoverflow.com/questions/54921049/reading-from-serial-port-gives-split-up-string – Subrata Das Nov 27 '19 at 07:41