Program freezing when using Serial out

edited July 2017 in Arduino

Hi guys,

This program I've made is designed to read data from a csv file and output it to an arduino (which will in turn eventually output it to a motor). Strangely, it runs just fine without the Arduino plugged in, but with the Arduino plugged in it starts freezing every one or two seconds after the first eight seconds or so. I presume some buffer somewhere is being filled up and is not clearing itself fast enough, but I have no idea where. Any thoughts?

/*  CSV_READER
    Version 3.4

    Improvements from last version:
    - 

    Improvements to be made:
    - Begins to freeze when Arduino plugged in

*/

import processing.serial.*;

int xpos=90; // set x servo's value to mid point (0-180);
int ypos=90; // and the same here
Serial port; // The serial port we will be using
Table table;
final int motorPulley = 17; //number of teeth on the pulley
final int enginePulley = 51;
final int calibrationFactor = (2880 * (enginePulley/motorPulley))/360; //2880 = max rpm of motor
int rowCount = 1;
int timeResolution; //the time interval in ms of the chosen data file
boolean fileSelected = false;
boolean run = false;
boolean pause = false;
boolean stop = false;
int time, rpm;
boolean debounce = true; //ensures button is released before it can be pressed again
int stopCount = 0; //how long before program closes
int timeRunning = 0; //time the program has spent running
int timePaused = 0;
String fileName;

void setup() 
{
  size(360, 360, P2D);
  background(50);

  selectInput("Select a file to process:", "fileSelected");
  //table = loadTable(file, "header");

  frameRate(1000); //sets framerate

  //println(Serial.list()); // List COM-ports
  port = new Serial(this, Serial.list()[5], 9600);


}

void fileSelected(File selection) //selects the file
{
  if (selection == null) 
  {
    println("Window was closed or the user hit cancel.");
  } 
  else 
  {
    println("User selected " + selection.getAbsolutePath());
    fileName = selection.getName();
    table = loadTable(selection.getAbsolutePath(), "header");
    fileSelected = true;
    TableRow row1 = table.getRow(1);
    TableRow row2 = table.getRow(2);
    int time1 = row1.getInt("time");
    int time2 = row2.getInt("time");
    timeResolution = time2 - time1;
  }
}

void draw() 
{
  while (fileSelected == false) //Does not run unti a file has actually been selected
  {
    delay(1);
  }

  clear();
  //println(frameRate); //use for testing a specific variable

  if (stop == false) //normal running section of program
  {
      if (run == true) //loads row data
      {
        timeRunning = millis() - timePaused;
        nextRow();
      }

      else
      {
        timePaused = millis() - timeRunning;
      }

      //println(rpm);
      update(0, rpm/calibrationFactor); //update serial signal being sent to arduino

      strokeWeight(10);
      stroke(0, 191, 255);
      fill(0, 0, 0);
      //ellipse(180, 180, rpm/24,rpm/24);
      arc(180,180,180,180, 0, radians(rpm/24)); //draws an arc whic represents the rpm value
      fill(255);
      text(rpm, 163, 185);
      text("rpm",166,195);
      text((float) time/1000, 30, 230);
      text("time (s)", 30, 240); //displays the time and rpm values
      text(fileName,30,30);
      text("timeR: " + (float) timeRunning/1000, 30, 260);

      if (run == false) //displays start button
      {
       strokeWeight(0);
        fill(51,255,51);
       rect(160,320,50,20);
       fill(0);
       text("START",165,332);


      }

      if (run == true) //displays pause button
      {
        strokeWeight(0);
        fill(255,153,51);
       rect(160,320,50,20);
       fill(0);
       text("PAUSE",165,332);

      }

      strokeWeight(0);
        fill(255,0,0);
       rect(230,320,50,20);
       fill(0);
       text("STOP",235,332);  

      if (mousePressed && overRect(160,320,50,20) && debounce) //detects if start/pause button has been pressed
       {
         debounce = false;
         if (run == false)
           run = true;
         else if (run == true)
           run = false;
        }

      if (mousePressed && overRect(230,320,50,20) && debounce)
      {
        stop = true;
      }

      if (rowCount >= table.getRowCount()) //end program when end of file is reached
      {
        stop = true;
      }
  }

  else if (stop == true)
  {

    fill(255);
    text("Run finished",150,160);
    stopCount++;
    if (stopCount >= 500)
      exit();
  }
}

boolean overRect(int x, int y, int width, int height) //checks to see if the mouse is over the defined rectangle
{
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
    return true;
  } 
  else 
  {
    return false;
  }
}

void nextRow() //loads data from the next row in the table
{
  TableRow row = table.getRow(rowCount);
  time = row.getInt("time");
  if (time <= timeRunning) //loads new rpm if we have reached that time
  {
    rpm = row.getInt("rpm"); 
    rowCount++;
  }
  if(time + timeResolution < timeRunning) //catches up if it falls behind
  {
     rowCount++; 
     println(rowCount);
  }

}

void mouseReleased() //ensures mouse is released before it can be pressed again
{
   debounce = true; 
}

void update(int x) //updates output to arduino
{
  //Calculate servo postion from mouseX
  xpos= x/2;
  ypos = y/2;
  //Output the servo position ( from 0 to 180)
  port.write(xpos+"x");
  port.write(ypos+"y");
}

Answers

  • I will use a simple program that sends key strokes or data directly from draw to your arduino and see if it works or if you see the same issues.

    Are you reading your data as a byte stream in your ino program? I notice that when you write your data into the port, there is no delimiter between your data tokens. This might work fine...

    Another test is that you send back the data read by your arduino...

    Where is the second update() function? You are using one with two parameters but I only see one with a single parameter, line 201.

    Kf

  • The update function does take two parameters, int x and int y, I'd just removed one right that second as I was thinking about trying something else. Sorry for the confusion.

    As I understand it, the Arduino code uses the 'x' and 'y' as delimiters. I have a copy of the Arduino code but I'm not the one who originally wrote it.

    I'll try a simple test of sending data to and from the Arduino as you suggest and see what happens.

Sign In or Register to comment.