My Arduino code only works properly when connected to my Laptop Can anyone help?

Basically im new to arduino and just learning how to code so for my first proper project i thought it dry an RFID Lock just using a basic servo an rgb LED and the standard rfid reader, it all works perfectly when connected to my pc however when its not no luck it fritzes out letting all passes have access and flashing the greed light if anyone has any ideas please share, my other power source is the 9v battery that also came with the arduino, ive attatched the code im using to this post, oh yeah im using an arduino mega 2560 and i know that an uno would probably be better for a small project like this but i only had the mega on hand

RFID_lock_no_id.ino (946 Bytes)

If you mean one of the 9v PP3 style smoke-alarm batteries then they can't produce enough current.

Try a pack of 6 x AA alkaline cells.

...R

it all works perfectly when connected to my pc however when its not no luck it fritzes out

That sounds like a missing ground connection that is being made accidentally by the connection to the lap top.