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