Trying to read from an arduino and check the values

edited April 2017 in Arduino

Hello, i did some weeks ago a classic thermometer with an arduino uno and i wanted it to store the data to my computer. So i used the classic arduino to processing code. Up there everything went alright. But when i stopped the code and it got saved on my computer i noticed that i had some null or 0.00 values. So it's been a week and i have been trying to make a fail-safe so the program read again when the value is 0.00 or null. But i either enters a loop or doesnt work. This is my code right now

import processing.serial.*; //<>//
Serial myPort;  // Create object from Serial class
String val = "Primero";     // Data received from the serial port
PrintWriter output;
int y = year();
int M = month();
int d = day();
int m = minute();  // Values from 0 - 59
int h = hour();    // Values from 0 - 23
float val1;

void setup()
{
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  output = createWriter("positions.txt"); 
  delay(1000);
}

void draw()
{
  Lectura();
}
void keyPressed() {
  if (key == ENTER)
  {
    output.flush(); // Writes the remaining data to the file
    output.close(); // Finishes the file
    exit(); // Stops the program
  }
}

void Lectura() {
  if ( myPort.available() > 0) 
  {  // If data is available,
    val = myPort.readStringUntil('\n');         // read it and store it in val
  } 
  Checkeo();
}
void Checkeo() {
  float val1 = float(val);
  if (val1 == 0.00) {
    Lectura();
  }
  if (val == null) {
    Lectura();
  } else {
    println(val); //print it out in the console
    output.println(val+","+ y +"/"+M+"/" + d +","+ h+":" + m);
    delay(600);
  }
}
``

Answers

  • Perhaps try:

    void Checkeo() {
      float val1 = float(val);
      if (val1 != 0.00 && val != null) {
        println(val);
        output.println(val+","+ y +"/"+M+"/" + d +","+ h+":" + m);
    }
    

    The idea (untested): Don't recursively call Lectura. Instead, let draw() call it at the rate of frameRate(). Don't use delay() -- let draw() space out its calls at the rate of frameRate(). Just ignore bad values in Checkeo, and keep calling Lectura() from draw() until keyPressed().

    P.S. Consider renaming Lectura to lectura and Checkeo to checkeo. The convention is capitalize classes, lower case methods/functions.

  • Answer ✓

    Using delay in any of your function called by draw is not a good idea as @jeremydouglass has pointed out.

    Please check this post: https://forum.processing.org/two/discussion/comment/92348/#Comment_92348

    In the above link, there are two pieces of code that you could use. The first one is using noLoop/redraw in tandem. This is an optimal approach if you just want to manage data as data arrives. However, if you are working on the sketch at the same time, then the second approach is better. The reason is that the noLoop/redraw approach only updates the sketch graphics when data arrives on the serial connection. In the second approach the sketch is updated independently to what arrives via serial.

    The examples in the post are very explicit and I am confident you will find them useful.

    Kf

  • That worked, but the delay() was used so i would only get one reading per hour

  • I see. the problem with delay in that case is that your application is unresponsive, isn't it?

    Kf

  • edited April 2017

    @marcost2 -- re:

    the delay() was used so i would only get one reading per hour

    Which do you want it to do?

    1. run at 12:00, 1:00, 2:00 etc.
    2. run every 60 minutes after whenever the sketch starts?

    You can do either without blocking draw by using millis() or hour(), saving the old value, and then checking when the new value is different enough that Lectura() should run again.

    You could also use a very low frameRate() or check frameCount, but these may drift over time.

Sign In or Register to comment.