Thursday, January 30, 2014

Electronic uppy-downy thingy (AKA: EchoBox)

Back in August I got a height adjustable desk. It's motorized! So, one of my basic tenets is: What has motors, must be hacked! (which incidentally is NOT why my Roomba isn't working right now)

I (as usual) didn't do a great job of documenting the process. The project took shape because in order to make the desk go up and down, I would be forced to hold a button for up 17 seconds! Can you imagine the drudgery of holding in a button for 17 long, tiring seconds?

I decided to create a device with a microcontroller and a sonic module to bounce sound off of the floor and measure the distance between the desk and the floor. I started with an Ardiuno nano and a (knock-off) Ping((( module.

Then I went to Radio Shack and found the perfect case and a piece of perf board to build on.

I knew I'd need to open up the control and solder in a connector (I chose a stereo plug) to actually control the direction of travel. I got lucky. The desk's controller had three solder pads right behind the plug for the controls. Up - Ground - Down.



Then I got to experimenting with the Ping module, I came up with some code to make it work. It would simply send a trigger signal out to the trigger pin and then begin listening on the output pin.

Yes I know the wires aren't hooked up in this picture!

Once that was working I just needed a way to switch the up and down controls on and off in the desk controller. I added a couple 2222 transistors to a couple digital pins and wired them up. I don't remember exactly how I did it, but I will post a schematic when I get a chance.



Next came the push buttons to select which direction the program will send the desk.

Red for up Black for down


The board, when it was all wired up, was a mess, but it works!



All dressed up in it's pretty enclosure...




Mounted under the desk. The finished product.




Now, the code!

/*
 echoBox
   This is a sketch that controls the control box for my desk at work.
   It is a motorized desk that requires you to hold the button until 
   it gets to the correct height. 
   
   This will allow me to program a height and press a button to get the 
   desk to the correct location.
 */

// set pin numbers:
#include
const int outputDn = 2;
const int outputUp = 3;
const int trigger  = 6;
const int pingPin  = 7;
const int buttonDn = 8;
const int buttonUp = 9;

// variables will change:
int setUpState = 0;
int setDnState = 0;

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(outputUp, OUTPUT);     
  pinMode(outputDn, OUTPUT);     
  pinMode(buttonUp, INPUT);     
  pinMode(buttonDn, INPUT);     
  Serial.begin(9600);
}

void loop(){
  long duration, debug;
  float inches;
  debug = 1;
  
  setUpState = digitalRead(buttonUp);
  setDnState = digitalRead(buttonDn);
  if (setDnState == LOW) {
    duration = 10000;
    while (duration > 2900) {
      digitalWrite(outputDn, HIGH);  
      delay(100); 
      pinMode(pingPin, OUTPUT);
      digitalWrite(trigger, LOW);
      delayMicroseconds(2);
      digitalWrite(trigger, HIGH);
      delayMicroseconds(5);
      digitalWrite(trigger, LOW);

      pinMode(pingPin, INPUT);
      duration = pulseIn(pingPin, HIGH);
      inches = microsecondsToInches(duration);
      if (debug = 1) {
        Serial.print(inches);
        Serial.print("in - ");
        Serial.print(duration);
        Serial.print("ms");
        Serial.println();
      }
    }
    digitalWrite(outputDn, LOW);
  }
  if (setUpState == LOW) {
    duration = 100;
    while (duration < 4650) {
      digitalWrite(outputUp, HIGH);  
      delay(200); 
      pinMode(pingPin, OUTPUT);
      digitalWrite(trigger, LOW);
      delayMicroseconds(2);
      digitalWrite(trigger, HIGH);
      delayMicroseconds(5);
      digitalWrite(trigger, LOW);

      pinMode(pingPin, INPUT);
      duration = pulseIn(pingPin, HIGH);
      inches = microsecondsToInches(duration);
      if (debug = 1) {
        Serial.print(inches);
        Serial.print("in - ");
        Serial.print(duration);
        Serial.print("ms");
        Serial.println();
      }
    }
    digitalWrite(outputUp, LOW); 
  }  
}

void getHeight() {
  // Add code here to do the measurement to reduce the number of lines
}

long microsecondsToInches(float microseconds)
{
  return microseconds / 74 / 2;
}


No comments:

Post a Comment