Need help on the code for my project using arduino uno.

edited July 2017 in Arduino

I am using arduino uno for my project. I use 4 digital inputs to arduino and it will be triggered by 4 different relay. I would like to save the data in *.csv file in below format. Example, if any of the inputs will go high, it will save a station id, date, time it will ON (start). But once the input goes low, it will save the time OFF (stop) on the same line. Attached photo of my target output.

Below my initial code where I stuck. I am new with this programming and would like to seek help from the expert. Appreciate for any help and advise to complete my project. Thank you very much in advance.

import processing.serial.*;

int Oven_P1_1 = 8;
int Oven_P1_2 = 9;
int Oven_P5_1 = 10;
int Oven_P5_2 = 11;
int ovenVals[] = {0,0,0,0};
Serial myPort;
Table table;

int numReadings = 100;
int readingCounter = 0

String filename, val;

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)
try{
  val = myPort.readStringUntil('\n');
  if (val!= null) {
  val = trim(val);
  println(val);
//float sensorVals[] = float(split(val, ','));  // i dont need floats because I just need high or low input in 4 channel., what other function should I use.
   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]);

    readingCounter++;

     if (reading Counter %numReadings ==0)
   {
      saveTable(table, "data/OvenStatus.csv");  
    }
   }
  }

void draw(){

}

Expected Output

Answers

  • Edit post, highlight code, press Ctrl-o to format the code

  • For your post I will recommend to amend your data sample that you provided. This forum uses Markdown which in a simple way, it doesn't respect the standard new line character. There are two ways to preserve line integrity. At the end of the line add two spaces before hitting the RETURN key or select your sample data and press ctrl+q to quote it. This way one can see the layout of your data sample properly.

    Below my initial code where I stuck.

    Can you describe your stuck situation please. What does it mean that you are stuck? What is working, and what is not behaving the way it is expected. In other words, what have you accomplished so far with your code?

    I notice your code above has void loop(){ ... In Processing, one has a setup() and a draw() function. Your loop() call is used when writing Arduino code in the Arduino IDE. Check more information about draw() here.

    You can always explore previous arduino posts: https://forum.processing.org/two/search?Search=arduino

    Kf

  • Hi Kfrajer,

    Thank you for your response. I've edited my post and I've attached a photo of the data sample that I target to output. I fixed some of the errors related to case sensitive.

    **About the void loop() code, ** - I removed it now, I initially use the arduino IDE but because I can't find the function to save the data to csv file, I'm changing my code to Processing.

    **About "Below my initial code where I stuck" **

    1.) Initially I have in my code "under line 7 "float ovenVals[] = {0,0,0,0}" and I got an error at line 49, "the method setInt(String, int) in the type TableRow is not applicable for the arguments(String, float)". - I removed the float function since the 4 channels of input have no decimal were just HIGH (1) and LOW (0) input, so I changed it to int.

    2.) After I ran the code, a small blank window appear and with error at line 18. error says "ArrayIndexOutOfBoundsExemption:5". - I checked that my port is correct because I verified my connected Arduino UNO at device manager and it show COM5. 9600 Bits persecond.

    3.) Could help to check my code for the table? I just need the data saved on my computer when the input is high or low and the time when the change status.

    thanks. appreciate your help.

  • 2.) After I ran the code, a small blank window appear and with error at line 18. error says "ArrayIndexOutOfBoundsExemption:5". - I checked that my port is correct because I verified my connected Arduino UNO at device manager and it show COM5. 9600 Bits persecond.

    Sorry for the late reply. One thing you mentioned in your message. Your ardunio connection happens in COM5, right? Then you should do:

    myPort = new Serial(this, "COM5", 9600);

    Or

    You should print the content of the list and choose the proper item in the array:

    println(Serial.list());


    Now, you need to fix your code. For some reason there are some things in your code that shouldn't be there. For example, line 48 has an extra double quotation marl: >> " << Also line 56: if (reading Counter %numReadings ==0) ... you have an extra white space inside the conditional. I wonder if the forum markdown text is affecting your code or this is coming from your original code. After you fix this, I can try implementing an example that doesn't require any arduino code. Or you can check the reference:

    https://processing.org/reference/Table.html
    https://processing.org/examples/loadsavetable.html

    Kf

Sign In or Register to comment.