Pants Switch

Imbedding  a switch into the button of a pair of pants.

The following code works in a circuit with two switches wired, but  I had some difficulty imbedding a switch within the zipper. The intent was to get a good estimate for when the user’s zipper or button is accidently left open.

 

/*

 * Pants Switch

 *

 * Reads two switches connected to the fly of a

   pair of pants, to approximate wether the user

   has accidently left his/her fly open. If fly is

   open (zipperDown) but the button is undone too,

   then the program assumes the user has conciously

   opened the fly. If only one is open, than the program

   assumes this is an accident. The program alerts the 

   user with a blinking LED.

 */

 

int buttonSwitchPin = 2;        // pants button switch connected to digital pin 2

int zipperSwitchPin = 3;        // pants zipper switch connected to digital pin 2

int ledPin = 13;                // LED connected to digital pin 13

 

boolean buttonUndone;           // state of the pants button

boolean zipperDown;             // state of the zipper

 

//long prevMillis = 0;            // stores the last millis value of the blinkLED function

long blinkInterval = 200;       // milliseconds per blink of the led

 

void setup(){                          // run once, when the sketch starts

  pinMode(buttonSwitchPin, INPUT);     // sets the digital pin as input

  pinMode(zipperSwitchPin, INPUT);     // sets the digital pin as input

  pinMode(ledPin, OUTPUT);             // sets the digital pin as output

 

}

 

void loop(){                           // run over and over again

  buttonUndone = (digitalRead(buttonSwitchPin) == HIGH);   // check if the pants button is open

  zipperDown = (digitalRead(zipperSwitchPin) == HIGH);     // check if the zipper is down

 

  if( buttonUndone != zipperDown){                         //check if the two states are dissimilar

    blinkLED();

  }

  else{

    digitalWrite(ledPin, LOW);   // sets the LED off

  }  

}

 

void blinkLED(){

  if(millis() % (2*blinkInterval) > blinkInterval){

    digitalWrite(ledPin, HIGH);

  }

  else{

    digitalWrite(ledPin, LOW);

  }

}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s