everytime various behaviour

Hi,

I’m new in uC.
I wrote simple program using my own library which should measure time. It should be stop-watch (chronometer?). I don’t know how it’s called in english :).

Almost all is OK, but…

When I want to measure second time, I need to restart arduino. But I have a loop which should (by pressing start button second time) start measure again. But it seems to freeze.

Here is the code:
“main.cpp”

#include <SWtimer.h>
#include <LiquidCrystal.h>

const int numRows = 4;
const int numCols = 20;

int c = 0;

int startPin = 13;

SWTimer timer;

LiquidCrystal lcd(6, 7, 8, 9, 10, 11);

void setup()
{                
  pinMode(startPin, INPUT);
  analogReference(DEFAULT);
  Serial.begin(115200);
  lcd.begin(numRows, numCols);
  lcd.setCursor(0,0);
  lcd.print("StopWatch v.0.0.1");
}

void loop()                     
{

// Wait for input signal for start count
  for(;;)
  {
    if(digitalRead(startPin) == HIGH)
    {
      attachInterrupt(0, Timer, RISING);
      break;
    }
  }

// Wait to end counting
  while(timer.ended == false)
  {

  }
  
}

void Timer()
{
//if all inputs was "enabled" stop measuring time
  if(timer.readInputs() == true)
  {
    detachInterrupt(0);
  }
  else
  {
//increment time
    timer.incTime();
//show time on display
    for(byte a = 0; a < timer.inputs(); a++)
    {
      lcd.setCursor(0,a+1);
      lcd.print(timer.textTime(a));
    }
  }
  
}

My library SWTimer.h is working 100% good. I tested it.

And I have another problem here. If I put this code to loop → “while(timer.ended == false){}” It is working(whole progam - it is counting again and again, allways when I press button on pin 13 (startPin)). But display don’t work - it look’s like “matrix”. There is random changing characters on all display.

lcd.setCursor(0,3);
lcd.print(millis());

Another situation is when I put that code there and when I delete this code from “for(byte a = 0; a < timer.inputs(); a++){}” loop.

lcd.setCursor(0,a+1);
lcd.print(timer.textTime(a));

If I delete this, display is OK, and I can measure 2nd, 3rd, … time without restarting arduino (but I don’t see time on display :-? )

I don’t understand why it’s one time working and another it isn’t.
Can somebody help me?

PS: sorry for my bad english

I think that it would be helpful to show the SWTimer.h and SWTimer.cpp files, too.

I can post it in evening. I'm now in work. But pssst! :-X ;)

Here is the SWTimer.h and SWTimer.cpp:

SWTimer.h

#ifndef SWTIMER_H
#define SWTIMER_H

#include "WProgram.h"

class SWTimer
{
  
public:
  
  SWTimer();
  ~SWTimer();
  
  void incTime();
  boolean readInputs(); // scan stop-inputs and write time to "endTime[stop-input number]" if any stop-input is enabled
  unsigned long* stopTime(byte input); //return endTime[] of the selected stop-input
  char* textTime(byte input); // return pointer to text version of current (end time) for selected stop-input with input identifier
  byte inputs(); //return number of stop-inputs
  void setInputs(byte input);
  void disqualify(); //disqualify this competior
  void unfinish(); //competior didn't finish
  void reset(); // reset time for next "competior"
  
  boolean ended;
  
private:
  
  boolean disqualified;
  boolean unfinished;
  
  byte stopInputs;
  
  char messageText[13];
  char inputIdentify[8];
  
  unsigned long time;
  unsigned long endTime[8];
  
};

#endif

SWTimer.cpp

#include "WProgram.h"
#include "SWtimer.h"

SWTimer::SWTimer()
{
  stopInputs = 1;
  for(byte a = 0; a < 8; a++)
  {
    inputIdentify[a] = a + 48;
  }
  reset();
}

SWTimer::~SWTimer()
{
}

void SWTimer::incTime()
{
  time++;
}

boolean SWTimer::readInputs()
{
  for(byte a = 0; a < stopInputs; a += 2)
  {
    int analog;
    analog = analogRead((a / 2) + 1);
    if(endTime[a] == 0 && analog > 100 && analog < 600)endTime[a] = time;
    if(endTime[a + 1] == 0 && analog > 400 && analog < 900)endTime[a+1] = time;
  }
  
  ended = true;
  
  for(byte a = 0; a < stopInputs; a++)
  {
    if(endTime[a] == 0)ended = false;
  }
  
  return ended;
}

unsigned long* SWTimer::stopTime(byte input)
{
  return &endTime[input];
}

char* SWTimer::textTime(byte input)
{
  if(endTime[input] == 0)
  {
    sprintf(messageText, "%c: %02d:%02d:%02d", inputIdentify[input], int(time/6000), int((time%6000)/100), int(time%100));
    return messageText;
  }
  else
  {
    sprintf(messageText, "%c: %02d:%02d:%02d", inputIdentify[input], int(endTime[input]/6000), int((endTime[input]%6000)/100), int(endTime[input]%100));
    return messageText;
  }
}

byte SWTimer::inputs()
{
  return stopInputs;
}

void SWTimer::setInputs(byte input)
{
  stopInputs = input;
}


void SWTimer::disqualify()
{
  disqualified = true;  
}

void SWTimer::unfinish()
{
  unfinished = true;  
}

void SWTimer::reset()
{
  ended = false;
  disqualified = false;
  unfinished = false;
  time = 0;
  for (byte a = 0; a < stopInputs; a++)
  {
    endTime[a] = 0;
  }
}