Inverted switch

is there anyway i can make something turn on when i open a box without it beiong on constantly to check if it is open? 

the box opens sort of like a hinge but there is nothing that touches when open that doesn't touch when closed

thanks :)

 

Use an spdt switch
Use an spdt switch

sorry

sorry i’m not quite sure how that would help could you explain?

 

You could always

You could always just invert the logic in your code (assuming the switch state is read by a microcontroller or similar).

Another alternative would be to make the switch’s closed state the ‘low’ logic state, and then you’d pull-up to the ‘high’ state when the switch opened.

Some more info on what the switch is connected to would probably help.

"Constantly check"

Because of the nature of code, you are going to be constantly checking no matter what. The button itself can click to low with a pull-up resistor or click to high with a pull-down resistor (it doesn’t matter). It could also be connected as N.C. or N.O. as to be clicked or unclicked when the lid opens (again, does not matter). 

In terms of the code, there is really only one way to know if the button has been triggered --keep checking it. You can do this each time you loop through the main loop of your code or you can use an interrupt. An interrupt will “interrupt” your code and send the code down to a specified “interrupt routine” where the “button got pushed” code will be executed. 

Now, in actuallity, when you attach an interrupt to your program, the state of the interrupt pin IS actually being “constantly checked” it is simply being done “automatically” and sorta “in the background”. Either way you go about this, one way or the other, that button will need to be “constantly checked”. 

Just did a re-read

I just noticed that you mention the fact that you don’t want whatever you are working on, to be “on” all the time… Using an interrupt, you can allow the microcontroller (assuming you are using one, your question was a bit vague) to go into a low-power state and then be “woken up” when the interrupt is triggered.

Use a SPDT switch as your

Use a SPDT switch as your power switch. When the box is closed and the switch is pushed down, then it will be off. When the box is open and the switch is up then it will be on depending on how you wire it. You can buy them with little levers on them and they are easily “clicked”.

I do not see how this has anything to do with code. 

provided you are not going

provided you are not going to use it in total darkness you could use a light sensor of some sort.

 

I would do it like Patrick

I would do it like Patrick says and have the switch act as the power switch. I used this exact same setup in my mailbox sensor https://www.robotshop.com/letsmakerobots/node/23524 The great thing about this setup is that when the lid is closed the circuit doesn’t draw any current so you battery would last for ages.