Author Topic: Processing to Arduino  (Read 3009 times)

0 Members and 1 Guest are viewing this topic.

Offline BrownTopic starter

  • Contributor
  • Posts: 45
Processing to Arduino
« on: June 16, 2014, 10:31:48 pm »
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. Thanks

Arduino Code:

//LCD Setup
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
 
    //Serial Setup
Serial.begin(9600);
 
  Serial myPort;
 
    //Port setup
  myPort = new Serial (this, "COM3", 9600);
 
 
  //LCD Setup
  lcd.begin(16, 2);
 

}

void loop() {


  int inByte = Serial.read();
 
  lcd.println(inByte);

}



Processing Code:

// Serial Comm with the Arduino
import processing.serial.*;
Serial myPort;

//variable used to store the temperature
int temperature = 0;

//Using 'date' as variable to store data
String date = "";

// store weather data
String 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 XML
  XML xml = loadXML(url);

  // Gets the XML element
  XML forecast = xml.getChild("channel/item/yweather:forecast");
  XML title = xml.getChild("channel/item/yweather:condition");

  // Get the attributes we want
  temperature = forecast.getInt("high");
  weather = forecast.getString("text");
  date = title.getString("date");
 
 
}

void draw() {
  background(255);
  fill(0);

  // Display all the stuff we want to display
  text("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);

}
 

Offline BrownTopic starter

  • Contributor
  • Posts: 45
Re: Processing to Arduino
« Reply #1 on: June 16, 2014, 10:36:31 pm »
Basically, all I want to do is send a variable from Processing to Arduino
 

Offline Rick Law

  • Super Contributor
  • ***
  • Posts: 3470
  • Country: us
Re: Processing to Arduino
« Reply #2 on: June 17, 2014, 02:13:54 am »
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. Thanks

Arduino Code:

//LCD Setup
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
 
    //Serial Setup
Serial.begin(9600);
 
  Serial myPort;
 
    //Port setup
  myPort = new Serial (this, "COM3", 9600);
 
 
  //LCD Setup
  lcd.begin(16, 2);
 

}
etc. etc...

What 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.)
 

Offline BrownTopic starter

  • Contributor
  • Posts: 45
Re: Processing to Arduino
« Reply #3 on: June 24, 2014, 02:24:23 pm »
Arduino IDE 1.5.6-r2

So, 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 Arduino

Arduino code:
]
Code: [Select]
//LCD Setup
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int temperature = 0;

void setup() {
 
    //Serial Setup
Serial.begin(9600);
 
  //LCD Setup
  lcd.begin(16, 2);
}

void loop() {
  //Reading the variable that's coming from Processing
  int inByte = Serial.read();
 
  //Setting the cursor so 'Today's High (C):' is on the top line
  lcd.setCursor(0, 0);
  //Prining 'Today's High (C):' on the LCD
  lcd.print("Today's High (C): ");
  //Setting the cursor so value is on the bottom line
  lcd.setCursor(0, 1);
  //Delay
  delay(500);
  //Printing the value
  lcd.print(inByte);

}




Processing 2.1.2


Code: [Select]
//LCD Setup
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int temperature = 0;

void setup() {
 
    //Serial Setup
Serial.begin(9600);
 
  //LCD Setup
  lcd.begin(16, 2);
}

void loop() {
  //Reading the variable that's coming from Processing
  int inByte = Serial.read();
 
  //Setting the cursor so 'Today's High (C):' is on the top line
  lcd.setCursor(0, 0);
  //Prining 'Today's High (C):' on the LCD
  lcd.print("Today's High (C): ");
  //Setting the cursor so value is on the bottom line
  lcd.setCursor(0, 1);
  //Delay
  delay(500);
  //Printing the value
  lcd.print(inByte);

}
« Last Edit: June 24, 2014, 02:28:08 pm by Brown »
 

Offline retrolefty

  • Super Contributor
  • ***
  • Posts: 1648
  • Country: us
  • measurement changes behavior
Re: Processing to Arduino
« Reply #4 on: June 24, 2014, 02:45:48 pm »
Code: [Select]
void loop() {
  //Reading the variable that's coming from Processing
  int inByte = Serial.read();
 
  //Setting the cursor so 'Today's High (C):' is on the top line
  lcd.setCursor(0, 0);
  //Prining 'Today's High (C):' on the LCD
  lcd.print("Today's High (C): ");
  //Setting the cursor so value is on the bottom line
  lcd.setCursor(0, 1);
  //Delay
  delay(500);
  //Printing the value
  lcd.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: [Select]
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

 
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf