stopwatch problems

edited November 2013 in Arduino

Hi. I needed a stopwatch which could be stoped by arduino. Because I'm beginner in Processing I borrowed this code http://www.openprocessing.org/sketch/11635 and modified it to be able to connect to arduino and to time faster. My version of stopwatch is timing minutes:seconds:miliseconds. My problem is that 50% of the times when I stop the clock it stops on xx:xx:x0 and the other 50% on xx:xx:x5. Is there any way to fix this, because I would like to get more accurate timing?

Here is the code:

// T/t - start(restart after pause) timing
// S/s - stop (pause) timing
// C/c - reset (clear) to zero
 
// each time the start timing is selected, store the millis() value as startTime
// so the total ellapsed time is accumTime + (millis() - startTime)
// when the clock is started set StartTime = millis()
// when the clock is stopped set accumTime = millis() - startTime
// the millis() function returns the milliseconds elapsed since the applet has been running
 
import ddf.minim.*;
import processing.serial.*;

AudioPlayer player;
Minim minim;

//String portname = "COM9";
Serial port; // Create object from Serial class

int val = 0; 
int accumTime;   // total time accumulated in previous intervals
int startTime;   // time when this interval started
boolean running = false;
int displayTime;   // value to display on the clock face
int dMins, dSecs, dMiniSecs, dDecimal; 

PrintWriter output; 
PFont smallfont;
 
void draw() {
  background(36, 19, 25);
  int thisTime = millis();
  
   
  //println("val = " + val);
  
  if (port.available() > 0) {  // If data is available,
      val = port.read();         // read it and store it in val
      //println("val = " + val);
  }
  
  if ((val > 0)) {
      // stop timing, but do not clear
      if (running == true) {
         println ("Stopped at " + thisTime);
         running = false;
         accumTime = accumTime + millis() - startTime;
         val = 0;
         output.println(dMins + ":" + dSecs +":" + dMiniSecs);
         output.flush();
         //output.close();
         //println("val = " + val);
      }
  }
   
  if (keyPressed) {
      if ((key == 'c') || (key == 'C')) {
        // clear the time and stop the clock
        accumTime = 0;
        displayTime = 0;
        player = minim.loadFile("start_miro_tekma_2011.mp3", 2048);
      }
      if ((key == 't') || (key == 'T')) {
        // start timing (but only if running == false)
        if (running == false && accumTime == 0 && displayTime == 0) {
            player.play();
            delay(6500);
            startTime = millis();
            running = true;
            //println("val = " + val);
        }
      }
      
      if ((key == 's') || (key == 'S')) {
          // stop timing, but do not clear
          if (running == true) {
            println ("Stopped at " + thisTime);
            running = false;
            accumTime = accumTime + millis() - startTime;
            val = 0;
            //println("val = " + val);
          }
      }
    }
    if (running == true) {
          displayTime = accumTime + millis() - startTime;
    }
   
   dMiniSecs = (displayTime / 10) % 100;
   dSecs = (displayTime / 10 / 100) % 60;
   dMins = (displayTime / 10 / 100/ 60);
     
    text(nf(dMins,2) + ":" + nf(dSecs, 2) + ":" + nf(dMiniSecs, 2), width/2, height/2 + 40);
}
 
void setup() {
  size(550,220);
  int day = day();
  int month = month();
  int year = year();
  int hour = hour();
  int minute = minute();
  //println (day + "." + month + "." + year + " " + hour + ":" + minute);
  String desktopPath = System.getProperty("user.home") + "/Desktop";
  //System.out.print(desktopPath.replace("\\", "/"));
  output = createWriter(desktopPath.replace("\\", "/") + "/SSV" + "/" + day + "." + month + "." + year + " " + hour + "h " + minute + "m.txt");
  port = new Serial(this, "COM9", 115200);
  minim = new Minim(this);
  player = minim.loadFile("start_miro_tekma_2011.mp3", 2048);
  
  smooth();
  frameRate(20);
  smallfont = loadFont("LCD-128.vlw");
  textFont(smallfont);  
  textSize(128);
  textAlign(CENTER);
  fill(202, 35, 55);
   
}

void stop()
{
  // always close Minim audio classes when you are done with them

  // always stop Minim before exiting
  minim.stop();
  
  super.stop();
}

I hope someone can help me. Sorry for my bad english.

Sign In or Register to comment.