So, I'm trying to create a really simple program that goes like Processing(start)--> Arduino(sensor data grabbing)-->Processing (data plotting). The first interaction is just processing sending a 1 whenever the mouse clicks in a box. This is done through serial.write. Then Arduino receives the 1 and starts recording data from sensors, this works out easily. This data is then sent back to processing. Incoming data triggers serialEvent and then I would plot the data somehow.
Problem is serial.write triggers serialEvent as soon as the program starts. I'm quite sure is not a bug, that's just how it works. Anyway this makes things really hard, because it forces me to use a physical button to avoid the first serial.write part.
Is there any way to use maybe a special character only to trigger serialEvent, or any other workaround?
ANY help is really appreciated!
Assuming that you are sending 1 from the InputBox of the Serial Monitor by clicking on the Send Button; if so, do not declare the SerialEvent() handler in your code; rather, include the following codes in the loop() function to grasp arrived 1.
void loop()
{
byte n = Serial.available()
if(n != 0)
{
char x = Serial.read(); //you have 0x31 in the variable x
//---- take actions as needed-------------
}
}
Ok, thanks for the answer, but I think you misunderstood or more likely I'm too noob at programming ( just one week in!).
The error I get is in the processing part, not in Arduino.
Or I'm just writing the void loop() into processing instead of Arduino?
Because I'm sending the 1s to Arduino, and then a chunk of data, lets make it 17,15,2,18,10 straight back to processing. but actually the 1 is triggering serialEvent, which is in processing!
Thanks for your answers, I'm really getting invested in programming but sometimes i just get stuck...
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker
...R
Hey thanks a lot Robin, crystal clear guide, the part about avoiding SerialEvent was what I was looking for. I'm trying to make a working script asap, if it doesn't work I'll post more here.