Simple tilt sensor and the delay function

Hello everyone, new guy here

So i was having fun with my arduino and tried to mess with the tilt sensor i bought today, im doing a simple circuit that when we tilt it a red led lights up and if not tilted a white one lights up.

My circuit and code is working fine but i have a simple question, in the loop method if i dont add a delay function, lets say delay(200). One of the led starts to flash rapidly and the second is not lighting off when its expected too. So my question is why do we need to add a delay at the end of the loop, Is it because when i tilt my sensor the output is at a fast rate the arduino cant read it ?

Thank you for reading this and sorry for my english, not my native language

Wail

Can you copy and past your full code (using the code tags [code].... your code ... [/code]

Do you have current limiting resistors for your leds?

J-M-L:
Can you copy and past your full code (using the code tags [code].... your code ... [/code]

Do you have current limiting resistors for your leds?

yes i have resistors for my led, but i know now why it wasnt working well cause i didnt know we had connect the tilt sensor to the positive 3v with a resistor, i thought only the input pin was needed.
Thank you J-M-L

You wire it the same way you would with a push button. Best is to use an input pull-up rather than an extra resistor

So my question is why do we need to add a delay at the end of the loop

You do not have to. The ONLY reason that it is there is to have the LED on or off long enough for you to see it that it was turned on or off.