Constantly check digitalRead?

Let me sum what I want to do quickly, I want an LED on pin 1 to run at PWM 150, and when a button on pin 5 is pushed, I want it to blink at 255.

Here is my code:

int led = 1;

void loop() {
}

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);}
}

I know what is wrong, But I just can't figure out how to make digitalRead check every few ms for the button push. I'm new to this, in-fact, this is one of my first projects. Thanks everyone, I look forward to making lots of useful things with the Arduino.

Examine some of the examples in the IDE to see how things are formatted. .

} else(digitalRead(5) == HIGH);{

Use: else if

Get rid of the ;

.

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.

Thanks a lot for helping out with this, :), And i'll go read some of the examples now.