user35165: I am using an Arduino Uno for my project. I use 4 digital inputs and they will be triggered by 4 different relays. I would like to save the data in a *.csv file in the format below.
For example, if any of the inputs goes high, it will save a station ID, date, time it goes ON (starts). But once the input goes low, it will save the time it goes OFF (stops) on the same line.
Kindly help to advise if my code is correct because I get too many errors when I run it.
Example table in .csv file:
Date        Oven Station    Time Start     Time Stop
3/7/2017       Oven_P1_1       11:58       16:00
3/7/2017       Oven_P1_2        7:30       11:00
3/7/2017       Oven_P5_1       14:10       20:00
4/7/2017       Oven_P5_2        01:20      7:30
4/7/2017       Oven_P5_1        2:40       8:45
4/7/2017       Oven_P1_2        08:05      12:00
4/7/2017       Oven_P1_1        09:12      13:10


import processing.serial.*;

int Oven_P1_1 = 8;
int Oven_P1_2 = 9;
int Oven_P5_1 = 10;
int Oven_P5_2 = 11;
float Ovenvals [] = {0,0,0,0};

Serial myPort;
String val;
Table table;

String filename;

void setup() {
  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);
  table = new Table();
  table.addColumn("id");
  table.addColumn("year");
  table.addColumn("month");
  table.addColumn("day");
  table.addColumn("hour");
  table.addColumn("minute");
  table.addColumn("second");
  table.addColumn("Oven_P1_1");
  table.addColumn("Oven_P1_2");
  table.addColumn("Oven_P5_1");
  table.addColumn("Oven_P5_2");
}

void serialEvent(Serial myPort) {
  val = myPort.readStringUntil('\n');
  if (val != null) {
    val = trim(val);
    println(val);
    float Ovenvals[] =float(split(val, ','));
    TableRow newRow = table.addRow();
    newRow.setInt("id", table.lastRowIndex());
    newRow.setInt("year", year());
    newRow.setInt("month", month());
    newRow.setInt("day", day());
    newRow.setInt("hour", hour());
    newRow.setInt("minute", minute());
    newRow.setInt("second", second());
    newRow.setInt("Oven_P1_1", ovenVals[0]);
    newRow.setInt("Oven_P1_2", ovenVals[1]);
    newRow.setInt("Oven_P5_1", ovenVals[2]);
    newRow.setInt("Oven_P5_2", ovenVals[3]);
    {
      saveTable(table, "data/OvenStatus.csv");  
    }
  }
}

void draw() {
  OvenVal[0] = digitalRead(Oven_P1_1);
  OvenVal[1] = digitalRead(Oven_P1_2);
  OvenVal[3] = digitalRead(Oven_P5_1);
  OvenVal[4] = digitalRead(Oven_P5_2);
  Serial.print(OvenVal[0]);
  Serial.print(",");
  Serial.print(OvenVal[1]);
  Serial.print(",");
  Serial.print(OvenVal[2]);
  Serial.print(",");
  Serial.print(OvenVal[3]);
  Serial.print(",");
  delay(100);
}


user7508: You asked for (emphasis mine):

[...] But once the input goes low, it will save the time OFF (stop)
  on the same line.

That's hard. What if several other inputs have gone HIGH in the
meanwhile? You will have to somehow rewind part of the file, then add
the stop time, the add again the subsequent lines...
You probably do not want to do all this on an Arduino. Especially
considering that the Arduino has no concept of file, unless you add an
SD card and the appropriate library. And it has no concept of time of
day, unless you add an RTC and the appropriate library. I would suggest
you go the easy path:

have the Arduino tell your computer through the Serial port whenever
one of those inputs changes state
write a program in your computer (with processing or whatever) that
reads this information and formats it the way you want.

For the first part, I suggest the following:
/*
 * Tell us when some pins change state.
 */

const int CHANNELS = 4;
const uint8_t pins[CHANNELS] = {8, 9, 10, 11};

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    static int states[CHANNELS];
    for (int i = 0; i < CHANNELS; i++) {
        int state = digitalRead(pins[i]);
        if (state != states[i]) {  // input changed state
            Serial.print("channel ");
            Serial.print(i);
            Serial.print(" going ");
            Serial.println(state ? "HIGH" : "LOW");
            states[i] = state;  // record new state
        }
    }
}

The output will be something like
channel 2 going HIGH
channel 1 going HIGH
channel 2 going LOW
...

For the second part, well, that's not an Arduino-related question
anymore!

35165: <p>I am using an Arduino Uno for my project. I use 4 digital inputs and they will be triggered by 4 different relays. I would like to save the data in a *.csv file in the format below.</p>

<p>For example, if any of the inputs goes high, it will save a station ID, date, time it goes ON (starts). But once the input goes low, it will save the time it goes OFF (stops) on the same line.</p>

<p>Kindly help to advise if my code is correct because I get too many errors when I run it.</p>

<p>Example table in .csv file:</p>

<pre><code>Date        Oven Station    Time Start     Time Stop
3/7/2017       Oven_P1_1       11:58       16:00
3/7/2017       Oven_P1_2        7:30       11:00
3/7/2017       Oven_P5_1       14:10       20:00
4/7/2017       Oven_P5_2        01:20      7:30
4/7/2017       Oven_P5_1        2:40       8:45
4/7/2017       Oven_P1_2        08:05      12:00
4/7/2017       Oven_P1_1        09:12      13:10
</code></pre>

<p><a href="https://i.stack.imgur.com/Ehoz8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ehoz8.png" alt="enter image description here"></a></p>

<pre><code>import processing.serial.*;

int Oven_P1_1 = 8;
int Oven_P1_2 = 9;
int Oven_P5_1 = 10;
int Oven_P5_2 = 11;
float Ovenvals [] = {0,0,0,0};

Serial myPort;
String val;
Table table;

String filename;

void setup() {
  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);
  table = new Table();
  table.addColumn("id");
  table.addColumn("year");
  table.addColumn("month");
  table.addColumn("day");
  table.addColumn("hour");
  table.addColumn("minute");
  table.addColumn("second");
  table.addColumn("Oven_P1_1");
  table.addColumn("Oven_P1_2");
  table.addColumn("Oven_P5_1");
  table.addColumn("Oven_P5_2");
}

void serialEvent(Serial myPort) {
  val = myPort.readStringUntil('\n');
  if (val != null) {
    val = trim(val);
    println(val);
    float Ovenvals[] =float(split(val, ','));
    TableRow newRow = table.addRow();
    newRow.setInt("id", table.lastRowIndex());
    newRow.setInt("year", year());
    newRow.setInt("month", month());
    newRow.setInt("day", day());
    newRow.setInt("hour", hour());
    newRow.setInt("minute", minute());
    newRow.setInt("second", second());
    newRow.setInt("Oven_P1_1", ovenVals[0]);
    newRow.setInt("Oven_P1_2", ovenVals[1]);
    newRow.setInt("Oven_P5_1", ovenVals[2]);
    newRow.setInt("Oven_P5_2", ovenVals[3]);
    {
      saveTable(table, "data/OvenStatus.csv");  
    }
  }
}

void draw() {
  OvenVal[0] = digitalRead(Oven_P1_1);
  OvenVal[1] = digitalRead(Oven_P1_2);
  OvenVal[3] = digitalRead(Oven_P5_1);
  OvenVal[4] = digitalRead(Oven_P5_2);
  Serial.print(OvenVal[0]);
  Serial.print(",");
  Serial.print(OvenVal[1]);
  Serial.print(",");
  Serial.print(OvenVal[2]);
  Serial.print(",");
  Serial.print(OvenVal[3]);
  Serial.print(",");
  delay(100);
}
</code></pre>
 7508: <p>You asked for (emphasis mine):</p>

<blockquote>
  <p>[...] But once the input goes low, it will save the time OFF (stop)
  <strong>on the same line</strong>.</p>
</blockquote>

<p>That's hard. What if several other inputs have gone HIGH in the
meanwhile? You will have to somehow rewind part of the file, then add
the stop time, the add again the subsequent lines...</p>

<p>You probably do not want to do all this on an Arduino. Especially
considering that the Arduino has no concept of file, unless you add an
SD card and the appropriate library. And it has no concept of time of
day, unless you add an RTC and the appropriate library. I would suggest
you go the easy path:</p>

<ul>
<li>have the Arduino tell your computer through the Serial port whenever
one of those inputs changes state</li>
<li>write a program in your computer (with processing or whatever) that
reads this information and formats it the way you want.</li>
</ul>

<p>For the first part, I suggest the following:</p>

<pre class="lang-c++ prettyprint-override"><code>/*
 * Tell us when some pins change state.
 */

const int CHANNELS = 4;
const uint8_t pins[CHANNELS] = {8, 9, 10, 11};

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    static int states[CHANNELS];
    for (int i = 0; i &lt; CHANNELS; i++) {
        int state = digitalRead(pins[i]);
        if (state != states[i]) {  // input changed state
            Serial.print("channel ");
            Serial.print(i);
            Serial.print(" going ");
            Serial.println(state ? "HIGH" : "LOW");
            states[i] = state;  // record new state
        }
    }
}
</code></pre>

<p>The output will be something like</p>

<pre><code>channel 2 going HIGH
channel 1 going HIGH
channel 2 going LOW
...
</code></pre>

<p>For the second part, well, that's not an Arduino-related question
anymore!</p>
