Operácie

Acrob004: Rozdiel medzi revíziami

Zo stránky SensorWiki

Balogh (diskusia | príspevky)
Bez shrnutí editace
Balogh (diskusia | príspevky)
Bez shrnutí editace
Riadok 43: Riadok 43:


<source lang="c">
<source lang="c">
#define SWITCH  3                // select the pin for Switch
#define LED_Red 7                // select the pin for LED


/*
int State = 0;                  // variable to store the pushbutton value
  Button
Turns on and off a light emitting diode(LED) connected to digital 
pin 13, when pressing a pushbutton attached to pin 7.
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 17 Jun 2009
by Tom Igoe
  http://www.arduino.cc/en/Tutorial/Button
*/


// constants won't change. They're used here to
void setup()
// set pin numbers:
{
const int buttonPin = 2;     // the number of the pushbutton pin
pinMode(LED_Red, OUTPUT );     // this pin is an OUTPUT
const int ledPin = 13;     // the number of the LED pin
  pinMode(SWITCH, INPUT );       // this pin is an INPUT
}


// variables will change:
void loop()                      // endless loop
int buttonState = 0;         // variable for reading the pushbutton status
{
 
State = digitalRead(SWITCH);
void setup() {
if (State == HIGH )            // notice 2x = !!!
  // initialize the LED pin as an output:
  digitalWrite(LED_Red, HIGH);    // make it visible
  pinMode(ledPin, OUTPUT);    
else
  // initialize the pushbutton pin as an input:
  digitalWrite(LED_Red, LOW);     // turn off
  pinMode(buttonPin, INPUT);    
delay(250);                     // wait 1/4s for another
}
}


void loop(){
</source>
  // 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>
[[Acrob003|< Previous]] | [[Acrob|Home]] | [[Acrob005|Next >]]
[[Acrob003|< Previous]] | [[Acrob|Home]] | [[Acrob005|Next >]]

Verzia z 14:31, 12. jún 2010

< Previous | Home | Next >


Pushbutton

To test the pushbutton, please connect one LED with current limiting resistor together with a pushbutton:

This setup doesn't require microcontroller, it is just to test it and recognize its function, pinout and operation. No program required!

Let's modify the circuit and try to measure the input with the microcontroller and display its value on the terminal. Modify the connection according the following diagram and download the attached piece of software.

#define SWITCH 3                 // select the pin for Switch

void setup()
{
 Serial.begin(9600);
 pinMode(SWITCH, INPUT );        // this pin is an INPUT
 Serial.println("Button test:");
}

void loop()                      // endless loop
{
 Serial.print("Input D3 = ");
 Serial.println(digitalRead(SWITCH),BIN);
 delay(250);                     // wait 1/4s for another
}


Another example uses the switch to control the LED. Exactly as in the first experiment, but now the controller makes the decision.

#define SWITCH  3                // select the pin for Switch
#define LED_Red 7                // select the pin for LED

int State = 0;                   // variable to store the pushbutton value

void setup()
{
 pinMode(LED_Red, OUTPUT );      // this pin is an OUTPUT
 pinMode(SWITCH, INPUT );        // this pin is an INPUT
}

void loop()                      // endless loop
{
 State =  digitalRead(SWITCH);
 if (State == HIGH )             // notice 2x = !!!
   digitalWrite(LED_Red, HIGH);    // make it visible
 else
   digitalWrite(LED_Red, LOW);     // turn off
 delay(250);                     // wait 1/4s for another
}


< Previous | Home | Next >