Hello all,
I'm developing some Arduino code using XBees to sense environmental data at a remote location. My Arduino code is working great and I'm now ready to upload my remotely acquired data to my computer so that I can parse the data and send it via an SMS message to my smart phone. This way I can monitor my remote location from anywhere.
I'm using a chunk of code I got from the "Getting Started with Processing" book by Reas and Fry. I am able to get the Arduino/XBee data into my computer this way, but I have a problem/issue:
The Arduino code loops continually, but each time I start the Processing Code, it restarts (resets?) the Arduino code. The Arduino code starts over from the beginning going through setup and then into the loop. I have no idea why this is happening. Here is the Processing code I'm using:
// Based on Example 11-07 from "Getting Started with Processing"
// by Reas & Fry. O'Reilly / Make 2010
// This sketch, as modified by kjh, reads data from an Arduino, stores it in an array
// then saves the data to a file on disk.
import processing.serial.*;
Serial port; // Create object from Serial class
int asize = 8; // define size of data array
byte[] val = new byte[asize]; // Data received from the serial port
// representing the setting of the pot
int i = 0; // array index
int x; // scratch value
float y;
void setup() {
//size(440, 220);
// IMPORTANT NOTE:
// The first serial port retrieved by Serial.list()
// should be your Arduino. If not, uncomment the next
// line by deleting the // before it. Run the sketch
// again to see a list of serial ports. Then, change
// the 0 in between [ and ] to the number of the port
// that your Arduino is connected to.
println(Serial.list());
String arduinoPort = Serial.list()[2];
port = new Serial(this, arduinoPort, 9600);
}
void draw() {
if (port.available() > 0) { // If data is available,
x = port.read(); // read it and store it in val
val[i] = byte(x);
y = float(x);
//println(y);
println(val[i]);
//val = map(val, 0, 255, 0, height); // Convert the value
i++;
}
if (i > asize-1){
saveBytes("arduino_data.dat", val);
println("Data written.");
exit();
}
}
My Arduino code does not read data from the computer or Processing, it only sends data to Processing. The Arduino code is quite long and involved using a fair amount of XBee interface code so I didn't include it here. But, as I said, it is running fine except for being restarted each time I run the Processing code. I'm using an Mega Arduino board connected to my PC via the USB cable.
I greatly appreciate any assistance on this.
Thanks,
Kevin H.
