Whenever the button is pressed down, the red LED lights up. I actually had to build my circuit with a resistor. Our textbook never really tells us why this is necessary and how the circuit actually works. I understand the code completely, but I don't quite understand the electronics. I have a few questions I'm hoping to ask Dominic in person.
Pressing the momentary button down to light up the red LED. |
/*
Turns on an LED when the momentary switch is held down.
*/
int led = 13;
int button = 7;
int val = 0;
void setup() {
// initialize then digital pins as input/output for the LED an switch
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
val = digitalRead(button);
if (val == HIGH) { //switch is pressed
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
} else {
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
}
}
No comments:
Post a Comment