Sunday, October 9, 2016

Project 5- Buttons!

Project 5 – Buttons

Our first use of a digital input, a push button to control a LED.
Molly pushing the button

Hardware concepts we learned were:
  • ·         How a push button can regulate voltage and send signals to the microcontroller
  • ·         Why a push button needs more resistance than an LED, and the use of a “pull-up” resistor to prevent false readings from the button



Programming concepts we learned were:
  1. ·         Logic Operations (==,!=,&&,||,!) to build if..then statements
  2. ·         digitalRead for a digital input


Our Program-  If you push one of the buttons, light up the LED.  If you press both at the same time, turn it off.

const int button1Pin = 2;  // pushbutton 1 pin
const int button2Pin = 3;  // pushbutton 2 pin
const int ledPin =  13;    // LED pin
void setup()
{  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(ledPin, OUTPUT); 
}
void loop()
{  int button1State, button2State;
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  if (((button1State == LOW) || (button2State == LOW)) 
      ((button1State == LOW) && (button2State == LOW)))
    digitalWrite(ledPin, HIGH);
  }  else  {    digitalWrite(ledPin, LOW);    }

}

No comments:

Post a Comment