guptaaryan1010
Published

LED pattern changer using push buttons

Today, we will learn how to use led's and four pushbuttons to control its pattern.

IntermediateFull instructions provided4,868
LED pattern changer using push buttons

Things used in this project

Hardware components

Resistor 1k ohm
Resistor 1k ohm
Those who are using four pushbutton , they will need four resistors . Those who are using one touch sensor , will only need three.
×4
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×4
Touch sensor
optional for one push button.
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Arduino UNO
Arduino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Web Editor
Arduino Web Editor
optional

Story

Read more

Schematics

Circuit Diagram

For those who have four pushbuttons.

Circuit Diagram

For those who do not have four.

Code

My code.

C/C++
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
const int led5 = 6;
const int led6 = 7;
const int led7 = 8;
const int led8 = 9;
const int BUTTON1Pin= 12 ;
const int BUTTON2Pin= 11 ;
const int BUTTON3Pin= 10 ;
const int BUTTON4Pin= 13 ;                                                                                      

int BUTTON1State = 0;
int BUTTON2State = 0;
int BUTTON3State = 0;
int BUTTON4State = 0;
void setup() {
 
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);
  pinMode(led6,OUTPUT);
  pinMode(led7,OUTPUT);
  pinMode(led8,OUTPUT);
  pinMode(BUTTON1Pin,INPUT);
  pinMode(BUTTON2Pin,INPUT);
  pinMode(BUTTON3Pin,INPUT);
  pinMode(BUTTON4Pin,INPUT);
}

void loop() {
  BUTTON1State = digitalRead(BUTTON1Pin);
  BUTTON2State = digitalRead(BUTTON2Pin);
  BUTTON3State = digitalRead(BUTTON3Pin);
  BUTTON4State = digitalRead(BUTTON4Pin);
  if (BUTTON1State == HIGH) {
    // turn LED on:
    digitalWrite(led1 , HIGH);
  digitalWrite(led2 , HIGH);
  digitalWrite(led3 , HIGH);
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  digitalWrite(led6 , HIGH);
  digitalWrite(led7 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(900);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led1 , HIGH);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , HIGH);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , HIGH);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , HIGH);
  digitalWrite(led8 , LOW);
  delay(900);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , HIGH);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , HIGH);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , HIGH);
  delay(900);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led1 , HIGH);
  digitalWrite(led2 , HIGH);
  digitalWrite(led3 , HIGH);
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  digitalWrite(led6 , HIGH);
  digitalWrite(led7 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(900);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led1 , HIGH);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , HIGH);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , HIGH);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , HIGH);
  digitalWrite(led8 , LOW);
  delay(900);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , HIGH);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , HIGH);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , HIGH);
  delay(900);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  } else {
    digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  }
  if (BUTTON2State == HIGH) {
    digitalWrite(led1 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , HIGH);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , HIGH);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , HIGH);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led8 , LOW);
  delay(100);//this is the break 
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led8 , LOW);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led7 , LOW);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led6 , LOW);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led5 , LOW);
  digitalWrite(led4 , HIGH);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led3 , HIGH);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led2 , HIGH);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led1 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  delay(100);//this is the break 
  digitalWrite(led1 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led2 , HIGH);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , HIGH);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , HIGH);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led8 , LOW);
  delay(100);//this is the break 
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led8 , LOW);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led7 , LOW);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led6 , LOW);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led5 , LOW);
  digitalWrite(led4 , HIGH);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led3 , HIGH);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led2 , HIGH);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led1 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  delay(100);//this is the break 
  }else{
    digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
    
  }
  if (BUTTON3State == HIGH) {
    digitalWrite(led1 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  delay(100);
  digitalWrite(led4 , HIGH);
  delay(100);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  delay(100);
  digitalWrite(led4 , LOW);
  delay(100);
  digitalWrite(led5 , LOW);
  delay(100);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led8 ,LOW);
  delay(100);//this is the break
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led4 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  delay(100);
  digitalWrite(led1 , HIGH);
  delay(100);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led5 , LOW);
  delay(100);
  digitalWrite(led4 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  delay(100);
  digitalWrite(led1 , LOW);
  delay(100);//this is the break
  digitalWrite(led1 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  delay(100);
  digitalWrite(led4 , HIGH);
  delay(100);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led5 , LOW);
  delay(100);
  digitalWrite(led4 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  delay(100);
  digitalWrite(led1 ,LOW);
  delay(100);//this is the break
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led4 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  delay(100);
  digitalWrite(led1 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  delay(100);
  digitalWrite(led4 , LOW);
  delay(100);
  digitalWrite(led5 , LOW);
  delay(100);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led8 , LOW);
  delay(100);
  } else{
    digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
    
  }
  if (BUTTON4State == HIGH) {
    digitalWrite(led1 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);//this is the break
  digitalWrite(led1 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  delay(100);//this is the brek
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led1 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);//this is the break
   digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led1 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  delay(200);
  digitalWrite(led1 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);//this is the break
  digitalWrite(led1 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  delay(100);//this is the brek
  digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led1 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);//this is the break
   digitalWrite(led4 , HIGH);
  digitalWrite(led5 , HIGH);
  delay(100);
  digitalWrite(led3 , HIGH);
  digitalWrite(led6 , HIGH);
  delay(100);
  digitalWrite(led2 , HIGH);
  digitalWrite(led7 , HIGH);
  delay(100);
  digitalWrite(led1 , HIGH);
  digitalWrite(led8 , HIGH);
  delay(100);
  digitalWrite(led1 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
  digitalWrite(led2 , LOW);
  digitalWrite(led7 , LOW);
  delay(100);
  digitalWrite(led3 , LOW);
  digitalWrite(led6 , LOW);
  delay(100);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  delay(200);
  } else {
    digitalWrite(led1 , LOW);
  digitalWrite(led2 , LOW);
  digitalWrite(led3 , LOW);
  digitalWrite(led4 , LOW);
  digitalWrite(led5 , LOW);
  digitalWrite(led6 , LOW);
  digitalWrite(led7 , LOW);
  digitalWrite(led8 , LOW);
  delay(100);
    
  
  
  
  
  

 
  
  
  

}
  
}
  

Credits

guptaaryan1010
1 project • 1 follower

Comments