Optocoupler code help needed

I'm using an Optocoupler module to detect an object falling through the slot. I need a simple code that reads the 5V input from the module to momentarily activate a relay. I've tried using the 5V to power the relay module directly, but without success. Arduino coding is definitely the hardest part for me, so thanks for your help.

How fast does this object pass? You may need to use an interrupt to bit miss the event.

How long to activate the relay?

This module indeed can’t power a relay. What you describe you may be able to do in hardware using a 555 timer.

Opto input on pin 2, relay output on pin 3. Assumes relay driver is active HIGH and opto is active LOW.

void setup()
  pinMode(2, INPUT_PULLUP);
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
     if (digitalInput(2) == LOW)
        // Activate relay
        digitalWrite(3, HIGH);

PM me if you need more than this.

When I compile it, it says that (digitalInput(2) "was not declared in this scope". Does it need to be defined at the top of the sketch?

That command is of course simply wrong. Use digitalRead() instead.

Ah, the joys of typing fast!

Thank you very much for your help. I was able to modify it without too much effort to get it to do what I needed using the following code:

void setup() {
pinMode(5, INPUT_PULLUP);
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);

} // put your setup code here, to run once:

void loop() {

if (digitalRead(5) == HIGH)
// Activate relay
digitalWrite(6, LOW);
delay (200);
digitalWrite(6, HIGH);
else {
digitalWrite(5, LOW);

This allowed the relay to switch every time the optocoupler slot is covered. I am building a "Plinko" game where a disk falls through the array of pins and through one of five slots. Four of the slots are "losers" and one is a "winner" I wired four of the "loser" sensors to one module and the "winner" sensor to a second module. The code I have doesn't include the second module yet, I'll just add in another if/else series.

Thanks again for your help.

If you digitalWrite(pin, LOW) to an input pin, the pull-up resistor is disabled. I don't think you want to do this.

I'm completely ignorant, can you let me know why I wouldn't want to do this? The circuit is working exactly as I had intended using this code.

You make the pinMode(5, INPUT_PULLUP) in the setup, you probably have a reason for using that instead of just INPUT.

Then in in the loop() you do a digitalWrite(5, LOW) which doesn't make much sense in the first place as it's an input, but it does disable this pull-up resistor.

It's simply a bug. Maybe you intend to do something with pin 6 there? If so, it nicely demonstrates also why you should use names for pins rather than numbers.

Thanks for your response. What change would you suggest that will give me the same output/result?

You can probably drop that whole else statement but it depends on the results you get and the circuit.

It'd be actually more useful (and important) for you to actually try to understand what's actually going on in the code you posted. Why do you manipulate those two pins in the way you do?