My post of yesterday Sending String with MyPort.write from processing to arduino. - Programming Questions - Arduino Forum
I made a post yesterday about sending a String with myPorty.write();
As response someone told me to follow example 2 guide of this post.
Example 2 guide i found = Serial Input Basics - updated - Introductory Tutorials - Arduino Forum
From my point of view when i press my mouse button when running the gui of Processing my led should turn to high.
Processing code
import processing.serial.*;
Serial myPort; // Create object from Serial class
void setup()
{
size(200, 200); //make our canvas 200 x 200 pixels big
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw() {
if (mousePressed == true)
{ //if we clicked in the window
myPort.write("hallo"); //send a 1
println("test");
} else
{ //otherwise
myPort.write('0'); //send a 0
}
}
Arduino code
// Example 2 - Receive with an end-marker
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
int ledPin = 3;
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
digitalWrite(ledPin, HIGH); // turn the LED on
delay(1000);
digitalWrite(ledPin, LOW); // turn the LED on
}
}
Can someone tell me what i am doing wrong or show me how to send a string with myPort.write();.