Saturday, January 4, 2014

Arduino Time-Lapse Dolly Project Part 2

Arduino Time-Lapse Dolly Project Part 2

Buttons make it go!


For those who have not had much experience with the Arduino, basically what happens is once the board if plugged into a power source the program starts to run - so in this case the relay will start clicking away until you unplug it or upload another program.  So its kind of nice to update the program so that the relay, and therefore the camera will only fire once a button is pressed.

Now I add a button that will trigger the relay and therefore the camera.  The basic button wiring can be found on the Arduino site but I have one here as well:


So I ran a wire from the Arduino +5v pin to the bread board.  Hooked one side to the switch, on the other side of the switch is connected to a digital pin on the Arduino (Pin X), plus a 10K Ohm resistor connected in parallel to ground.

Here is my set up:

Button is the little back square on the white bread board.  The purple wires connect to the relay.  The red, yellow and black wires on the left side go to the camera.  

One thing to consider is that buttons aren't perfect and may "bounce" - meaning they don't make smooth contact and may chatter a bit.  I added a simple debounce code - very simple, maybe even laughably so - but I am a simple guy and I didn't understand the debounce code available online.

Here is my code:

const int buttonpin = 53;  //number of the pin the button is connected too
const int camerapin = 52; //number of the pin the relay to trigger the camera is connected too

int buttonstate = 0;

void setup() {

 pinMode(camerapin, OUTPUT);  //sets pin 52 to output mode
 pinMode(buttonpin, INPUT);  //sets pin 52 to input mode
}

void loop(){
  buttonstate = digitalRead(buttonpin);

  if (buttonstate == HIGH){
    digitalWrite(camerapin, HIGH);  // since I am not sure how much time the relay needs to be on for the camera to register it will keep the relay "on" for .1 seconds. 
    delay(100);
    digitalWrite(camerapin, LOW);
    delay(500); //to help prevent the button from triggering the relay multiple times by accident, this delay prevents the button from being pushed again for .5s
  }
  else{
    digitalWrite(camerapin, LOW);  //in hindsight, I probably don't need an "ELSE" statement
  }
}


Next: Stepper it up!

No comments: