Pages: [1]   Go Down
Author Topic: Processing Code Help Needed  (Read 937 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am trying to read two temperature sensors using Processing. The sensors work, but only output 0.0 when I run the code. I have tried everything I can think of and I would greatly appreciate any help. Thanks!

Code:
import cc.arduino.*;
import processing.serial.*;

Arduino arduino;

PrintWriter output;
DateFormat fnameFormat= new SimpleDateFormat("yyMMdd_HHmm");
DateFormat  timeFormat = new SimpleDateFormat("hh:mm:ss");
String fileName;
int serialCount = 3;
int sensorPin2 = 2;
int sensorPin3 = 3;
//int sensorPin4 = 4;
float temp1;
float temp2;
float rpm;
char DELIM = ',';
long timeold = 0;
int interval = 500;
int length;
int BAUDRATE = 9600;
String serialString;


void setup() {
  Date now = new Date();
  fileName = fnameFormat.format(now);
  output = createWriter(fileName + ".csv"); // save the file in the sketch folder
  arduino = new Arduino(this, Arduino.list()[0], 9600);
  }

void draw() {
  long currentTime = millis();
  if(currentTime - timeold >= interval){
    temp1 = arduino.analogRead(sensorPin2);
    temp1 = (temp1 * 5.0) / 1024.0;
    temp2 = arduino.analogRead(sensorPin3);
    temp2 = (temp2 * 5.0) / 1024.0;
    //rpm   = arduino.analogRead(sensorPin4);

    print(timeold);
    print(", ");
    print(temp1);
    print(", ");
    println(temp2);
    //print(", ");
    //println(rpm);
    output.print(timeold);
    output.print(", ");
    output.print(temp1);
    output.print(", ");
    output.println(temp2);
    //output.print(", ");
    //output.println(rpm);
   
    timeold = millis();
  }
}

void keyPressed() {
  output.flush(); // Writes the remaining data to the file
  output.close(); // Finishes the file
  exit(); // Stops the program
  }
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34164
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try setting the timeold variable to current time plus about five seconds to allow the arduino to go through its reset before you try and talk to it.

What code is your arduino rumming?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It currently has the standardfirmata example running on it. The change in timeold had no effect. Any other ideas?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am trying to read two temperature sensors using Processing.
What kind of temperature sensors? Are they really analog devices?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

They are DS18B20 sensors. I looked them up and realized they are digital. However, I have tried using them as digital as well and had no luck with the same results.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://playground.arduino.cc/Learning/OneWire
There's more to reading the DS18B20 sensor than just analogRead() or digitalRead(). Firmata doesn't understand that.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have created code for the arduino for them to work. However, I am trying to control the arduino board solely through processing. Is it possible to use this sensor directly with processing?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it possible to use this sensor directly with processing?
Yes. But, NOT using Firmata. Processing can send a message to the Arduino - "Hey, what's the temperature inside?" or "Hey, what's the temperature outside?" (Or, something similar but easier to parse).

The Arduino can see the message ("<I>" or "<O>"), and decide whether to read the inside sensor or the outside sensor (assuming one is inside and one is outside). If not, use 1 and 2, instead of I and O. The Arduino then replies with the temperature as a string.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks! I'll try it out.
Logged

Pages: [1]   Go Up
Jump to: