„Button” változatai közötti eltérés
Ugrás a navigációhoz
Ugrás a kereséshez
79. sor: | 79. sor: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<html> | |||
<pre> | |||
<span style="color: #007F00;">/*</span> | |||
<span style="color: #007F00;"> Button</span> | |||
<span style="color: #007F00;"> </span> | |||
<span style="color: #007F00;"> Turns on and off a light emitting diode(LED) connected to digital </span> | |||
<span style="color: #007F00;"> pin 13, when pressing a pushbutton attached to pin 2. </span> | |||
<span style="color: #007F00;"> </span> | |||
<span style="color: #007F00;"> </span> | |||
<span style="color: #007F00;"> The circuit:</span> | |||
<span style="color: #007F00;"> * LED attached from pin 13 to ground </span> | |||
<span style="color: #007F00;"> * pushbutton attached to pin 2 from +5V</span> | |||
<span style="color: #007F00;"> * 10K resistor attached to pin 2 from ground</span> | |||
<span style="color: #007F00;"> </span> | |||
<span style="color: #007F00;"> * Note: on most Arduinos there is already an LED on the board</span> | |||
<span style="color: #007F00;"> attached to pin 13.</span> | |||
<span style="color: #007F00;"> </span> | |||
<span style="color: #007F00;"> </span> | |||
<span style="color: #007F00;"> created 2005</span> | |||
<span style="color: #007F00;"> by DojoDave <http://www.0j0.org></span> | |||
<span style="color: #007F00;"> modified 30 Aug 2011</span> | |||
<span style="color: #007F00;"> by Tom Igoe</span> | |||
<span style="color: #007F00;"> modified Apr 27 2012</span> | |||
<span style="color: #007F00;"> by Robert Wessels</span> | |||
<span style="color: #007F00;"> </span> | |||
<span style="color: #007F00;"> This example code is in the public domain.</span> | |||
<span style="color: #007F00;"> </span> | |||
<span style="color: #007F00;"> http://www.arduino.cc/en/Tutorial/Button</span> | |||
<span style="color: #007F00;"> */</span> | |||
<span style="color: #007F00;">// constants won't change. They're used here to </span> | |||
<span style="color: #007F00;">// set pin numbers:</span> | |||
<span style="color: #FF0000;">const</span> <span style="color: #FF8000;">int</span> buttonPin = PUSH2; <span style="color: #007F00;">// the number of the pushbutton pin</span> | |||
<span style="color: #FF0000;">const</span> <span style="color: #FF8000;">int</span> ledPin = GREEN_LED; <span style="color: #007F00;">// the number of the LED pin</span> | |||
<span style="color: #007F00;">// variables will change:</span> | |||
<span style="color: #FF8000;">int</span> buttonState = 0; <span style="color: #007F00;">// variable for reading the pushbutton status</span> | |||
<span style="color: #FF0000;">void</span> <span style="color: #8B308B;"><b>setup</b></span>() { | |||
<span style="color: #007F00;">// initialize the LED pin as an output:</span> | |||
<span style="color: #FF8000;">pinMode</span>(ledPin, <span style="color: #0066FF;">OUTPUT</span>); | |||
<span style="color: #007F00;">// initialize the pushbutton pin as an input:</span> | |||
<span style="color: #FF8000;">pinMode</span>(buttonPin, <span style="color: #0066FF;">INPUT_PULLUP</span>); | |||
} | |||
<span style="color: #FF0000;">void</span> <span style="color: #8B308B;"><b>loop</b></span>(){ | |||
<span style="color: #007F00;">// read the state of the pushbutton value:</span> | |||
buttonState = <span style="color: #FF8000;">digitalRead</span>(buttonPin); | |||
<span style="color: #007F00;">// check if the pushbutton is pressed.</span> | |||
<span style="color: #007F00;">// if it is, the buttonState is HIGH:</span> | |||
<span style="color: #FF0000;">if</span> (buttonState == <span style="color: #0066FF;">HIGH</span>) { | |||
<span style="color: #007F00;">// turn LED on: </span> | |||
<span style="color: #FF8000;">digitalWrite</span>(ledPin, <span style="color: #0066FF;">HIGH</span>); | |||
} | |||
<span style="color: #FF0000;">else</span> { | |||
<span style="color: #007F00;">// turn LED off:</span> | |||
<span style="color: #FF8000;">digitalWrite</span>(ledPin, <span style="color: #0066FF;">LOW</span>); | |||
} | |||
} | |||
</pre> | |||
</html> |
A lap 2014. május 11., 23:10-kori változata
Kapcsolók
Kapcsolások |
Nyomógomb |
Pull up |
Pull down |
Tranzisztorok |
Kapcsolások tranzisztorokkal |
Kapcsolás mgszakítással (interrupts) |
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
modified Apr 27 2012
by Robert Wessels
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = PUSH2; // the number of the pushbutton pin
const int ledPin = GREEN_LED; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
/* Button Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2. The circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground * Note: on most Arduinos there is already an LED on the board attached to pin 13. created 2005 by DojoDave <http://www.0j0.org> modified 30 Aug 2011 by Tom Igoe modified Apr 27 2012 by Robert Wessels This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button */ // constants won't change. They're used here to // set pin numbers: const int buttonPin = PUSH2; // the number of the pushbutton pin const int ledPin = GREEN_LED; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT_PULLUP); } void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }