Sending an Arduino variable to Processing

My Arduino is hooked up to a temperature sensor, and it reads the temperature and "displays" it in a variable. How can processing read the variable's value?

Thanks -duemilanove

Processing can read from the serial port. The Arduino can write to the serial port.

So, make the Arduino sketch write the value to the serial port, probably in response to a request from the Processing application.

Thanks for the helpful reply, Paul!


I've been playing around with [u]Pachube[/u] for a little bit, and I've been trying this code for a little bit from [u]http://dailyduino.com/archives/616[/u], but could not get it to work, so I decided that reading a variable from an Arduino and then posting it on Pachube through processing would be much easier. I have been having troubles setting up the processing code to take the value of my temperature sensor (variable: temperature) and sending that value to be uploaded onto the website. Does anyone have any useful coding tips? Does anyone else have any experiences with getting Pachube and Arduino up and running?

Thanks a bunch! -duemilanove

Copy paste this Arduino code in your Arduino IDE:

/*

  • interface map with Processing to light up LEDs on arduino
  • Works with Processing sketch ‘pdxMap’
    */

int ledPin13 = 13; //Array of pins to controll lights
int ledPin12 = 12;
int ledPin11 = 11;
int ledPin10 = 10;
int ledPin9 = 9;
int ledPin8 = 8;
int ledPin7 = 7;
int ledPin6 = 6;

int analogPin = 15;
int value15 = 0;

int value = 1000; // variable to keep the actual value
void setup()
{
//Initialize pins as OUTPUTS and begin serial connection
//for(int i=1; i<9 ; i++){
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin10, OUTPUT);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(analogPin, INPUT);

//}
Serial.begin(115200);
}

void loop()
{
int input = Serial.read(); // read serial

switch (input){
case 1: //If processing passes a ‘1’ do case one
if(input == 1){
analogWrite(ledPin13, value);
delay(1000);
analogWrite(ledPin13,0);
delay(1000);
//For fading un-comment
/Serial.write(input);
for(value = 0 ; value <= 255; value++){ // ramp up the LED
analogWrite(Pin[1], value);
delay(30);
}
delay(5000);
for(value = 255; value >=0; value–){ //ramp down the LED
analogWrite(Pin[1], value);
delay(30);
}
/
}
break;
case 2:
if(input == 2){ //If Processing passes a ‘2’ do case two
analogWrite(ledPin12, value);
delay(1000);
analogWrite(ledPin12,0);
delay(1000);
}
break;
case 3:
if(input == 3){ //If Processing passes a ‘3’ do case three
analogWrite(ledPin11, value);
delay(1000);
analogWrite(ledPin11,0);
delay(1000);
}
break;
case 4:
if(input == 4){ //If Processing passes a ‘4’ do case four
analogWrite(ledPin10, value);
delay(1000);
analogWrite(ledPin10, 0);
delay(1000);
}
break;
case 5:
if(input == 5){ //If Processing passes a ‘5’ do case five
analogWrite(ledPin9, value);
delay(1000);
analogWrite(ledPin9,0);
delay(1000);
}
break;
case 6:
if(input == 6){ //If Processing passes a ‘6’ do case six
analogWrite(ledPin8, value);
delay(1000);
analogWrite(ledPin8,0);
delay(1000);

}
break;
case 7:
if(input == 7){ //If Processing passes a ‘7’ do case seven
analogWrite(ledPin7, value);
delay(1000);
analogWrite(ledPin7,0);
delay(1000);

}
break;
case 8:
if(input == 8){ //If Processing passes a ‘8’ do case eight
analogWrite(ledPin6, value);
delay(1000);
analogWrite(ledPin6,0);
delay(1000);

}
break;
}
}

++++++++++++++++++++++++++++++++++++++++++++

and then copy paste this processing code in the processing IDE:
make sure you delete the ++++signs I have top and bottom
It is just for you to be able to see the codes seperately without having
to read through the code…do not modify anything in the codes

+++++++++++++++++++++++++++++++++++++++++++++

/*
*Works with Arduino sketch ‘pdxMap’
*and arduino 8 LED sheild
*/

import processing.serial.*;

Serial myPort;
int a = 600;

void setup(){
//println(Serial.list());
myPort = new Serial(this, “COM4”, 115200);
myPort.buffer(1);
size(400, 400);
background(50);
noLoop(); // Makes draw() only run once
}

void draw() {
fill(a,0,0);
rect(0,0,width/4, height/2);
rect(100,0,width/4, height/2);
rect(200,0,width/4, height/2);
rect(300,0,width/4, height/2);
rect(0,200,width/4, height/2);
rect(100,200,width/4, height/2);
rect(200,200,width/4, height/2);
rect(300,200,width/4, height/2);
}

void mousePressed() {
println(“Coordinates: " + mouseX +”," + mouseY);
mouseAction();
//myPort.write(1);
//println(“square displayed”);
}

void mouseAction(){
if( mouseX > 0 && mouseX < 100){
if( mouseY > 0 && mouseY < 200){
println(“square 1”);
myPort.write(4);
}
}
if( mouseX > 100 && mouseX < 200){
if( mouseY > 0 && mouseY < 200){
println(“square 2”);
myPort.write(3);
}
}
if( mouseX > 200 && mouseX < 300){
if( mouseY > 0 && mouseY < 200){
println(“square 3”);
myPort.write(2);
}
}
if( mouseX > 300 && mouseX < 400){
if( mouseY > 0 && mouseY < 200){
println(“square 4”);
myPort.write(1);
}
}
if( mouseX > 0 && mouseX < 100){
if( mouseY > 200 && mouseY < 400){
println(“square 5”);
myPort.write(8);
}
}
if( mouseX > 100 && mouseX < 200){
if( mouseY > 200 && mouseY < 400){
println(“square 6”);
myPort.write(7);
}
}
if( mouseX > 200 && mouseX < 300){
if( mouseY > 200 && mouseY < 400){
println(“square 7”);
myPort.write(6);
}
}
if( mouseX > 300 && mouseX < 400){
if( mouseY > 200 && mouseY < 400){
println(“square 8”);
myPort.write(5);
}
}
}

++++++++++++++++++++++++++++++++++++++
Comments: All you need now is to set up a hardware of 8 LED’s on
a breadboard and hook one end of each LED to one pin of the Arduino and the other end to the GND.
Pin numbers to hook the LED’s to are specified in the code so do not choose random numbers otherwise you won’t get the readings unless you decided the change the numbers in the code accordignly.
If all said and done, you should be able to see a GUI of 8 buttons with a red background every time you click on one of them, one of the LED’s will turn on…the only you need to don on your end is the hardware like i said and it’s easy…also make sure your baud rate is 115200 not 9600 b/c i have it in the code set up that way

Hey, Sammor, it's time (after 16 posts) to learn how to post code properly On the top bar of the panel where you post messages and replies, there is a button with a # sign on it. Before pasting code, press that button. Or, after pasting the code, select the code, and then press that button. It puts tags in front of and after the code that cause the forum software to display code in a scrollable box.

Hi duemilanove, what kind of processing code you have to upload the data into Pachube? Any good pointers for that side of the project? BR, fatzo

Hi duemilanove, what kind of processing code you have to upload the data into Pachube? Any good pointers for that side of the project? BR, fatzo

Actually, I'm looking for pointers myself, so sorry I can't help. :-/

Ah okay, thought that you had figured out the Pachube part of the thingie. There are quite many tutorials in Pachube site, yet haven't spotted a simple enough for my needs... I don't have an ethernet shield, just serial connection to host pc that SHOULD send the data to Pachube site. That's piece of cake, right :-/