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! =)