„Button” változatai közötti eltérés

Innen: apaboard
Ugrás a navigációhoz Ugrás a kereséshez
a (Levédte a(z) Button lapot ([Szerkesztés=Csak adminisztrátoroknak engedélyezett] (határozatlan) [Átnevezés=Csak adminisztrátoroknak engedélyezett] (határozatlan)))
 
(24 közbenső módosítás ugyanattól a szerkesztőtől nincs mutatva)
16. sor: 16. sor:
|-
|-
|}
|}
=== Digitális kapcsolás forráskód ===
<source lang="c" collapse="true" first-line="1">
/*
  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);
  }
}
</source>
<html>
<pre>
<span style="color: #007F00;">/*</span>
<span style="color: #007F00;">&nbsp;&nbsp;Button</span>
<span style="color: #007F00;">&nbsp;</span>
<span style="color: #007F00;">&nbsp;Turns&nbsp;on&nbsp;and&nbsp;off&nbsp;a&nbsp;light&nbsp;emitting&nbsp;diode(LED)&nbsp;connected&nbsp;to&nbsp;digital&nbsp;&nbsp;</span>
<span style="color: #007F00;">&nbsp;pin&nbsp;13,&nbsp;when&nbsp;pressing&nbsp;a&nbsp;pushbutton&nbsp;attached&nbsp;to&nbsp;pin&nbsp;2.&nbsp;</span>
<span style="color: #007F00;">&nbsp;</span>
<span style="color: #007F00;">&nbsp;</span>
<span style="color: #007F00;">&nbsp;The&nbsp;circuit:</span>
<span style="color: #007F00;">&nbsp;*&nbsp;LED&nbsp;attached&nbsp;from&nbsp;pin&nbsp;13&nbsp;to&nbsp;ground&nbsp;</span>
<span style="color: #007F00;">&nbsp;*&nbsp;pushbutton&nbsp;attached&nbsp;to&nbsp;pin&nbsp;2&nbsp;from&nbsp;+5V</span>
<span style="color: #007F00;">&nbsp;*&nbsp;10K&nbsp;resistor&nbsp;attached&nbsp;to&nbsp;pin&nbsp;2&nbsp;from&nbsp;ground</span>
<span style="color: #007F00;">&nbsp;</span>
<span style="color: #007F00;">&nbsp;*&nbsp;Note:&nbsp;on&nbsp;most&nbsp;Arduinos&nbsp;there&nbsp;is&nbsp;already&nbsp;an&nbsp;LED&nbsp;on&nbsp;the&nbsp;board</span>
<span style="color: #007F00;">&nbsp;attached&nbsp;to&nbsp;pin&nbsp;13.</span>
<span style="color: #007F00;">&nbsp;</span>
<span style="color: #007F00;">&nbsp;</span>
<span style="color: #007F00;">&nbsp;created&nbsp;2005</span>
<span style="color: #007F00;">&nbsp;by&nbsp;DojoDave&nbsp;&lt;http://www.0j0.org&gt;</span>
<span style="color: #007F00;">&nbsp;modified&nbsp;30&nbsp;Aug&nbsp;2011</span>
<span style="color: #007F00;">&nbsp;by&nbsp;Tom&nbsp;Igoe</span>
<span style="color: #007F00;">&nbsp;modified&nbsp;Apr&nbsp;27&nbsp;2012</span>
<span style="color: #007F00;">&nbsp;by&nbsp;Robert&nbsp;Wessels</span>
<span style="color: #007F00;">&nbsp;</span>
<span style="color: #007F00;">&nbsp;This&nbsp;example&nbsp;code&nbsp;is&nbsp;in&nbsp;the&nbsp;public&nbsp;domain.</span>
<span style="color: #007F00;">&nbsp;</span>
<span style="color: #007F00;">&nbsp;http://www.arduino.cc/en/Tutorial/Button</span>
<span style="color: #007F00;">&nbsp;*/</span>
<span style="color: #007F00;">//&nbsp;constants&nbsp;won't&nbsp;change.&nbsp;They're&nbsp;used&nbsp;here&nbsp;to&nbsp;</span>
<span style="color: #007F00;">//&nbsp;set&nbsp;pin&nbsp;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;">//&nbsp;variables&nbsp;will&nbsp;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>() {
&nbsp;&nbsp;<span style="color: #007F00;">// initialize the LED pin as an output:</span>
&nbsp;&nbsp;<span style="color: #FF8000;">pinMode</span>(ledPin, <span style="color: #0066FF;">OUTPUT</span>);     
&nbsp;&nbsp;<span style="color: #007F00;">// initialize the pushbutton pin as an input:</span>
&nbsp;&nbsp;<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>(){
&nbsp;&nbsp;<span style="color: #007F00;">// read the state of the pushbutton value:</span>
&nbsp;&nbsp;buttonState&nbsp;=&nbsp;<span style="color: #FF8000;">digitalRead</span>(buttonPin);
&nbsp;&nbsp;<span style="color: #007F00;">// check if the pushbutton is pressed.</span>
&nbsp;&nbsp;<span style="color: #007F00;">// if it is, the buttonState is HIGH:</span>
&nbsp;&nbsp;<span style="color: #FF0000;">if</span> (buttonState == <span style="color: #0066FF;">HIGH</span>) {   
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007F00;">// turn LED on:    </span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF8000;">digitalWrite</span>(ledPin, <span style="color: #0066FF;">HIGH</span>); 
&nbsp;&nbsp;}&nbsp;
&nbsp;&nbsp;<span style="color: #FF0000;">else</span> {
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007F00;">// turn LED off:</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF8000;">digitalWrite</span>(ledPin, <span style="color: #0066FF;">LOW</span>);
&nbsp;&nbsp;}
}
</pre>
</html>

A lap jelenlegi, 2014. május 29., 12:58-kori változata

Kapcsolók

Kapcsolások
Nyomógomb
Pull up
Pull down
Tranzisztorok
Kapcsolások tranzisztorokkal
Kapcsolás mgszakítással (interrupts)

Digitális kapcsolás forráskód

/*
  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); 
  }
}