9.6 Example: ledButton

/* ledButton. 
This script will cause an LED to turn on when a
push-button switch is pressed. The LED is attached to pin 13
and the button is attached to pin 7. */

#define LED 13
#define BUTTON 7
int val=0;

void setup() {
   pinMode(LED, OUTPUT);
   pinMode(BUTTON, INPUT);
}

void loop() {
   val=digitalRead(BUTTON);
   if (val == HIGH){
      digitalWrite(LED, HIGH);
    else
      digitalWrite(LED, LOW);
   }
}

License

Icon for the Creative Commons Attribution 4.0 International License

Applied Electrical Engineering Fundamentals by David J. McLaughlin is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.

Share This Book