Saving data from a sensor/arduino
in
Programming Questions
•
10 months ago
The goal of this script is to have a user trace a pre-generated sine wave with a potentiometer. I would then like to save a file with a three-column matrix (or a .csv or .txt file) that includes the following information for each data point:
- Time (milliseconds)
- 'senseval' (the value of the sensor coming from the potentiometer through serial communication)
- Actual sine wave value (the value of the yellow ball)
Question: How do I continuously store the three values I mentioned above and then save a file with all the data when the program is stopped.
I am aware of PrintWriter, output.print, println, output.flush, and output.close but I'm having trouble figuring out where to place all these commands to get this to work. Any advice appreciated! Thanks.
- /**
- *
- * Tracing a sine wave with a potentiometer
- *
- **/
- // For sensor:
- import processing.serial.*;
- Serial myPort; // The serial port
- float senseval;
- // End For sensor
- // For rendering sine wave:
- int xspacing = 1; // How far apart should each horizontal location be spaced
- int w; // Width of entire wave
- float theta = 0.0; // Start angle at 0
- float amplitude; // Height of wave
- float period = 600; // How many pixels before the wave repeats
- float dx; // Value for incrementing X, a function of period and xspacing
- float[] yvalues; // Using an array to store height values for the wave
- // End For rendering sine wave
- void setup() {
- // For rendering sine wave:
- size(600, 600);
- amplitude = height/3; //amplitude of wave is always 1/3 of window height
- w = width;
- dx = (TWO_PI / period) * xspacing;
- yvalues = new float[w/xspacing];
- // End For rendering sine wave
- // For sensor:
- println(Serial.list());
- myPort = new Serial(this, Serial.list()[0], 9600);
- myPort.bufferUntil('\n');
- size(600, 600);
- smooth();
- stroke(0);
- // End For sensor
- }
- void draw() {
- background(0);
- calcWave();
- renderWave();
- renderBall();
- renderSensor();
- }
- void calcWave() {
- // Increment theta (try different values for 'angular velocity' here
- theta += 0.02;
- // For every x value, calculate a y value with sine function
- float x = theta;
- for (int i = 0; i < yvalues.length; i++) {
- yvalues[i] = sin(x)*amplitude;
- x+=dx;
- }
- }
- void renderWave() {
- noStroke();
- fill(100);
- // Draw the wave by putting an ellipse at each location
- for (int x = 0; x < yvalues.length; x++) {
- ellipse(x*xspacing, height/2+yvalues[x], 16, 16);
- }
- }
- void renderBall() { // Renders a yellow ball in the center of the screen
- noStroke();
- fill(255,255,0,255); // Yellow color, full opacity
- ellipse(width/2,height/2+yvalues[yvalues.length/2],16,16);
- }
- void renderSensor() { // Renders the sensor circle depending on 'senseval' from potentiometer
- noFill();
- strokeWeight(6);
- stroke (255,69,0);
- ellipse(width/2,senseval,25,25);
- }
- // For sensor:
- void serialEvent (Serial myPort) {
- // get the ASCII string:
- String inString = myPort.readStringUntil('\n');
- if (inString != null) {
- // trim off any whitespace:
- inString = trim(inString);
- // convert to an int and map to the screen height:
- float inByte = float(inString);
- inByte = map(inByte, 0, 1023, height/6, height-height/6);
- senseval = inByte; //Sets the current value
- }
- }
- // End For sensor
1