Touch-Sensing Dual Bed-Side Light Switch

I was out shopping for a bedroom set with my partner a little while ago and one of the sets that was shown to us by an anxious sales-person had a bed frame with an interesting feature.  It had a thin metal bar running along the back of the headboard that acted as a touch-sensor and would switch on and off a bedside light plugged into an outlet attached to the side of the bed.  I didn’t have a lot of time to check it out, but it made me wonder, maybe I could put something together like that for our new bed.

My plan was to expand on the bedroom set’s functionality however.  I’m not the only one that sleeps in the bed and there is a light on either side.  Wouldn’t it be nice if there were two sensor bars, only above each of our sides of the bed that controlled our individual light?  Better yet, how about having the sensor differentiate between a tap and a hold to control the opposite light as well.  So, a light and a sensor bar on each side of the bed.  A tap to a sensor bar would toggle on/off its respective light, while a hold would toggle the other side’s light.  The other side’s bar would do the same, but a tap would toggle its own light and a hold the opposite.  Get that?

I’ve had some long past experience switching line level power sources for use in lighting for stage and club settings so I had something of an idea on how to I would proceed (I would need to buy some triacs in the near future), but I had absolutely no idea how touch-sensors worked.  A little research revealed that a lot of the switches use capacitance to sense touch.  I spent probably hours looking into how I might rig something up to an arduino, and even purchased a relatively expensive capacitance sensor.  I never could get anything working though…

Then one evening I was visiting the Sparkfun’s IRC channel #sparkfun on irc.freenode.net and pitched the idea about making an arduino into a touch sensor.  Adam Greig there offered some excellent advice and provided some example code he had done in the past to control his computer’s media player and it really set me off in the right direction!  His ideas worked fantastically, with no extra hardware needed!

Code for the project can be found here:

//Dual Lamp Control By Capacitence Sensors
// Thanks to Adam Greig for the basic infos
// CC BY-SA-NC 3.0

int i;  //    for loop counter - also used to time the disipation of the charge on the sensor pin (if it's short, then the bar is being touched)
int leftState;  / status of the left outlet (either on or off)
int rightState; // status of the right outlet
int leftcount;  // counts the time the left touch bar was touched
int rightcount; // time the right touch bar was touched

void setup() {
 Serial.begin( 9600 );
 pinMode(11, OUTPUT);      // sets the digital pin as output
 pinMode(12, OUTPUT);
 leftcount = 0;
 rightcount = 0;
}

void loop() {

 //Pin5
 DDRD    = B11111110;
 PORTD    = B00000000;
 DDRD    = B00000010;
 PORTD    = B11111100;
 for( i = 0; i < 10; i++ ) {
   if( PIND & B100000 ) break;
 }
 if( i > 0 ) {
   Serial.println( "Left Sensor Touched" );
   leftcount++;
   delay( 50 );
 }
 else {
   if( leftcount > 3){
     Serial.println( "Left Sensor HOLD Detected" );
     rightToggle();
   }
   else if( leftcount == 1 or leftcount == 2 or leftcount == 3){
     Serial.println( "Left Sensor TAP Detected");
     leftToggle();
   }
 }

 //Pin7
 DDRD    = B11111110;
 PORTD    = B00000000;
 DDRD    = B00000010;
 PORTD    = B11111100;
 for( i = 0; i < 10; i++ ) {
   if( PIND & B10000000 ) break;
 }
 if( i > 0 ) {
   Serial.println( "Right Sensor Touched" );
   rightcount++;
   delay( 50 );
 }
 else {
   if( rightcount > 3){
     Serial.println( "Right Sensor HOLD Detected");
     leftToggle();
   }
   else if( rightcount == 1 or rightcount == 2 or rightcount == 3){
     Serial.println( "Right Sensor TAP Detected");
     rightToggle();
   }
 }

 delay( 100 );
}

void leftToggle()  // if left is on, turn it off, and vice-versa
{
  Serial.println("Toggle Left Light");
  if (leftState == LOW){
    leftState = HIGH;
    Serial.println("Left Light Is Off, Turn On");
  }
  else {
    leftState = LOW;
    Serial.println("Left Light Is not Off, Turn Off");
  }
  digitalWrite(11, leftState);  //update the output pin
  rightcount = 0;
  leftcount = 0;
}

void rightToggle() // if left is on, turn it off, and vice-versa
{
 Serial.println("Toggle Right Light");
 if (rightState == LOW){
   rightState = HIGH;
   Serial.println("Right Light Is Off, Turn On");
 }
 else{
   rightState = LOW;
   Serial.println("Right Light Is not Off, Turn Off");
 }
 digitalWrite(12, rightState);
 leftcount = 0;
 rightcount = 0;
}

Now the WAF on this project needed to be high as this was going to be attached to our lovely new bed that we had spent a fortune on.  So, I wasn’t going to be able to just breadboard something up and duct tape it on like I might have done in University.  No, I was going to have to make an actual printed circuit board.  Unfortunately, I’ve never done one before, but fortunately, I’ve been really interested in trying it out.

I visited a local electronics store, bought some copper-clad board, and some etchant, and then put it aside for a couple months.  Yup, I was too scared to use it.   I’ve never used pcb design software let alone ever etched anything.  I was overwhelmed.

Part two will be up shortly, in which I get over my fear by committing to going through with it all, even if I screw it up!

Advertisements
This entry was posted in Projects, Touch-Sensing Dual Light Switch, Unfinished. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

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