2016年11月21日 星期一

/Noshiro/按鈕 - botton - 電路板

今天是教botton的使用方式

主要是botton將其作成一個迴路
然後利用其斷開和連接
讓LED量來測試
還會用到電阻

// http://www.arduino.cc/en/Tutorial/Button

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);
  }
}
//----------------------------------------------------------------------
利用裡面附贈的EXAMPLE 來完成
然後可以改

**----------------------
然後是更簡單的接法
只需兩條線
利用內建電阻



// http://www.arduino.cc/en/Tutorial/InputPullupSerial


void setup(){
  //start serial connection
  Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);

}

void loop(){
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  }
  else {
    digitalWrite(13, HIGH);
  }
}

//----------------------------------------------------------------------

上面的網址會有簡單接線教學

新增感光元件
//----------------


// http://arduino.cc/en/Tutorial/AnalogInput

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);  
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);        
  // turn the ledPin off:      
  digitalWrite(ledPin, LOW);  
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                
}

可以感測光的程度
被白色閃蝦@@

沒有留言:

張貼留言