Alarm - Connection

Hello!

I haven't worked with the Arduino very much and I'm not good in programming.
My goal is, to build a alarm system, that reacts on movement/shock.
Therefore my Idea was to build a system simmilar to this example:

A plastic piece should fall out of the circuit(5V to pin 2)(simmilar to a button) and the Arduino should detect this.
So if the circuit is closed, the Arduino should make a sound for 5min (for example).
My problem is, that the sampleprogramm would detect a closed circuit, even if it isn't like this.

How can I programm the Arduino, that he only reacts in the case of a closed circuit?

Thank you for your answers!

My problem is, that the sampleprogramm would detect a closed circuit, even if it isn't like this.

Even if what wasn't like what?

How can I programm the Arduino, that he only reacts in the case of a closed circuit?

Wire the circuit correctly, first. Then, reading whether the pin is HIGH or LOW is trivial.

A plastic piece should fall out of the circuit(5V to pin 2)(simmilar to a button) and the Arduino should detect this.

Using the internal pullup resistor, and connecting a wire to a pin, to the device holding the plastic piece, and from the other side to ground, the plastic piece will prevent the connection from the pin to ground, so the pin will read HIGH. When the plastic piece falls out, and the pin to switch to ground is complete, the pin will read LOW.

My problem is, that the sampleprogramm would detect a closed circuit, even if it isn't like this.

Even if what wasn't like what?

If the circuit is closed, it says: 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
If the circuit isn't closed, it says: 0,0,0,1,1,0,1,0,0,0,1,1,1,1,0,0,1,1,1,0,0,0,1

How can I programm the Arduino, that he only reacts in the case of a closed circuit?

Wire the circuit correctly, first. Then, reading whether the pin is HIGH or LOW is trivial.

Sorry, but how can I do this?

A plastic piece should fall out of the circuit(5V to pin 2)(simmilar to a button) and the Arduino should detect this.

Using the internal pullup resistor, and connecting a wire to a pin, to the device holding the plastic piece, and from the other side to ground, the plastic piece will prevent the connection from the pin to ground, so the pin will read HIGH. When the plastic piece falls out, and the pin to switch to ground is complete, the pin will read LOW.

Sorry, what?
I haven't worked with the Arduino very much.
I don' understand this.
Talk to me, like to an idiot.
Can you give me an example, please?

If the circuit is closed, it says: 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
If the circuit isn't closed, it says: 0,0,0,1,1,0,1,0,0,0,1,1,1,1,0,0,1,1,1,0,0,0,1

What circuit? Quit waving your arms and post a schematic, a picture, or, at a minimum, a description of what is connected where. Post your code.

What you describe sounds like a floating pin condition, caused be lack of pullup or pulldown resistors.

Sorry, but how can I do this?

Using digitalRead().

Can you give me an example, please?

I don't know what your circuit looks like. I'm assuming that your piece of plastic is holding a switch open, and that removing the plastic allows the switch to close.

If that is correct, then one end of the switch goes to a digital pin, and the other end goes to ground.

Then, in setup:

pinMode(somePin, INPUT);
digitalWrite(somePin, HIGH); // Turn on the pullup resistor

Then, in loop:

int state = digitalRead(somePin);
if(state == LOW)
{
   // The plastic piece is gone. Raise a squawk.
}

First of all, thank you!

What circuit? Quit waving your arms and post a schematic, a picture, or, at a minimum, a description of what is connected where. Post your code.

It is just a wire from 5v to pin2, which is interrupted by a plastic part.
If the plastic piece falls out, the wire isn't interrupted, so electricity can flow.
My code: http://arduino.cc/en/Tutorial/Button

I'm assuming that your piece of plastic is holding a switch open, and that removing the plastic allows the switch to close.

That's correct.

If that is correct, then one end of the switch goes to a digital pin, and the other end goes to ground.

Really?
One goes to 5v and one to pin2 (http://arduino.cc/en/Tutorial/Button)

Do I have to put the wire in another pin?
Can you give me a complete code, please?
And maybe a connecting instruction?

Thank you very much!

That sketch shows how to wire a switch with an external pulldown resistor. It is simpler to wire the switch using the internal pullup resistor.

It is just a wire from 5v to pin2, which is interrupted by a plastic part.
If the plastic piece falls out, the wire isn't interrupted, so electricity can flow.

Still have no idea what you are talking about. "just a wire" doesn't have any plastic pieces that can fall out.

And maybe a connecting instruction?

I don't know how much simpler it can get than "put one end of the switch in the digital pin and the other end in ground".

Now, it is your turn. Post a picture of this "just a wire" with "a plastic piece that falls out".

It should be simmilar to this:

And now I have to put the cables into pin2 and ground?!?

Would you be so kind, to send me the complete programm code?

Thank you very much!

And now I have to put the cables into pin2 and ground?!?

After setting pin 2 as input and turning on the internal pullup resistor, yes.

Would you be so kind, to send me the complete programm code?

If you send me some money. I've already spelled out everything you need to do. It's your turn to do something.

How much money do you want?

Thank you for your answer!
You have helped me a lot.