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);
}
-
1Better if you ask a particular problem rather than "too many error when we run"Prasan Dutt– Prasan Dutt2017年07月03日 11:11:48 +00:00Commented Jul 3, 2017 at 11:11
-
Thanks for the response. I've edited my code and run it again. Error shows: OvenVals cannot be resolved to a variable.Bong– Bong2017年07月03日 11:42:34 +00:00Commented Jul 3, 2017 at 11:42
-
1Grinds teeth What you have is not referred to as "codes." You have posted your program code. "Codes" are the things you enter into a numeric keypad lock to open a door. "Code" is the text you write that is compiled to be made into the binary file that becomes your firmware.JRE– JRE2017年07月03日 11:48:42 +00:00Commented Jul 3, 2017 at 11:48
-
1C is case-sensitive. You have defined a variable array "Ovenvals[]", but try to use "OvenVals[]" - those are two different arrays.Peter Bennett– Peter Bennett2017年07月03日 15:37:29 +00:00Commented Jul 3, 2017 at 15:37
-
Doesn't look like an Arduino sketch to me. What libraries are you using?user31481– user314812017年07月03日 16:25:14 +00:00Commented Jul 3, 2017 at 16:25
1 Answer 1
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!
-
Thanks Edgar for the enlightenment. Would you be able to help me how to save those output to .csv file or txt file on my computer?Bong– Bong2017年07月04日 03:20:25 +00:00Commented Jul 4, 2017 at 3:20
-
@Bong: No, but that could be a legitimate question on stackoverflow.Edgar Bonet– Edgar Bonet2017年07月04日 07:16:06 +00:00Commented Jul 4, 2017 at 7:16