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
-
/* 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);
-
//----------------------------
-
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);
-
}
-
}
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.