Asking questions is the first step to gaining knowledge.
Using deadly force is a complex subject, with both a moral and legal component. The legal question is easiest to answer, as the laws are written, and there for all to see. Depending on your location, the laws vary considerably. And what the law defines is the conditions where you are justified in using deadly force, meaning that you would not be found legally liable, not that you would be morally correct.
The law allows the use of deadly force in certain situations. It does not require it, only provides circumstances where it is justified. Study your specific state (and local) laws carefully, as a first step.
Learn, and live by, the rules for safe gun handling. All of them, especially the one that says "know your target and what is behind it."
Morally (and legally most places), absent a clear, immediate threat of death or serious bodily injury to yourself (or family member) you are not justified shooting someone. And you are rarely justified shooting at them.
Warning shots are serious. And they are a double edged sword. Remember that you are responsible for every round you fire. That warning shot goes somewhere, and you are legally responsible for whatever it hits. Also, some jurisdictions have legal restrictions about warning shots. In general, in todays society they are a very poor idea. If you wind up in court, (either civil or criminal) due to shooting, having fired a "warning shot" can be seen as proof that you were not convinced that the use of deadly force was necessary and your only option.
Using deadly force in self defense is a very complicated and convoluted issue. Many books and discussion forums are dedicated to it.
One thing I am sure of, firing a shot in the dark, only because you hear a noise at night is a very, very bad idea.
In general, you are allowed to use deadly force to protect your life, but not your property. And this is very, very dependent on the specifics of the situation.
Asking what you did, the way you did tells us that you are just beginning to explore this area, and you need to do a lot of research on the subject before relying on a gun for personal defense. Doing anything else is a virtual guarantee of tragedy.
There are many resources available for information, please educate yourself to both the legal and moral issues involved, before a situation arises where you might make a disasterously wrong decision.