Quick actions

cmd+k|ctrl+k

Navigation

Languages

Door Open Serial 

Snippet info

Language

Cpp

Visibility

public

Author

mondoha

Created

2020-04-01T21:11:14Z

Updated

2022-09-06T03:56:53.882062Z


// ***************************************************************************

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int DOCounter = 0;   // counter for the number of Door Opened

int doorState = 0;         // current state of the Door
int lastdoorState = 0;     // previous state of the Door

unsigned long Dtemps = 0; //  Duration of the operation
unsigned long SDtemps = 0; // Duration of the operation in second


unsigned long DtimeO = 0; // Exact time the door is opened
unsigned long SDtimeO = 0; // Exact time the door is opened in seconds

unsigned long DtimeC = 0; // Exact time the door is closed
unsigned long SDtimeC = 0; // Exact time the door is Closed in seconds

unsigned long DOtime = 0; // Door Previous opening time
unsigned long SDOtime = 0; // Door Previous opening time in seconds

unsigned long DOTotaltime = 0; // Cumulative time of all Door openings
unsigned long SDOTotaltime = 0; // Cumulative time of all Door openings in seconds

float DOpercent = 0; // Percentage of door opening time


// ********************************************************

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  //Program Start Phrase
  Serial.println( " Starting the program: Hello ... ");
  Serial.println(" ");
  
   /*
  Op : Opening
  D. O .T. : Door opening time
  D. C .T. : Door closing time
  D. O. : Door open time
  C. D. O. : Cumulative door open time
  D. S. O. : Duration of the operation
  % : Percentage of door opening time
  */
  
  Serial.println(" Op : Opening | D. O .T. : Door opening time | D. C .T. : Door closing time");
  Serial.println("       D. O. : Door open time | C. D. O. : Cumulative door open time       ");
  Serial.println(" D. S. O. : Duration of the operation | % : Percentage of door opening time ");
  Serial.println(" ");
  
  Serial.println("********************************************************************");
  Serial.println("  Op  | D. O .T. | D. C. T. |  D. O.  | C. D. O. | D. S. O. |   %   ");
  Serial.println("--------------------------------------------------------------------");
  
} //void setup


// ***************************************************************************

void loop() {
  // read the pushbutton input pin:
  doorState = digitalRead(buttonPin);
  
  
  Dtemps = millis(); // Exact time the operation began
  
  // compare the buttonState to its previous state
  if (doorState != lastdoorState) { 
    // if the state has changed, increment the counter
    if (doorState == LOW) {
  
      // turn LED ON:
  	  digitalWrite(ledPin, HIGH);
      // if the current state is HIGH then the button went from off to on:
  	  DOCounter++; // Adds 1 to the counter 
      
      DtimeO = millis(); // Exact time the door is opened
         
     
    } //if (doorState == LOW)
   
    else {
      
      // turn LED OFF:
      digitalWrite(ledPin, LOW);
      
      // if the current state is LOW then the button went from on to off:
  	 
      SDtemps = 0.001 * Dtemps; // Expression of time in seconds
  	  SDtimeO = 0.001 * DtimeO; // Expression of time in seconds
     
      DtimeC = millis(); // Exact time the door is closed
      SDtimeC = 0.001 * DtimeC;// Expression of time in seconds
      
      DOtime = DtimeC - DtimeO; // Calculation of door opening time
      SDOtime = 0.001 * DOtime; // Expression of time in seconds
      
      DOTotaltime = DOTotaltime + DOtime; // Calculation of the total duration of all door openings
      SDOTotaltime = 0.001 * DOTotaltime; // Expression of time in seconds
  
      float fDtimeC = (float)DtimeC; // Conversion to Float
      float fDOTotaltime = (float)DOTotaltime; // Conversion to Float
      
      DOpercent = 100 * (fDOTotaltime / fDtimeC); // percentage calculation
      
      // Display values
      
        // Op:
      	if (DOCounter < 10){Serial.print("    ");}
      	else if (DOCounter < 100){Serial.print("   ");}
      	Serial.print(DOCounter);
      
      	Serial.print(" |  ");
     
        // D. O. T.
      	if ( SDtimeO < 10){ Serial.print("    ");}
      	else if ( SDtimeO < 100){ Serial.print("   ");}
      	else if ( SDtimeO < 1000){ Serial.print("  ");}
      	else if ( SDtimeO < 10000){ Serial.print(" ");}
      	Serial.print(SDtimeO);
      
      	Serial.print("   |   ");
  
      	// D. C. T.
      	if ( SDtimeC < 10){ Serial.print("    ");}
      	else if ( SDtimeC < 100){ Serial.print("   ");}
      	else if ( SDtimeC < 1000){ Serial.print("  ");}
      	else if ( SDtimeC < 10000){ Serial.print(" ");}
      	Serial.print(SDtimeC);
      
      	Serial.print("  | ");
        
        // D. O.
      	if ( SDOtime < 10){ Serial.print("     ");}
      	else if ( SDOtime < 100){ Serial.print("    ");}
      	else if ( SDOtime < 1000){ Serial.print("   ");}
      	else if ( SDOtime < 10000){ Serial.print("  ");}
      	Serial.print(SDOtime);
      
      	Serial.print("  |  ");
      
  	  	// C. D. O.
      	if ( SDOTotaltime < 10){ Serial.print("     ");}
      	else if ( SDOTotaltime < 100){ Serial.print("    ");}
      	else if ( SDOTotaltime < 1000){ Serial.print("   ");}
      	else if ( SDOTotaltime < 10000){ Serial.print("  ");}
      	Serial.print(SDOTotaltime);
      
      	Serial.print("  |  ");
      
        // D. S. O.
      	if ( SDtemps < 10){ Serial.print("     ");}
      	else if ( SDtemps < 100){ Serial.print("    ");}
      	else if ( SDtemps < 1000){ Serial.print("   ");}
      	else if ( SDtemps < 10000){ Serial.print("  ");}
      	Serial.print(SDtemps); 
      
      	Serial.print("  | ");
       
        // %
      	if ( DOpercent < 10){ Serial.print("  ");}
      	else if ( DOpercent < 100){ Serial.print(" ");}
 	  	Serial.println(DOpercent);
      
      Serial.println("--------------------------------------------------------------------");
               
      } //else
  
    // Delay a little bit to avoid bouncing
    delay(10);
 
  }  //if (doorState != lastdoorState)
  
  // save the current state as the last state, for next time through the loop
  lastdoorState = doorState;

} //void loop
INFO