2016年3月6日 星期日

Arduino的教學11- 數位輸入 button 的的使用

一、學習目標: 學習使用button來控制 led燈

二、重點說明:
按鈕一腳串接一個10k的電阻。

三、接線圖:
buttonPin = 2;
ledPin = 13;



四、程式碼
/*
  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 
 modified 30 Aug 2011
 by Tom Igoe

 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 = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // 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);
}

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


練習01-設定按鈕後不同值(按鈕後依序出現 0-4值)
// set pin numbers:
const int buttonPin = 12;     // 按鈕輸入值pin

int ledCount=5;//除數
int no=0;  //流水號
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int thisNo=0;  //餘數值
void setup() {
  pinMode(buttonPin, INPUT);   
  Serial.begin(9600);   
}

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) {     
   
    no++;
    thisNo = no % ledCount;//取餘數
delay(100);
Serial.println(thisNo);
} 

    
  delay(100);
}
練習02-設定按鈕後 依序點亮不同的led燈


// set pin numbers:
const int buttonPin = 2;     // 按鈕輸入值pin
int ledPins[] = { 
  8,9,10};   // an array of pin numbers to which LEDs are attached

int ledCount=3;//除數
int no=0;  //流水號
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int thisNo=0;  //餘數值
void setup() {
  pinMode(buttonPin, INPUT);   
  Serial.begin(9600);   

  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT); 
  }

}

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) {     

    no++;
    thisNo = no % ledCount;//取餘數
    //    digitalWrite(ledPins[thisNo], HIGH );


    for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      if(thisLed == thisNo){

        digitalWrite(ledPins[thisLed], HIGH);
      }
      else{
        digitalWrite(ledPins[thisLed], LOW     );
      }

    }

    delay(100);
    Serial.println(thisNo);
  } 

  delay(100);
}



沒有留言:

張貼留言