# Robot Programming

I am going to create a robot that will turn on my light when I enter the room and will turn it off when I exit my room. The robot will turn the light on/off using a servo mounted near the light switch. The way I am thinking for the robot to know whether I enter/exit my room will be with using 2 PIR Motion sensors. Meaning one outside my door and one in my room near the door. For the program for these pir sensors I'm thinking I will have to use pulseIn() in order to see which PIR sensor went HIGH first, because the output pin goes high when it detects something. Is this correct?

Any help is great.

I don't understand how pulseIn() gives you the information that you need. I would think that a digitalRead(...) for each pin/PIR is sufficient. millis() or micros() may help as well.

How far apart are the PIRs? Are they at ankle height? Knee height? Hip height? What if more than one person enters the room?

Wouldn't a PIR that senses presence in the room be sufficient?

But remember, those sensors don’t latch. So you have to catch that HIGH which is only on for as long as you are in the beam. (you aren’t the Flash are you?)
You could add a latching circuit that gets canceled by the other beam being broken or you could add a latching routine in the code.

So have your loop continuously monitor for a digitalRead(HIGH) and when it does trigger, set a flag. That way the pin can go low again but the flag is still set. (myFlag=TRUE)
Then when the other beam gets broken or a certain amount of time has passed you can set the flag to false and start the process all over again.

If he really is going to continuously monitor the state of a beam then it would 'better' to use an interrupt - this would ensure it didn't get missed when he's also computing the millionth prime number in all those spare CPU cycles.

It could also simplify the programming a better (by abstracting the trigger away from the main loop).

On the other hand, just buying a PIR light switch would do the job too. I wonder what size this robot is? To reach the light switch. And how stable, I mean there could be Health & Safety considerations here. I think a single PIR would be more than sufficient to do the job.

Aside: there is a new breed of PIRs coming along that permanently detect body heat (unlike existing ones that detect movement of heat) - they will be able to sense the presence of a body and would be ideal for this project if only they were at consumer-level prices today. Oh well.

Just sayin'. :smiling_imp:

Ralph_S_Bacon wrote

If he really is going to continuously monitor the state of a beam then it would 'better' to use an interrupt

Novices often have difficulty with implementing and debugging ISRs. On the other hand, if the output pulse from the PIR is short enough, this may be the only practical way to go.

If he's using the senors we use here, an interrupt is probably overkill. I can wave my hand through the sensor's "field" as fast as I can and I get a 300-400 ms steady pulse out of it. Easily caught by the digitalRead, and I think easily caught by any reasonable length of code that doesn't have delay(s) in it.

And as for his question, there is still the time between him being on the outside of the door vs crossing the sensor on the inside of the door (he wasn't specific on this one's position). But even at a brisk walking speed of 4mph that 6 inches (between outer sensor on one side of a door jam and the one on the inside of the door jam) will take about 100 ms to traverse plus the 400 ms of the sensor going high, so I think that is probably enough time to catch which one came first, again with the digitalReads, especially if the first trigger sets up the code to listen for the second (in either direction).

I agree though that interrupts are a more elegant way to go, and it frees up the code to do other things without the distraction of listening, but coding interrupts for the first time can be hair wrenching.

ardshark: I agree though that interrupts are a more elegant way to go, and it frees up the code to do other things without the distraction of listening, but coding interrupts for the first time can be hair wrenching.

Although I've covered ISR programming (briefly) in one of my videos, it sounds like a foundation video might be useful to to noobs. Perhaps I'll combine it with using pointers.

Just kidding about that last item :smiling_imp: (I'll do it as a separate video).

Yes, the pulse from every PIR I've ever used is long enough to be detected easily in the main loop (prime number calculation notwithstanding) so that's a simpler way to go for a beginner.

Regarding the use of TWO sensors, my ones seem to detect me at 50 yards in the fog in a snowstorm (as soon as I can see my PIR it sees me too) but hey, if he wants to use two then it will provide a bigger challenge, so be it. I wish these contributors would return once they have their project up and running and post the code etc so others could see how they got on. Perhaps we need a separate section for "IT WORKED" rather than just queries?

Yes, I will most likely make a new topic describing how I got it to work. I haven't built it yet, though when I finish I will do this and describe my setup of how I did it, along with my code.

8) don't forget to include things that went wrong as well as what worked. It's all part of life's rich Arduino tapestry.

This thread reminds me of this Instructable....

I wonder what size this robot is? To reach the light switch. And how stable, I mean there could be Health & Safety considerations here.

What do you mean there could be Health & Safety considerations here? The way the robot will turn on the light switch will be by a micro servo mounted right near my light switch in a position so that the servo horn will be able to move the light switch to the ON/OFF positions.

SMM2: What do you mean there could be Health & Safety considerations here? The way the robot will turn on the light switch will be by a micro servo mounted right near my light switch in a position so that the servo horn will be able to move the light switch to the ON/OFF positions.

That was a very tongue-in-cheek concern, imagining that you might have a towering robot standing next to your light switch. It was not meant to be taken seriously. In the UK, we like to mock the 'Elf 'n' Safety people as there is nothing you can do that they can't rule as being, in some way, unsafe.

I don't know where in the world you're based; if in the UK you will now see what I mean, if elsewhere then apologies, I should me more culture-aware and not assume everyone will understand the dry British wit that sometimes passes for humour.

But I'm glad that you don't have a Robbie The Robot doing the switching. Dang! That's another non-culturally aware reference to a cult TV program Lost in Space. Danger! Will Smith! Danger!

Ralph_S_Bacon: That was a very tongue-in-cheek concern, imagining that you might have a towering robot standing next to your light switch. It was not meant to be taken seriously. In the UK, we like to mock the 'Elf 'n' Safety people as there is nothing you can do that they can't rule as being, in some way, unsafe.

I don't know where in the world you're based; if in the UK you will now see what I mean, if elsewhere then apologies, I should me more culture-aware and not assume everyone will understand the dry British wit that sometimes passes for humour.

But I'm glad that you don't have a Robbie The Robot doing the switching. Dang! That's another non-culturally aware reference to a cult TV program Lost in Space. Danger! Will Smith! Danger!

Ohh, ok then just wanted to make sure.

At least get the reference correct. Danger, Will Robinson!

Dr Smith is the bumbling idiot https://www.youtube.com/watch?v=OWwOJlOI1nU

It was well before my time, I'm just glad I got the robot's name correct!