Let me comment on your code.
void loop() {
}
Loop has nothing inside. This means that there is no program for the Arduino to "always run". Read about the LOOP function.
void setup() {
pinMode(led, OUTPUT);
pinMode(5, INPUT);
if (digitalRead(5) == LOW){
loop(); {
analogWrite(led, 150);
}
} else(digitalRead(5) == HIGH);{
loop(); {
analogWrite(led, 255);
delay(500)}
analogWrite(led, 0);
delay(500);}
}
Here is "your whole program". SETUP runs only once, whenever your Arduino is turned on or reset occurs. Soon, your program runs only once and it does not generate the result you expect.
Definition of pins:
int led = 5;
int mySwitch = 6;
SETUP
void setup() {
pinMode(led, OUTPUT);
pinMode(mySwitch , INPUT);
}
LOOP
void loop() {
if (digitalRead(mySwitch) == LOW) {
analogWrite(led, 50);
} else if (digitalRead(mySwitch) == HIGH) {
analogWrite(led, 255);
delay(500);
analogWrite(led, 0);
delay(500);
}
}
When mounting this, remember to insert a 10K resistor to pull down on pin 6 (or whenever you use a pin whose input you want to know if it is in LOW). Google "pull down resistor". When after you upload the program to Arduino and you have insert 10K resistor pull down, you will notice the "slightly lit" LED.
When making mySwitch input HIGH, the Led will start blinking as you want.
larryd:
Examine some of the examples in the IDE to see how things are formatted.
.
Larryd said to observe the examples inside the Arduino IDE.
Go to file / examples / basics and test the codes from there and understand them. This will help in your development.