2017年1月15日 星期日

WEEK12 按鈕製作

按鈕實作方法1



程式碼:

void setup(){
  pinMode(13,OUTPUT);
  pinMode(2,INPUT);
 }
void loop(){
  int result= digitalRead(2);
  if(result==HIGH) digitalWrite(13,HIGH);
  else digitalWrite(13,LOW);
}







按鈕實作方法2(簡化版)




程式碼: 

void setup(){
  pinMode(13,OUTPUT);
  pinMode(2,INPUT_PULLUP);
 }
void loop(){
  int result= digitalRead(2);
  if(result==HIGH) digitalWrite(13,LOW);
  else digitalWrite(13,HIGH);
}













光敏電阻



程式碼:

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/4);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue/4);
}

沒有留言:

張貼留言