The draw() method is made redundant because the screen updating is handled by the serialEvent() in response to serial data communication with the Arduino UNO
Here is the Processing sketch code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| import processing.serial.*; Serial myPort; String sensorReading=""; PFont font; void setup() { size(400,200); myPort = new Serial(this, "COM13", 9600); myPort.bufferUntil('\n'); font = createFont(PFont.list()[2],32); textFont(font); } void draw() { //The serialEvent controls the display } void serialEvent (Serial myPort){ sensorReading = myPort.readStringUntil('\n'); if(sensorReading != null){ sensorReading=trim(sensorReading); } writeText("Sensor Reading: " + sensorReading); } void writeText(String textToWrite){ background(255); fill(0); text(textToWrite, width/20, height/2); } |
If you don't know what COM port to use, then you may wish to use
ReplyDeleteprintln(Serial.list());
to display the available COM ports that processing can communicate with. This was suggested by PaulS in the Arduino forum
http://www.arduino.cc/forum/index.php/topic,72700.0.html
Good day. Thank you for this tutorial. I have a question about multiple readings. How do I display multiple readings from the arduino to processing. For example, I would like to display temperature sensor readings, rpm sensor readings and voltage sensor readings. How can this be done. Thank you
ReplyDeleteThis will eventually be covered in my progressive Serial Communication Tutorial which can be found here:
Deletehttp://arduinobasics.blogspot.com/2012/07/arduino-basics-simple-arduino-serial.html
u are smart, great article!!!!!!!!!!!!!!!
ReplyDeleteDear Anonymous,
ReplyDeleteI would say that you may have left out a bracket or semi colon or something like that. I am using a Windows PC, so not sure how you would do this with a MAC. If you still have no luck, then perhaps look to the Arduino Forums for more clues.
Original comment by anonymous was deleted due to the size of the message.
DeleteThanks a lot.
ReplyDeleteIf the serial setup can be done with:
Serial.begin(9600);
why to use:
myPort = new Serial(this, "COM13", 9600);
Is there any benefit?
"Serial.begin(9600);" is used in the Arduino IDE, while
Delete"myPort = new Serial(this, "COM13", 9600);" is used in Processing.
They are two different programming environments. The reason they look
so much alike is that the Arduino IDE is based on Processing.
Thank you.
DeleteWhen one should use Processing? And I think there is extra options in it? What is it?
Have a look at this site:
Deletehttp://www.processing.org/reference/libraries/serial/index.html
Thanks a lot, bookmarked.
DeleteThis can be useful when working with string - good function "readStringUntil", specially when reading also needed beside print. Probably the library is good for other projects too.
Thanks again.
** The captcha with numbers is too small to read or understand.
Thanks for the feedback Anonymous. The captcha is only used when commenting under "Anonymous". This is a blogger/blogspot feature that I have no personal control over. However, I will feed it back to Google.
ReplyDeleteCaptcha feedback has been submitted to Google.
DeleteThanks a lot Scott C, for informing google about captcha and your other helps. (I understand that you don't have anything to do to fix it, as it's up to google. But honestly, I am surprised to see that you care - that much! I asked the earlier questions as Anonymous, as now.)
ReplyDeleteKeep up the good work, good man :). May God bless you.
this doesnt run at least for me it is stopping at sensorReading = myPort.readStringUntil('\n');
ReplyDeleteNevermind i figured it out that got taken out in the 2.1 version i went to the 2.0.3 and it works just fine now
DeleteThis is the only code that i have seen that actually works great. Thank you very much!
ReplyDeleteI am glad you liked it
Delete