First processing sketch

Hi guys

I will try to make a "monitoring system" to see how the temperature and humidity in my little green house changes.
While i am waiting for the parts, i thought i would start checking out how to write the Processing Sketch (which i have NEVER used) to read and display the values sent from the Arduino.

In the final project, i will have 4 sensors (two temperature and two humidity) which i would like to read from the Arduino and save into a file (which i will later use to make some kind of graphic).

Now i am just trying things out with a pot.

As i said before, i have no experience with processing. I have read some tutorials to try to learn a bit how things work, but it seems i am doing something wrong.

On the Arduino side i am using this little code:

int pot = A0;

// set the lastPotValue to 11 to ensure first potVal prints
unsigned int lastPotVal = 11;

void setup() {
  pinMode(pot,INPUT);
  Serial.begin(9600);
}

void loop() {
  int potRead = analogRead(pot);
  
  // mapped from 0 to 1015 to help get a clean mapped 5
  // if mapped from 0 to 1023 the mapped 5 would sometimes "bounce" to 4
  int potVal = map(potRead, 0, 1015, 0, 5);
  
  if(potVal != lastPotVal){  
    Serial.println(potVal);
    lastPotVal = potVal;
    delay(10);    
  }
}

would this work as a way to send data to Processing? Is there a better way?

Taking bits and pieces from online tutorials and examples and trying to get it to do what i want, i got to this little Processing sketch:

import processing.serial.*;

Serial myPort;        // The serial port

void setup () {
  // set the window size:
  size(400, 400);        

  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  // set inital background:
  background(0);
}

void serialEvent (Serial myPort) {
  // get the string:
  String inputRead = myPort.readStringUntil('\n');

  fill(255);
  text(inputRead,10,10,70,80);
}

... but it isn't working as intended.
I would like the value to be updated each time it changes. Now it is just "printing" of top of the old one...

I saw that there are some libraries available. Would you advise me to use one, or is this (and then final project) simple enough to write my own little sketch?

How would it be possible to write the values to a file instead of displaying them on screen?

Ai, ai, ai... when i was starting to feel that i finally know a little bit about my Arduino, i feel again like i actually know nothing!!! (which is very true!) 8)

I am very grateful for any help and will gladly read any links and tutorials you can give me talking about this.
Thanks!
=)

would this work as a way to send data to Processing?

Yes.

Is there a better way?

Get rid of the delay().

... but it isn't working as intended.

I'm surprised it works at all, since there is no draw() method.

I would like the value to be updated each time it changes. Now it is just "printing" of top of the old one...

Perhaps you should clear the screen between calls to text().

How would it be possible to write the values to a file instead of displaying them on screen?

That would be a Processing question, wouldn't it? There are dozens of examples with Processing. I'm sure some deal with files.

I am very grateful for any help and will gladly read any links and tutorials you can give me talking about this.

The Arduino site has a good example to start with.
http://arduino.cc/en/tutorial/Graph

I've been trying to make that example more practical, but keep it super simple.

Processing comes with File I/O examples. Choose File | Examples... and poke around.

Others on this forum have shared their helpful projects too, for example:
http://www.negtronics.com/simplot
http://forum.arduino.cc/index.php?topic=185740.0

While those might take some fun out of learning how to write your own tools, using existing tools will leave you a ton of time which you can spend adding features to your Arduino sketch.

thanks for your input guys.
i have been reading more about it and i ended up thinking that using the Firmata library would be a good option for this project.
I am still waiting for the parts to come, so i created a little sketch to try things out with two pots, a button and an led.
it is working like this:

  • the pot values are being printed on the window;
  • on button press it records both pot values into a file and lights the LED for 1second.

here is the code:

/*

 CIRCUIT:
 * Button (with pull-up res) arduino pin 4
 * LED arduino pin 9
 * 2 POTS arduino pins A0 and A4
 
 On button press Processing will read the pot values and store them
 on a file called "potValues.txt". It will also light the LED for
 1 second to signal the sucessfull record.
 
 */

// IMPORT LIBRARIES
import processing.serial.*;
import cc.arduino.*;

// DECLARE ARDUINO OBJECT
Arduino arduino;

// PRINTWRITER
PrintWriter output;    // initialize the PrintWriter object "output"

// VARIABLES
int pot1 = 0;
int pot2 = 4;
int pot1read;
int pot2read;

int button = 4;
int butState;
int lastButState = arduino.HIGH;

int led = 9;
int ledMode = 0;
long startLED;
int ledTime = 1000;


void setup() {
  // window size
  size(120, 80);

  // print the available ports
  println(Arduino.list());

  // Select and start arduino board
  arduino = new Arduino(this, Arduino.list()[0], 57600);

  // Set arduino pinModes
  arduino.pinMode(pot1, arduino.INPUT);
  arduino.pinMode(pot2, arduino.INPUT);
  arduino.pinMode(button, arduino.INPUT);
  arduino.pinMode(led, arduino.OUTPUT);

  // Create the output file
  output = createWriter("potValues.txt");
}


void draw() {
  background(100);

  pot1read = arduino.analogRead(pot1);
  pot2read = arduino.analogRead(pot2);
  butState = arduino.digitalRead(button);

  if (butState == arduino.LOW && butState != lastButState) {
    printData(pot1read, pot2read);
    startLED = millis();
  }
  lastButState = butState;

  text(pot1read, 40, 40);
  text(pot2read, 40, 60);

  lightLED();
}


void printData(int pot1data, int pot2data) {
  output.println(pot1data + ", \t" + pot2data);
  output.flush();
}


void lightLED() {
  if (millis() - startLED < ledTime) {
    arduino.digitalWrite(led, Arduino.HIGH);
  } 
  else {
    arduino.digitalWrite(led, Arduino.LOW);
  }
}

the code seems to be working ok.
But now my question is, is there a way NOT to create a new file on each time i run the sketch?
What is happening now is that on setup i am creating a new file

output = createWriter("potValues.txt");

so i am overwriting any data that was previously in the file.

Is it possible to do something like?

// check if file exists
// if not, create file
// if it exists, continue using the file

thanks! =)

i ended up thinking that using the Firmata library would be a good option

If you think that turning the arduino into a dumb I/O machine is a good idea then I think you have a lot to learn.

is there a way NOT to create a new file on each time i run the sketch?

Yes, don't create one.

 // Create the output file
  output = createWriter("potValues.txt");

The comment is the clue.

Is it possible to do something like?......

Yes, try opening the file for read, then read to the end and then add stuff.