Conversion of string in text for Led Control

Problem to display a text on the display window of processing.

I wrote a programm in processing that build two buttons on the display window, click on the green button and send to Arduino the orden to switch on a Led. click on the red button send the order to switch off the Led.

Here is the code:

1. // Ard_to_pross_

2. /* If the programm receives a "1" from processing,it will switch on a Led on pin 10 and send back to
3. processing the word ON

4. If the programm receives a "2" from processing,it will switch off the Led on pin 10 and send back to
5. processing the word OFF
6. */


7.  char val;                // Data received from serial port
8.  int ledPin = 10;         // Set the pin to digital I/O 10
9.  boolean ledState = LOW;  // to toggle our LED

10. void setup() 
11.   {
12.   // Serial.println("LED CONTROL  (send A: ON,  send B:  OFF )");
13.    pinMode(ledPin,OUTPUT);              // Declare pin 10 as an output
14.    //digitalWrite (10,LOW);               // Declare initial state as OFF (LOW)
15.    Serial.begin(9600);                  // Serial Port speed
16.    establishContact();                  // send a byte to establish contact until receiver responds
17.   }

18. void loop() 
19. {
20.   if (Serial.available()> 0) // if serial available functions returns 1
21.                                 // it means that some data has been received in the
22.                                 // serial buffer of Arduino
23.     {
24.       val = Serial.read();
25.       if (val == '1')         // if we get a "1"
26.        {
27.         digitalWrite (ledPin,HIGH);   // Turn ON Led
28.             Serial.println("ON");      //  Return ON
29.         }
30.            delay (100);    
31.         
32.        if (val == '2')          //Check if an '2' has been received
33.         {
34.        digitalWrite (ledPin,LOW);    // Turn OFF Led
35.        Serial.println("OFF");          //  Return OFF
36.        delay(50);
37.         }
38.   }
39. }
40. ```

41. void establishContact() {
42.   while (Serial.available() <= 0)
43.   {
44.   Serial.println("A");          // send a capital A to process
45.   delay(300);
46.   }
47. }


The programm in processing is:


// Proc to ARD

  1. /* Control Switching of LED (LED-COntrol)

  2. Programm draw two button, a green and a red one.

  3. Click on the green button will send a 1 to Arduino to switch on the Led

  4. Arduino will answer with the word "ON" that shoul be displayed on the corresponding green button

  5. Click on the red button will send a 2 to Arduino to switch off the Led

  6. Arduino will answer with the word "OFF"that shoul be displayed on the corresponding red button

  7. */

  8. import processing.serial.*; //import the Serial library

  9. Serial myPort; //the Serial port object

  10. String val;

  11. String on = "ON";

  12. String off = "OFF";

  13. Boolean firstContact = false; // Since we are doing serial handshaking

  14.                            // we need to check if we have heard from the microcontroller
    
  15. void setup() {

  16. size(300, 300); //make our canvas 200 x 200 pixels big

  17.                //  initialize your serial port and set the baud rate to 9600
    
  18.                // myPort = new Serial(this, Serial.list()[4], 9600);
    
  19. String portName = Serial.list() [0];

  20. // myPort = new Serial(this, portName, 9600);

  21. myPort = new Serial(this, Serial.list() [0], 9600);

  22. // myPort.bufferUntil ('\n');

  23. }

  1. void draw() {

  2.       //we can leave the draw method empty, 
    
  3.       //because all our programming happens in the serialEvent (see below)
    
  4.       // draw: Called directly after setup() and continuously executes the lines of code contained inside
    
  5.       //its block until the program is stopped or noLoop() is called
    
  6. fill(106,242,10); // Green (ON)

  7. rect(50, 50, 50,50);

  8. fill(15,13,13);

  9. text("LED ON", 55,45);

  10. fill(245,49,15); // Red (OFF)

  11. rect(200, 50, 50, 50);

  12. fill(15,13,13);

  13. text("LED OFF", 205,45);

  14. if (mousePressed == true)

  15. {

  16. if(mouseX >= 50 && mouseX < 100 && mouseY >= 50 && mouseY < 100)

  17. {

  18. //println(" Mouse pressed, LED ON");

  19.  myPort.write('1');        //send a 1
    
  20.  // println("                                           Mouse pressed, send 1 to arduino");
    
  21.     delay   (100);
    
  22. }

  23. if(mouseX >= 200 && mouseX < 250 && mouseY >= 50 && mouseY < 100)

  24. {

  25. //println(" Mouse pressed, LED OFF");

  26.  myPort.write('2');        //send a 2
    
  27.   //println("                                           Mouse pressed, send 2 to arduino");
    
  28.   delay   (100);
    
  29. }

  30. }

  31. if (myPort.available() > 0)

  32. {  // if data available
    
  33. val = myPort.readStringUntil ('\n');  // read it and store it in val
    
  34. }
    
  35. println (val);
    
  36. //----------------------------

  37. if (val == on)
    
  38.  {
    
  39.    fill(106,242,10); // Green (ON)
    
  40. rect(50, 50, 50,50);

  41.  fill(15,13,13); 
    
  42. // text("ON", 65,80);

  43.  }
    
  44. //----------------------------

  45. if (val == off)

  46. {

  47. fill(245,49,15); // Red (OFF)

  48. rect(200, 50, 50, 50);

  49. fill(15,13,13);

  50. text("OFF",210,80);

  51. }

  52. }



My problem, and I cannot solve for already a week is that when I get the feedback from Arduino, I cannot write the words "on" and "off" on the corresponding buttons, I don't get how to convert the value of the string "val" in text in the display window. 
On the console I can see that the two words are received (  println (val);) line of code Nr 67.

Thank you before hand for you time and help.

Sorry, but your code is unreadable.
Please insert it according the forum rules using code tags.

@condorp

Here is your code, formatted and in code blocks. You should also provide a wiring drawing of your project. It will make this more understandable.

ARDUINO TO PROCESS

// Ard_to_pross_

/*
  If the programm receives a "1" from processing
  - it will switch on the Led on pin 10 and
  - send back to processing the word ON
  If the programm receives a "2" from processing,
  - it will switch off the Led on pin 10 and
  - send back to processing the word OFF
*/

char val;                // Data received from serial port
int ledPin = 10;         // Set the pin to digital I/O 10
boolean ledState = LOW;  // to toggle our LED

void setup() {
  // Serial.println("LED CONTROL  (send A: ON,  send B:  OFF )");
  pinMode(ledPin, OUTPUT);  // Declare pin 10 as an output
  //digitalWrite (10,LOW);               // Declare initial state as OFF (LOW)
  Serial.begin(9600);  // Serial Port speed
  establishContact();  // send a byte to establish contact until receiver responds
}

void loop() {
  if (Serial.available() > 0) { // if serial available functions returns 1
    // it means that some data has been received in the serial buffer of Arduino
    val = Serial.read();
    if (val == '1') {  // if we get a "1"
      digitalWrite(ledPin, HIGH);  // Turn ON Led
      Serial.println("ON");        //  Return ON
    }
    delay(100);
    if (val == '2') { //Check if an '2' has been received
      digitalWrite(ledPin, LOW);  // Turn OFF Led
      Serial.println("OFF");      //  Return OFF
      delay(50);
    }
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print("A");  // send a capital A to process
    Serial.write(' ');  // send a NUL to process
    delay(300);
  }
}

PROCESS TO ARDUINO

// Proc to ARD

/* Control Switching of LED (LED-COntrol)

    Programm draw two buttons
    - a green button and
    - a red button

    Click on the green button will
    - send a 1 to Arduino to switch on the Led
    - Arduino will answer with the word "ON"
    - that should be displayed on the corresponding green button

    Click on the red button will
    - send a 2 to Arduino to switch off the Led
    - Arduino will answer with the word "OFF"
    - that should be displayed on the corresponding red button
*/

import processing.serial.*; //import the Serial library
Serial myPort; // the Serial port object
String val;
String on = "ON";
String off = "OFF";
Boolean firstContact = false;
  //  Since we are doing serial handshaking we need to 
  //  check if we have heard from the microcontroller

void setup() {
  size(300, 300); //make our canvas 200 x 200 pixels big
  String portName = Serial.list() [0];
  myPort = new Serial(this, Serial.list() [0], 9600);
  //  initialize your serial port and set the baud rate to 9600
  // myPort = new Serial(this, Serial.list()[4], 9600);
  // myPort = new Serial(this, portName, 9600);
  // myPort.bufferUntil ('\n');
}

void draw() {
  /*
    we can leave the draw method empty, because all our programming happens in the serialEvent
    (see below) draw: Called directly after setup() and continuously executes the lines of code
    contained inside its block until the program is stopped or noLoop() is called
  */

  fill(106, 242, 10); // Green (ON)
  rect(50, 50, 50, 50);
  fill(15, 13, 13);
  text("LED ON", 55, 45);
  fill(245, 49, 15); // Red (OFF)
  rect(200, 50, 50, 50);
  fill(15, 13, 13);
  text("LED OFF", 205, 45);

  if (mousePressed == true) {
    if (mouseX >= 50 && mouseX < 100 && mouseY >= 50 && mouseY < 100) {
      //println(" Mouse pressed, LED ON");
      myPort.write('1');        //send a 1
      // println("                                           Mouse pressed, send 1 to arduino");
      delay   (100);
    }

    if (mouseX >= 200 && mouseX < 250 && mouseY >= 50 && mouseY < 100) {
      //println(" Mouse pressed, LED OFF");
      myPort.write('2');        //send a 2
      //println("                                           Mouse pressed, send 2 to arduino");
      delay   (100);
    }
  }

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

  println (val);

  if (val == on) {
    fill(106, 242, 10); // Green (ON)
    rect(50, 50, 50, 50);
    fill(15, 13, 13);
    // text("ON", 65,80);
  }

  if (val == off) {
    fill(245, 49, 15); // Red (OFF)
    rect(200, 50, 50, 50);
    fill(15, 13, 13);
    text("OFF", 210, 80);
  }
}

@xfpd Thank you for correcting my post.

@b707.
xfpd was so kind to correct it for me. Being new in this forum I am not quite certain how the thing works. Sorry about that

They actually anticipated that and put this thread at the top of every single board on the forum.

Hello. Would you show information about your project and how it is connected? You can hand-draw a picture, or supply a link to the information. It will help with answering your question.

The "project " is very simple. I want to draw in processing two buttons to switch on and off an Led that is connected to PWm Pin 10 in Arduino Mega2560.
The program function like this, click on the green Button and processing send to Arduino a "1". Arduino turn on the Led on pin 10 and send back to processing the word "ON".
Click on the red Button and processing send to Arduino a "2". Arduino turn on the Led off pin 10 and send back to processing the word "OFF".

Everything functions ok except that when I get the feedback, it is supposed to write the words "ON" and "OFF" on the on the buttons (to know the order did arrive in Arduino).

This is the part that doesn't work, The String "val" carry the information from Arduino, I can see that contains the desired words because I print it on the processing console, but when I write an If to check if val = ON or OFF it doesn't work. Obviously that is the problem and second to print it on the processing window I suppose I need to change the String into text and I cannot get it to work.

The circuit I don't know how to include the picture of the circuit, but is very symple, USB connection between Arduino MEGA 2560 and the PC with processing. Arduino PWM Pin 10 connected to Led. All this works fien, the change of String "val" to text is my problem.
Circuit

Finally I gto to upload the draft

What does this show (just before sending "on/off" to the the screen)?

Thank you for your time.

The "If" check that there is data available in the port and assign the characters to the string "val" until a carriage return is found (?n). The intruction to print is actually there because I tried to see if what is arriving is OK. Actually it is, I receive the ON and OFF words according to what the Led does in Arduino and the words "ON" and "OFF" are displayed on the Processing Console. But when I want to print those two words on the Buttons on the Processing screen it doesn't work. I am doing something wrong, but I cannot figure it out.

this prints the value of the string "val" on the processing console and it shows the right thing. Only when I try to print it on the processing screen (the red and green buttons) nothing happens.

You have these two conditionals:

if (val == on)

and

if (val == off)

Examine their condition to see if you are acting on the correct result, for example:

Serial.print (val == on);

and

Serial.print (val == off);

Would be nice if you posted the cade for this "processing screen" whatever it is. We have no idea what your code looks like.

Normally if I want to use a button to change an output pin on an Arduino or similar I just use a single button for On and Off. Click the button and the button Text changes from OFF to ON and you can also change the button background color. Click on button changes the text.

Anyway less seeing your form code there is no way to diagnose your problem.

Ron

I haven't ever used Processing. But it seems like we may be missing the point of the question. I think the title would be better, "how to change the text on a button" rather than something about conversion of text. I think the OP has the text, they just don't know how to change the button. Seems like a Processing question and not an Arduino question to me.

Blockzitat

Hallo Ron_Blain, thank you for your help
Hier is the code:

Blockzitat

// Ard_to_pross    ARDUINO CODE

/* If the programm receives a "1" from processing,it will switch on a Led on pin 10 and send back to
processing the word ON

If the programm receives a "2" from processing,it will switch off the Led on pin 10 and send back to
processing the word OFF
*/


 char val;                // Data received from serial port
 int ledPin = 10;         // Set the pin to digital I/O 10
 boolean ledState = LOW;  // to toggle our LED

void setup() 
  {
  // Serial.println("LED CONTROL  (send A: ON,  send B:  OFF )");
   pinMode(ledPin,OUTPUT);              // Declare pin 10 as an output
   //digitalWrite (10,LOW);               // Declare initial state as OFF (LOW)
   Serial.begin(9600);                  // Serial Port speed
   establishContact();                  // send a byte to establish contact until receiver responds
  }

void loop() 
{
  if (Serial.available()> 0) // if serial available functions returns 1
                                // it means that some data has been received in the
                                // serial buffer of Arduino
    {
      val = Serial.read();
      if (val == '1')         // if we get a "1"
       {
        digitalWrite (ledPin,HIGH);   // Turn ON Led
            Serial.println("ON");      //  Return ON
        }
           delay (100);    
        
       if (val == '2')          //Check if an '2' has been received
        {
       digitalWrite (ledPin,LOW);    // Turn OFF Led
       Serial.println("OFF");          //  Return OFF
       delay(50);
        }
  }
}

void establishContact() {
  while (Serial.available() <= 0)
  {
  Serial.println("A");          // send a capital A to process
  delay(300);
  }
}

Blockzitat
Hier is the code in processing, There may be a bit of
rubish because I am trying to debug it

Blockzitat

// Proc to ARD   PROCESSING CODE


/* Control Switching of LED   (LED-COntrol)

Programm draw two button, a green  and a red one.
Click on the green button will send a 1 to Arduino to switch on the Led
Arduino will answer with the word "ON" that shoul be displayed on the corresponding green button
Click on the red button will send a 2 to Arduino to switch off the Led
Arduino will answer with the word "OFF"that shoul be displayed on the corresponding red button
*/


import processing.serial.*;     //import the Serial library

Serial myPort;                //the Serial port object


 
String val;

String on = "ON";
String off = "OFF";

Boolean firstContact = false;  // Since we are doing serial handshaking
                               // we need to check if we have heard from the microcontroller


void setup() {
  size(300, 300);  //make our canvas 200 x 200 pixels big
                   //  initialize your serial port and set the baud rate to 9600
                   // myPort = new Serial(this, Serial.list()[4], 9600);
String portName = Serial.list() [0];                   
 // myPort = new Serial(this, portName, 9600);
 myPort = new Serial(this, Serial.list() [0], 9600);
// myPort.bufferUntil ('\n');
 }
 




void draw() {
          //we can leave the draw method empty, 
          //because all our programming happens in the serialEvent (see below)
          // draw: Called directly after setup() and continuously executes the lines of code contained inside
          //its block until the program is stopped or noLoop() is called

  
 fill(106,242,10); // Green (ON)
 rect(50, 50, 50,50);
  fill(15,13,13); 
  text("LED ON", 55,45);

 fill(245,49,15); // Red (OFF)
 rect(200, 50, 50, 50);
  fill(15,13,13); 
  text("LED OFF", 205,45);
  
  
 if (mousePressed == true)
{
if(mouseX >= 50 && mouseX < 100 && mouseY >= 50 && mouseY < 100)
 { 
  //println("                                           Mouse pressed, LED ON");
     myPort.write('1');        //send a 1
     // println("                                           Mouse pressed, send 1 to arduino");
        delay   (100);
 }
 if(mouseX >= 200 && mouseX < 250 && mouseY >= 50 && mouseY < 100)
 { 
  //println("                                           Mouse pressed, LED OFF");
     myPort.write('2');        //send a 2
      //println("                                           Mouse pressed, send 2 to arduino");
      delay   (100);
   }
  }
  
  
  if  (myPort.available() > 0)
    {  // if data available
    val = myPort.readStringUntil ('\n');  // read it and store it in val
    }
   
   
    println (val);

Serial.print (val == on);
   
//----------------------------    
    if (val == on)
     {

       fill(106,242,10); // Green (ON)
 rect(50, 50, 50,50);
     fill(15,13,13); 
  text(val, 65,80);
 
     }
  
//---------------------------- 
  

  
  if (val == off)
  {
   fill(245,49,15); // Red (OFF)
  rect(200, 50, 50, 50);
  fill(15,13,13); 
  text(val,210,80);

  }
}

You see, I did the following test at the beginning the te processing code there are two strings defined, on and off,

if a use the statement: text (on,65,80); it print it on the processing screen as I wish. If I do the same with the string val I get an error "NullPointerException".
So, if the string is defined on the code, it works, if it comes from Arduino it doesnt work
Something comes from Arduino with the string val that make it fail. (may by a carriage return [\n]. The question is how I take it out to try.
"

Blockzitat
You see, I did the following test, at the beginning of the processing code there are two strings defined, on and off,

if a use the statement: text (on,65,80); it print it on the processing screen as I wish. If I do the same with the string val I get an error "NullPointerException".
So, if the string is defined on the code, it works, if it comes from Arduino it doesnt work
Something comes from Arduino with the string val that make it fail. (may by a carriage return [\n]. The question is how I take it out to try.

Blockzitat

Serial.print (val == on);

Blockzitat
I tried to use as you suggested the Serial instruction but it gave the error
The function2print(boolean)" does not exist
Blockzitat

Change this line to:

String val = "NothingReceivedYet";

And try the same code.

Blockzitat

You are right, it is a processing issue. I have tried to get into processing forum but I never receive the confirmation E-Mail and all intents to contact them have failed.

Blockzitat
I try to use the initialization of the string val as you suggested, but nothing happens. I also thought iz was an initialization problem