Stopwatch Program Questions.....

I have a ton of questions, since I'm totally new to the world of Arduino's. So please bare with me. I need to apply a Stopwatch program to my Arduino so it can coincide with it's other functions. The Stopwatch functions I saw some variations of the Stopwatch programs on the internet but they are of no help meaning that they do not run or I would have to heavily manipulate them for it to compile. I found a script that somewhat works so I modified it but in doing so it acts weird.

Problems
Pause Function Help
First, I want to create a pause function that saves the time that the stopwatch is running for each individual pin(button). I need it to know when a different pin is being pressed so it can pause the time on previous pin and start the stopwatch on the current pin. This should enabled me to go back where I left off, if I choose to.

Algorithm Help
Secondly, I need to make sure that my algorithm flows perfectly. Currently it is repeating the base step for each button where it is checking whether it is off (not pressed). So it automatically jumps to the conclusion that no pin was pressed was pressed. I want it to perform the if else statements in the way they are mentioned. So can someone help me with that, as well.

Functionality Help
Instead of have two separate buttons for stop/pause and start. I was told to try to make it all on 1 input. For example, when a users holds down/ or puts something on it to suppress the button, the stopwatch should run and when the pressure is taken off then it should stop. But I don't know how to make it as one input which is in effect messing with my algorithm. Is there any ways to fix this or alternatives? Originally I was going to have multiple LEDS. It sort of worked with two separate buttons as you can see in the orignal script at the end of the program. But when I pressed stop on the original script. I just gives the time that I stopped at and then it is still running in the background. So for example, if I start the program and and press start and stop/pause at lets say 30secs. And let's say I wait 2 minutes and the press start and stop again 30secs later, it gives me that the elapsed time instead of being 60secs, it gives me 3mins.

If you have any suggestions I'm open to hear, thanks. If there is anymore information that you need about this problem I will respond back in a timely manner.

Thanks again.

original_ver.ino (4.92 KB)

modified_vers.pde (7.83 KB)

Maybe:
http://69.5.26.215/forum/bbshowpost.php?bbsubject_id=10591

This didn't help me at all. Since it needs to be in arduino language. I can't even understand the implementation of it.

I'm very much new to the Arduino thing. But I need help with my Stopwatch program....The functionality of the button is not responding the way I want it to.

Instead of have two separate buttons for stop/pause and start. I was told to try to make it all on 1 input. For example, when a users holds down/ or puts something on it to suppress the button, the stopwatch should run and when the pressure is taken off then it should stop. But I don't know how to make it as one input which is in effect messing with my algorithm. Is there any ways to fix this or alternatives? But when I hold down on the start button, it just keeps on restarting the timer rather than letting the timer run. I assume it is being caught by the delay() which gives the same reading, under 20 ms. If I change the delay on the start portion it will set the elapsed time to be within that fixed range and that is not what I want, it should depend on the user and how long they are holding down on it. I use a capacitive function so it can calculate the amount of pressure being put on the button.

If you have any suggestions I'm open to hear, thanks. If there is anymore information that you need about this problem I will respond back in a timely manner.

Thanks again.

short_stopwatch.pde (3.47 KB)

check - Arduino Playground - StopWatchClass - ?

Okay I've made some modifications to it but still no luck. Here's what I've done so far...

int touchedCutoff = 60;
int firsttime1,firsttime2=1;
unsigned long startTime1,startTime2;
unsigned long pressTime1,pressTime2;
int capSensePin1 = 2;
int capSensePin2 = 3;
int ledPin = 13;
float h, m, s, ms;
unsigned long over;

void setup()
{
Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
pinMode(capSensePin1, INPUT);
digitalWrite(capSensePin1, HIGH);
 Serial.println("Press 1 for Start/reset, 2 for elapsed time");

}

void displayResult(unsigned long a, unsigned long b)
{
  h = int(a / 3600000);
 over = a % 3600000;
 m = int(over / 60000);
 over = over % 60000;
 s = int(over / 1000);
 ms = over % 1000;
 
 Serial.print("Formatted Press time: ");
 Serial.print(h, 0);
 Serial.print("h ");
 Serial.print(m, 0);
 Serial.print("m ");
 Serial.print(s, 0);
 Serial.print("s ");
 Serial.print(ms, 0);
 Serial.println("ms");
 Serial.println(); 
}

void loop(){
  // If the capacitive sensor reads above a certain threshold,
  //  turn on the LED
  if (readCapacitivePin(capSensePin1) > touchedCutoff) {
   // digitalWrite(LEDPin, HIGH);
 if(digitalRead(capSensePin1) == LOW){
  if(firsttime1 == 1){
    startTime1 = millis();

   firsttime1=0;
  }
      pressTime1 = millis()- startTime1;

  if(pressTime1 >= 1){
   Serial.print("Pin2 Runtime: ");
   Serial.print(int(pressTime1/1000));
   Serial.print(" secs ");
   Serial.print(pressTime1);
   Serial.println(" msecs");

 displayResult(pressTime1,startTime1); 

         }
  if(pressTime1>3000){
   //digitalWrite(ledPin, HIGH); 
 
     }
 }
 
 else if(firsttime1 == 0){
  firsttime1 = 1;
  delay(5);
  Serial.println("Time: 0 milleseconds; 0 seconds");
 // digitalWrite(ledPin, LOW);
 }
  }

  if (readCapacitivePin(capSensePin2) > touchedCutoff) {
   // digitalWrite(LEDPin, HIGH);
   delay(10);
 if(digitalRead(capSensePin2) == LOW){
  if(firsttime2 == 1){
    startTime2 = millis();
   firsttime2=0;
  }
     
   pressTime2 = millis()- startTime2;
  if(pressTime2 >= 1){
   Serial.print("Pin3 Runtime: ");
   Serial.print(int(pressTime2/1000));
   Serial.print(" secs ");
   Serial.print(pressTime2);
   Serial.println(" msecs");

 displayResult(pressTime2,startTime2); 

}
  if(pressTime2>3000){
   //digitalWrite(ledPin, HIGH); 
  }
 }
 
 else if(firsttime2 == 0){
  firsttime2 = 1;
  
  Serial.println("Time: 0 milleseconds; 0 seconds");
 // digitalWrite(ledPin, LOW);
 }
  }  
  
 }
 
// readCapacitivePin
//  Input: Arduino pin number
//  Output: A number, from 0 to 17 expressing
//          how much capacitance is on the pin
//  When you touch the pin, or whatever you have
//  attached to it, the number will get higher
//  In order for this to work now,
// The pin should have a 1+Megaohm resistor pulling
//  it up to +5v.
uint8_t readCapacitivePin(int pinToMeasure){
  // This is how you declare a variable which
  //  will hold the PORT, PIN, and DDR registers
  //  on an AVR
  volatile uint8_t* port;
  volatile uint8_t* ddr;
  volatile uint8_t* pin;
  // Here we translate the input pin number from
  //  Arduino pin number to the AVR PORT, PIN, DDR,
  //  and which bit of those registers we care about.
  byte bitmask;
  if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
    port = &PORTD;
    ddr = &DDRD;
    bitmask = 1 << pinToMeasure;
    pin = &PIND;
  }
  if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
    port = &PORTB;
    ddr = &DDRB;
    bitmask = 1 << (pinToMeasure - 8);
    pin = &PINB;
  }
  if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
    port = &PORTC;
    ddr = &DDRC;
    bitmask = 1 << (pinToMeasure - 13);
    pin = &PINC;
  }
  // Discharge the pin first by setting it low and output
  *port &= ~(bitmask);
  *ddr  |= bitmask;
  delay(1);
  // Make the pin an input WITHOUT the internal pull-up on
  *ddr &= ~(bitmask);
  // Now see how long the pin to get pulled up
  int cycles = 16000;
  for(int i = 0; i < cycles; i++){
    if (*pin & bitmask){
      cycles = i;
      break;
    }
  }
  // Discharge the pin again by setting it low and output
  //  It's important to leave the pins low if you want to 
  //  be able to touch more than 1 sensor at a time - if
  //  the sensor is left pulled high, when you touch
  //  two sensors, your body will transfer the charge between
  //  sensors.
  *port &= ~(bitmask);
  *ddr  |= bitmask;
  
  return cycles;
}

The moment I hold down even press down on the touchpad surface. it starts the timer. So if I let go of the button it still continues to run if I'm still pressing down, though it doesn't show the results on the screen. For example, lets say I start my program and hold down on the surface for 10 secs. and let go and wait 20secs before I press down again. It should show it continuing from the 10secs where I left off, but instead it is still running continuing from 30secs.

Here is a picture of it. I paused for like 10secs in between the gaps that are off. So you can have a better idea of what I'm talking about.

Next, thing is that if I press capSensePin1 first. Then I press capSensePin2, it is suppose to start it's own independent timer. But instead it is acting the same as as the first button pressed. Giving me the elapsed time from capSensePin1, still. Please help if I can get over this hurdle then I'm done.

-Thanks

just some math done a bit differently , with numbers that are a bit more recognizable

int  h, m, s, ms;  // no need to be float 

void displayResult( unsigned long a, unsigned long b )
{
  ms = a % 1000;
  a = a/1000;
  s = a % 60;
  a = a / 60;
  m = a % 60;
  h  = a / 60;

Thanks but this only improves the formatting of the displayResult() function. Which is minor to me at this point, the actually functionality of it is more important.