3

Ok, this is my first project where I've not had @forsvunnet close by to help.

I've got a camera connected to an optocoupler, that just fires HIGH then LOW.

I can't get a button working with it, so when I push a button, it fires opto 3 times.

This is my sketch.

 // Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int opto = 12;
int buttonInput = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.m
pinMode(opto, OUTPUT);
pinMode(buttonInput, INPUT);
digitalWrite(buttonInput, LOW);}

// the loop routine runs over and over again forever:
void loop()
{
  buttonState = digitalRead(buttonInput);
  if (buttonState == HIGH) {
      digitalWrite(opto, HIGH);
      delay(1000);               // wait for a second
      digitalWrite(opto, LOW);
      delay(1000);               // wait for a second
      digitalWrite(opto, HIGH);
      delay(1000);               // wait for a second
      digitalWrite(opto, LOW);
  } else {

  }

}

Anyone point me in the right direction? It's driving me mad.

2
  • What camera are you using? What hardware? Can you provide more information by editing your question? Thanks! May 30, 2014 at 23:13
  • Why do you use digitalWrite() on an input pin (buttonInput) in setup()? Should it be opto instead? Also, do you use a pullup (or pulldown) resistor to force a level on buttonInput when the button is not pushed?
    – jfpoilpret
    Jun 1, 2014 at 8:36

1 Answer 1

1

First of all, the loop (if it worked well) would only fire twice. It should only fire when you turn it on/off in a cycle (unless it's a rising/falling edge circuit (i.e. a pull down button/a falling edge interrupt), but I won't get into that). In your code you toggle it twice. The third one? I suspect that your camera only fires when it's given a low signal. This means that, when you set it to low in setup(), it takes a photo. To fix this, switch all the HIGHs and the LOWs for the opto pin.

This is the code which I think should work for you:

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int opto = 12;
int buttonInput = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.m
pinMode(opto, OUTPUT);
pinMode(buttonInput, INPUT);
digitalWrite(buttonInput, HIGH);}

// the loop routine runs over and over again forever:
void loop()
{
  buttonState = digitalRead(buttonInput);
  if (buttonState == HIGH) {
      digitalWrite(opto, LOW);
      delay(1000);               // wait for a second
      digitalWrite(opto, HIGH);
      delay(1000);               // wait for a second
      digitalWrite(opto, LOW);
      delay(1000);               // wait for a second
      digitalWrite(opto, HIGH);
      delay(1000);               // wait for a second
      digitalWrite(opto, LOW);
      delay(1000);               // wait for a second
      digitalWrite(opto, HIGH);
      delay(1000);               // wait for a second
      digitalWrite(opto, LOW);
      delay(1000);               // wait for a second
      digitalWrite(opto, HIGH);
  } else {

  }

}

Without much info about your camera, I'd imagine that the HIGH/LOW discrepancy is the issue. Another note: this code doesn't protect against bouncing. It's fine if as it is because the delays will cause the bounces to not be picked up. If you do a "blink without delay" type of sketch using millis() and a loop, you might need to fix this. Like I said, it'll work fine now, but it's kind a crude fix since it could cause problems later if mixed with other code.

3
  • Thanks for getting back to me. I think you're right with the LOW being the trigger here, it's a CANON 350D (Pretty old, it's all I had around that I could rag about) But it works like most canons, using a 2.5mm jack + 3 wires. GND, Focus and Trigger. Jun 3, 2014 at 10:25
  • Would there be benefits for using blink without delay? My original intentions where to have it play the loop once when you push a button. Which made me wonder if it would be worth setting a separate void() with my camera behaviour in to clean the code. but might just be arbitrary Jun 3, 2014 at 10:27
  • @Andrew Did you test the camera yet? From the wires you posted, it only makes sense to have it be pulled down to ground. If so, did it work right? As far as the blink without delay, I personally wouldn't implement it if you don't change any features. The only reason I could see implementing it is to have a "cancel" button. If you want the experience, go ahead, just remember to debounce the button. EDIT: I found a problem with your code that I didn't notice. I'm editing my answer right now. Jun 3, 2014 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.