Here is the current working code that prints only once after every time the loop is satisfied. Again, I want to extend this program to using another pair of switch/led with the same functionality in the same program. Thanks for your patience with me.
int ledPin = 13;
int buttonPin = 2;
int firsttimeflag = 1;
unsigned long startTime;
unsigned long pressTime;
unsigned long timeInterval = 10000;
int printflag = 1;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
Serial.begin(9600);
}
void loop()
{
if(digitalRead(buttonPin) == LOW){
if(firsttimeflag == 1){
startTime = millis();
firsttimeflag=0;
}
pressTime = millis()- startTime;
if(pressTime >= 1){
Serial.print("Time: ");
Serial.print(pressTime);
Serial.print(" milliseconds ");
Serial.print(int(pressTime/1000));
Serial.println(" seconds");
}
if(pressTime>timeInterval){
digitalWrite(ledPin, HIGH);
if(printflag == 1)
Serial.println("holding");
printflag = 0;
}
}
else if(firsttimeflag == 0)
{
firsttimeflag = 1;
printflag = 1;
Serial.println("Time: 0 milleseconds; 0 seconds");
digitalWrite(ledPin, LOW);
}
}