13 Mart 2015 Cuma

Arduino Experiment 5 - Push Buttons Modify

Circuit Diagram

Definition : In here, we use 2 buttons and 3 function to blink our leds. If left button is pushed, the leds will blink like disco. If right button pushed we have ordered blinking(from left to right) and if both are pushed, three of leds blink at the same time.





Components


  •          5 x 1K resistors
  •          3 x LED
  •          2 x On / Off Buttons
  •          Wires
  •          Arduino Uno


Arduino Code

// First we'll set up constants for the pin numbers.
// This will make it easier to follow the code below.

const int button1Pin = 2;  // pushbutton 1 pin
const int button2Pin = 3;  // pushbutton 2 pin
const int ledPin =  13;    // LED pin
const int ledPin2 = 12;
const int ledPin3 = 11;


void setup()
{
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);

  // Set up the LED pin to be an output:
  pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin2, OUTPUT);

}


void loop()
{
  int button1State, button2State;  // variables to hold the pushbutton states

  // Since a pushbutton has only two states (pushed or not pushed),
  // we've run them into digital inputs. To read an input, we'll
  // use the digitalRead() function. This function takes one
  // parameter, the pin number, and returns either HIGH (5V)
  // or LOW (GND).

  // Here we'll read the current pushbutton states into
  // two variables:

  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  if ((button1State == LOW)) {
    for(int i = 2; i>=1; i--) {
    digitalWrite(ledPin, HIGH);  // turn the LED on
    delay(500);
    digitalWrite(ledPin2, HIGH);  // turn the LED
    delay(500);
    digitalWrite(ledPin3, HIGH);  // turn the LED on
    delay(500);
    digitalWrite(ledPin, LOW);  // turn the LED on
    delay(500);
    digitalWrite(ledPin2, LOW);  // turn the LED on
    delay(500);
    digitalWrite(ledPin3, LOW);  // turn the LED on
    delay(500);
       }  
  }    

  if ((button1State == LOW) && (button2State == LOW))  // if we're pushing button 1 OR button 2
        {                                             // AND we're NOT
      // pushing button 1 AND button 2
                                                     // then...


    digitalWrite(ledPin, HIGH);  // turn the LED on
    digitalWrite(ledPin2, HIGH);  // turn the LED on
    digitalWrite(ledPin3, HIGH);  // turn the LED on
    delay(500);
    digitalWrite(ledPin, LOW);  // turn the LED on
    digitalWrite(ledPin2, LOW);  // turn the LED on
    digitalWrite(ledPin3, LOW);  // turn the LED on
    delay(500);
     digitalWrite(ledPin, HIGH);  // turn the LED on
    digitalWrite(ledPin2, HIGH);  // turn the LED on
    digitalWrite(ledPin3, HIGH);  // turn the LED on
    delay(500);
    digitalWrite(ledPin, LOW);  // turn the LED on
    digitalWrite(ledPin2, LOW);  // turn the LED on
    digitalWrite(ledPin3, LOW);  // turn the LED on
    delay(500);
     digitalWrite(ledPin, HIGH);  // turn the LED on
    digitalWrite(ledPin2, HIGH);  // turn the LED on
    digitalWrite(ledPin3, HIGH);  // turn the LED on
    delay(500);
    digitalWrite(ledPin, LOW);  // turn the LED on
    digitalWrite(ledPin2, LOW);  // turn the LED on
    digitalWrite(ledPin3, LOW);  // turn the LED on
  }
 
  if ((button2State == LOW)) {
    for(int i = 0; i<4; i++) {
    digitalWrite(ledPin, HIGH);  // turn the LED on
    digitalWrite(ledPin3, HIGH);  // turn the LED on
    delay(500);
    digitalWrite(ledPin2, HIGH);  // turn the LED on
 
    digitalWrite(ledPin, LOW);  // turn the LED on
    digitalWrite(ledPin3, LOW);  // turn the LED on
    delay(500);
    digitalWrite(ledPin2, LOW);  // turn the LED on
    }}}
  

Video : 

                                           

Hiç yorum yok:

Yorum Gönder