IR switch

Hello,

I want to build an IR switch. What I mean is to have an IR transmitter (IR LED) and on the other side to have an IR receiver. I want to sent lets say the pulse: ON-(1sec delay)-OFF-(1sec delay)-ON and the IR receiver to accept the pulse and do nothing. However, when an object "cuts" the IR connection and the IR receiver does not get the pulse to give me a signal (for example Light ON a green LED on pin 13). My concern is how do I implement this, since they have to work in parallel, because If I implement this in void loop(), there should be the one after the other, meaning:

void loop()
{
....
send_pulse()
receive_IR_pulse()
....
}

I hope I haven't confused you. If you could help me, I would be grateful,
Thank you...

Yes, but surely the send part and the receive part will be running on two different arduinos ? Or is one arduino talking to itself over IR ?

You turn on the TX LED for, example one second.

When the TX LED is on, you check to see if there is something blocking the beam.

6v6gt:
Yes, but surely the send part and the receive part will be running on two different arduinos ? Or is one arduino talking to itself over IR ?

One Arduino talking to itself!

larryd:
You turn on the TX LED for, example one second.

When the TX LED is on, you check to see if there is something blocking the beam.

Yes, but when I use the TX, I cannot at the same time run the RX code! I use Arduino MEGA 2560 R3!

Why can you not run TX and RX at the same time?

You turn on the TX LED and start a timer (1second)

When you are timing, you are allowed to read the RX.

If the RX does not see the TX, you know there is blockage.

When the timer finishes, you don’t read the RX.

Any sample/test code how to start with? I have never used timers before! It's totally new to me! I am googling it however to find something helpful and well written!

Thank you...

Show us your current ‘complete’ code, we can discuss changes necessary.

Use CTRL T to format your code.
Attach the sketch between code tags, use the </> icon in the posting menu.

[code]Paste your sketch here[/code]

See attachment:

sketch_jul14a.ino (1.93 KB)

Using millis() for timing beginner’s guide Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum

larryd:
Show us your current ‘complete’ code, we can discuss changes necessary.

Use CTRL T to format your code.
Attach the sketch between code tags, use the </> icon in the posting menu.

[code]Paste your sketch here[/code]

See attachment:

Thank you for the code! It runs ok! I embedded to my central code, and on the void setup() field I remote all the other functions/commands and the result is that it cannot run. My concern is that if it affected by other commands, such as Serial.println I don't know...

You need to write code in a way that there is no blocking.

Stay away from delay(), while() and do()while.

The example offered does not wait for anything, it is referred to as ‘non blocking code’.

See Robin2’s discussion “Several things at the same time”.

https://forum.arduino.cc/index.php?topic=223286.0

larryd:
You need to write code in a way that there is no blocking.

Stay away from delay(), while() and do()while.

The example offered does not wait for anything, it is referred to as ‘non blocking code’.

See Robin2’s discussion “Several things at the same time”.

Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Just curious. I have faced such incompatibility between commands in the same code again in the past. I went "down" searching to assembly and registers links for days without success. Is this something that has to do with the compiler?

Thanks a lot for your time and the code again!

“I have faced such incompatibility between commands”

This needs to be explained to us better.

Any poorly written code, on any controller, can lock up other code from running.
If you call a function that ‘waits’ for an external event rather than ‘checking’ for the event, you have blocking code.

Get into the habit of always writing non blocking code.

My concern is that if it affected by other commands, such as Serial.println I don't know...

Try it!
It won't break just because you write duff code! The joy of this hobby is being able to try stuff, if it doesn't work modify it until it does, when it works you can congratulate yourself. Don't be scared to experiment.

One Arduino talking to itself!

I know how it feels :confused:

I made this function:

void detect_object()
{
    if(objectFlag)
       Serial.println("Object detected!");      
}

which reads the public variable objectFlag. And on void loop() I have:

void loop()
{
     send_pulse();
     receive_IR_pulse();
     detect_object();
}

and prints continuously the message although I have not put any object between Receiver and Transmitter. Do you see something wrong?

prints continuously the message although I have not put any object between Receiver and Transmitter. Do you see something wrong?

No, I think it's doing exactly what I'd expect it to do with the code shown and with the circuit diagram you post...oh, I can't see a diagram.

Do you think something is wrong?

“and prints continuously the message although I have not put any object between Receiver and Transmitter. Do you see something wrong?”

Try:
void detect_object()
{
if(objectFlag == true)
{
Serial.println("Object detected!");
objectFlag = false;
}
}

I get this when there is not an object:

1
Object detected!
2
Object detected!
3
Object detected!
4
Object detected!
5
Object detected!
6
Object detected!
7
Object detected!
8
Object detected!
9
Object detected!
10
Object detected!
11
Object detected!
12
Object detected!
13
Object detected!
14
. . .

and nothing (or "counter wait" better) when I have an object.

Thank you...

Show us the ‘complete’ sketch that produces that output.

It is your code, with the adds that I have posted previously.