Processing to Arduino - Page 1
Forum section: electronics / beginners

author: Brown
author_rank: Contributor
timestamp: June 16, 2014, 10:31:48 pm
content: Hi, I need help connecting my Processing sketch to my Arduino sketch. How do I sent the date, temp , weather, and woeid variable from Processing to Arduino. ThanksArduino Code://LCD Setup#include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 5, 4, 3, 2);void setup() {//Serial SetupSerial.begin(9600);Serial myPort;//Port setupmyPort = new Serial (this, "COM3", 9600);//LCD Setuplcd.begin(16, 2);}void loop() {int inByte = Serial.read();lcd.println(inByte);}Processing Code:// Serial Comm with the Arduinoimport processing.serial.*;Serial myPort;//variable used to store the temperatureint temperature = 0;//Using 'date' as variable to store dataString date = "";// store weather dataString weather = "";String woeid = "24124932";PFont font;void setup() {size(600, 360);font = createFont("Georgia", 28);textFont(font);String url = "http://weather.yahooapis.com/forecastrss?w=" + woeid;// Loads the XMLXML xml = loadXML(url);// Gets the XML elementXML forecast = xml.getChild("channel/item/yweather:forecast");XML title = xml.getChild("channel/item/yweather:condition");// Get the attributes we wanttemperature = forecast.getInt("high");weather = forecast.getString("text");date = title.getString("date");}void draw() {background(255);fill(0);// Display all the stuff we want to displaytext("Last Updated: " + date, width*0.15, height*0.33);text("Today’s high: " + temperature, width*0.15, height*0.5);text("Forecast: " + weather, width*0.15, height*0.67);text("WOEID: " + woeid, width*0.15, height*0.84);myPort.write(date);}

author: Brown
author_rank: Contributor
timestamp: June 16, 2014, 10:36:31 pm
content: Basically, all I want to do is send a variable from Processing to Arduino

author: Rick Law
author_rank: Super Contributor
timestamp: June 17, 2014, 02:13:54 am
content: Quote from: Brown on June 16, 2014, 10:31:48 pmWhat model Arduino and IDE are you using?"COM3" is a PC side thing, on the Arduino, it is Serial. Serial1 (for the MEGA)...Try using their example and see if it works just to test out the connection.Also, what are you using on the PC side to receive? (A VT100 emulation will work just fine if you want to see if things are coming in as expected.)

author: Brown
author_rank: Contributor
timestamp: June 24, 2014, 02:24:23 pm
content: Arduino IDE 1.5.6-r2So, I got it to work, but I can only send one variable. I want to send multiple variables and strings. How do I do that?I can only send one variable, in this case temperature , from Processing to ArduinoArduino code:]Code://LCD Setup#include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 5, 4, 3, 2);int temperature = 0;void setup() {//Serial SetupSerial.begin(9600);//LCD Setuplcd.begin(16, 2);}void loop() {//Reading the variable that's coming from Processingint inByte = Serial.read();//Setting the cursor so 'Today's High (C):' is on the top linelcd.setCursor(0, 0);//Prining 'Today's High (C):' on the LCDlcd.print("Today's High (C): ");//Setting the cursor so value is on the bottom linelcd.setCursor(0, 1);//Delaydelay(500);//Printing the valuelcd.print(inByte);}Processing 2.1.2Code://LCD Setup#include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 5, 4, 3, 2);int temperature = 0;void setup() {//Serial SetupSerial.begin(9600);//LCD Setuplcd.begin(16, 2);}void loop() {//Reading the variable that's coming from Processingint inByte = Serial.read();//Setting the cursor so 'Today's High (C):' is on the top linelcd.setCursor(0, 0);//Prining 'Today's High (C):' on the LCDlcd.print("Today's High (C): ");//Setting the cursor so value is on the bottom linelcd.setCursor(0, 1);//Delaydelay(500);//Printing the valuelcd.print(inByte);}

author: retrolefty
author_rank: Super Contributor
timestamp: June 24, 2014, 02:45:48 pm
content: Code:void loop() {//Reading the variable that's coming from Processingint inByte = Serial.read();//Setting the cursor so 'Today's High (C):' is on the top linelcd.setCursor(0, 0);//Prining 'Today's High (C):' on the LCDlcd.print("Today's High (C): ");//Setting the cursor so value is on the bottom linelcd.setCursor(0, 1);//Delaydelay(500);//Printing the valuelcd.print(inByte);}The fatal flaw in your reading of serial data is that you must first perform a test to see if a serial byte is indeed available to be read before actually reading it. The arduino serial library is fully interrupt driven and buffered for both send and receive data. Also serial comm is a single byte at a time process and if your variables are multi-byte in length you must perform your own parsing using whatever protocol you have defined. Here is an example from the arduino serial reference page showing the proper method to read a single byte from the PC:Code:void loop() {// send data only when you receive data:if (Serial.available() > 0) {// read the incoming byte:incomingByte = Serial.read();// say what you got:Serial.print("I received: ");Serial.println(incomingByte, DEC);}}http://arduino.cc/en/Reference/Serial