Phuong Vo
Published © MIT

Dimming Lights with PWM using Push Button

This project will show you how to fade lights, add a button to it.

BeginnerFull instructions provided1 hour29,836
Dimming Lights with PWM using Push Button

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
The amount you use may differ, it depends on the method that is used.
×11
LED (generic)
LED (generic)
Any colors would do, but I recommend 3 different colors.
×3
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 330 ohm
Resistor 330 ohm
×4

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Fade Lights

Fade Lights with Push Button

Code

Fade Lights

Arduino
int ledRed = 9;
int ledYellow = 6;
int ledGreen = 3;
const int fade = 10;

void setup() {
  pinMode(ledRed, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(ledGreen, OUTPUT);

}

void loop() {
  //ledRed
  for (int i = 0; i <= 255; i++) {
    analogWrite(ledRed, i);
    delay (fade);
  }
  for (int i = 255; i >= 0; i--) {
    analogWrite(ledRed, i);
    delay (fade);
  }

  //ledYellow
  for (int i = 0; i <= 255; i++) {
    analogWrite(ledYellow, i);
    delay (fade);
  }
  for (int i = 255; i >= 0; i--) {
    analogWrite(ledYellow, i);
    delay (fade);
  }

  //ledGreen
  for (int i = 0; i <= 255; i++) {
    analogWrite(ledGreen, i);
    delay (fade);
  }
  for (int i = 255; i >= 0; i--) {
    analogWrite(ledGreen, i);
    delay (fade);
  }

  //ledYellow
  for (int i = 0; i <= 255; i++) {
    analogWrite(ledYellow, i);
    delay (fade);
  }
  for (int i = 255; i >= 0; i--) {
    analogWrite(ledYellow, i);
    delay (fade);
  }

}

Fade Lights with Push Button

Arduino
int ledRed = 9;
int ledYellow = 6;
int ledGreen = 3;
int button = 7;
const int fade = 10;



void setup() {
  pinMode(ledRed, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(button, INPUT);
  digitalWrite(ledRed, LOW);
  digitalWrite(ledYellow, LOW);
  digitalWrite(ledGreen, HIGH);
}


void loop() {

  if (digitalRead(button) == HIGH) {
    delay(15);
    if (digitalRead(button) == HIGH) {
      fadeLights();
    }
  } else {
    digitalWrite(ledRed, LOW);
    digitalWrite(ledYellow, LOW);
    digitalWrite(ledGreen, HIGH);
  }
}

void fadeLights() {

  for (int i = 255; i >= 0; i--) {
    analogWrite(ledGreen, i);
    delay (fade);
  }
  //ledYellow
  for (int i = 0; i <= 255; i++) {
    analogWrite(ledYellow, i);
    delay (fade);
  }
  for (int i = 255; i >= 0; i--) {
    analogWrite(ledYellow, i);
    delay (fade);
  }

  //ledRed
  for (int i = 0; i <= 255; i++) {
    analogWrite(ledRed, i);
    delay (fade);
  }
  for (int i = 255; i >= 0; i--) {
    analogWrite(ledRed, i);
    delay (fade);
  }

  //ledYellow
  for (int i = 0; i <= 255; i++) {
    analogWrite(ledYellow, i);
    delay (fade);
  }
  for (int i = 255; i >= 0; i--) {
    analogWrite(ledYellow, i);
    delay (fade);
  }


  //ledGreen
  for (int i = 0; i <= 255; i++) {
    analogWrite(ledGreen, i);
    delay (fade);
  }

}

Fade Lights with Push Button (improvement to delay)

Arduino
int ledRed = 9;
int ledYellow = 6;
int ledGreen = 3;
int button = 7;
const int fade = 10;

void setup() {
  pinMode(ledRed, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(button, INPUT);
  digitalWrite(ledRed, LOW);
  digitalWrite(ledYellow, LOW);
  digitalWrite(ledGreen, HIGH);

}

void loop() {
  if(digitalRead(button) == HIGH) {
    delay(15);
    if(digitalRead(button) == HIGH){
      fadeLights();
    }
  } else {
    digitalWrite(ledRed, LOW);
    digitalWrite(ledYellow, LOW);
    digitalWrite(ledGreen, HIGH);
   }
} 


void fadeLights(){
  
    for( int i=255; i>=0; i--) {
    analogWrite(ledGreen, i);
    for( int y = 0; y < 1000; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

   //ledYellow
  for( int i=0; i<=255; i++) {
    analogWrite(ledYellow, i);
    for( int y = 0; y < 400; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

  for( int i=255; i>=0; i--) {
    analogWrite(ledYellow, i);
    for( int y = 0; y < 400; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }
  
  //ledRed
  for( int i=0; i<=255; i++) {
    analogWrite(ledRed, i);
    for( int y = 0; y < 1000; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }
  for( int i=255; i>=0; i--) {
    analogWrite(ledRed, i);
    for( int y = 0; y < 1000; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

  //ledYellow
  for( int i=0; i<=255; i++) {
    analogWrite(ledYellow, i);
    for( int y = 0; y < 400; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

  for( int i=255; i>=0; i--) {
    analogWrite(ledYellow, i);
    for( int y = 0; y < 400; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

  //ledGreen
  for( int i=0; i<=255; i++) {
    analogWrite(ledGreen, i);
    for( int y = 0; y < 1000; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }
}

Credits

Phuong Vo

Phuong Vo

4 projects • 10 followers
Hi

Comments