Hi guys, this is my first post on this forum. I'm new to Arduino and Processing IDE (only the last few days)
I apologize for my abrupt writing style, but I just wrote a new topic on this forum that took me over 1/2 hr and for some reason the topic window re-set!!!!
I have some basic programming experience, but never with the UNO. What I am trying to do is pretty basic I would have thought, but I'm struggling.
I'm reading an analog input (0-5v) and converting to it's 10bit equiv. (0-1023) then saving this data to a file. This works fine so far but I want to read/save the data at 100 samples per second. The best I can get so far is around 60.
I've tested using a pot and it all works good except for the sample rate.
This will be used for a thrust meter testing sugar rocket motors. The analog input will be connected to a load cell which delivers 0-5vdc output.
I'm hoping that it's my programming rather than limitations with the device itself.
Maybe reading the input data into an array first, then writing to the file???
import org.firmata.*;
import cc.arduino.*;
import processing.serial.*;
Arduino arduino;
int comPort = 2; // Change the value to suit the usb port number
int analogPin = 0;
int val = 0;
PrintWriter output;
void setup()
{
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[comPort], 57600);
output=createWriter("Numbers.txt"); //set and open filename for data output
}
void draw()
{
val = arduino.analogRead(analogPin);
println(val); //can be commented out if this causes issues with sample rate
output.println(val); //write to file
}
void keyPressed() //stop and finalize written data
{
output.flush();
output.close();
exit();
}
Changing the serial speed will help.
arduino = new Arduino(this, Arduino.list()[comPort], 57600);
Your currently running at 57600 baud but PC's will usually do 115200 baud without problem. Change the processing and arduino sketch to the higher values.
I don't know what overhead (if any) will be involved in using Firmata on the Arduino that may slow the sample speed but writing your own arduino sketch would speed things up. Currently the PC requests a single sample from the arduino, the arduino does the sample and then sends the result back to the PC one at a time. Better would be the arduino waits for a start command from the PC and then starts sending samples until told to stop.
Thanks guys, after doing a lot of reading, I finally got this sorted - I think. I was over complicating, thinking I needed to use Firmata..
This is what I came up with:
Arduino:
int analogPin = 0;
int sensorValue = 0;
unsigned long timecheck = 0;
void setup()
{
// initialize serial communication:
Serial.begin(57600);
}
// the loop routine runs infinitely:
void loop()
{
// read the input on analog pin 0:
sensorValue = analogRead(analogPin);
// print out the value that is read:
if (millis() > timecheck)
{
timecheck +=10; // increment by 10 milliseconds (1/100th of a second)
Serial.println(sensorValue);
}
}
Processing:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String sensorValue; // Data received from the serial port
PrintWriter output;
void setup()
{
size (400,100);
output = createWriter("new_data_v3.txt"); // output file
// List all the available serial ports
println(Serial.list());
// Open the port
myPort = new Serial(this, Serial.list()[2], 57600); //change the 0 to a 1 or 2 etc. to match your port
myPort.bufferUntil('\n');
}
void draw()
{
// there's nothing here to see - move on.
}
void serialEvent(Serial myPort)
{
// read String from the serial port:
sensorValue = myPort.readString();
print(sensorValue); //print it out to the console
output.print(sensorValue); // Write data to file
}
void keyPressed()
{
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
It seems to work fine - I did a 5 minute recording (3 times) and ended up at 30,000 samples +/- 10 samples.
Now I just need to know how to create a more intuitive GUI - eg, buttons, dialog boxes etc.
I'll ask some questions in the appropriate sub forum.
Now I just need to know how to create a more intuitive GUI - eg, buttons, dialog boxes etc.
I like C# for that. You can get free versions of Visual Studio that make designing good looking forms apps very easy, and C# and serial data were made for each other (unlike C++ or VB).
PaulS:
I like C# for that. You can get free versions of Visual Studio that make designing good looking forms apps very easy, and C# and serial data were made for each other (unlike C++ or VB).
Thanks. I did look at the free version of Visual Studio and unfortunately it won't run under win xp. I'm considering upgrading my os, but that seems like a lot of mucking around and expense for no great benefit.
I found Visual Studio 2010 Express for free . I had to upgrade XP to SP3 to use it - goes to show how far behind I am with my OS. Nevertheless, it should be usable. It won't be my OS that let's this project down, it will be the learning curve ie. C# that will be the hard task to get my mind around, but I'm looking forward to the challenge and will post a question regarding implementation.
michinyon:
Now all you need, is a rocket that will run for 5 minutes.
What! - 5 minutes?... Who cares - I'm more concerned about the fact that millis() will apparently overflow after only 50 days!... I've got a couple of big inter-planetary projects planned. Rather disappointing - but I'm sure I'll come up with a solution.
If only NASA were up to speed on what I'm trying to achieve then it would... ... knock, knock... hang on... knock, knock, knock... Hey, this could be them now, gotta go guys, catch you later.
I'm more concerned about the fact that millis() will apparently overflow after only 50 days!... I've got a couple of big inter-planetary projects planned. Rather disappointing - but I'm sure I'll come up with a solution.
Your watch overflows every day, or twice a day, depending on the kind of watch you have. That hasn't brought civilization to a halt yet. The fact that millis() overflows every 7 weeks is NOT a problem.
Yes, that makes sense Paul. At this point I'm still using my old laptop booted up to dos 6.2 running a quickbasic program connected to an old A/D converter through the parallel port - it evaluates the data and draws a nice graph too.
It does the job, but I'd like more resolution and a nice GUI, hence the Arduino.
Unfortunately that wasn't NASA knocking at the door, but my disgruntled neighbours. After 27 days of constant deafening noise from my test rocket motor (not to mention the acrid stench) they've finally decided to lodge a formal complaint!
Bastards they are - how dare they try to inhibit a mans quest for knowledge.