Arduino: 光敏電阻

若亮度有一定變化就會讓LED亮
code:
int led = 2;
static int oldValue = 0;
// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  int value = analogRead(A0);
  int a = abs(value-oldValue);
  Serial.println(a);
  if(a > 50){
    digitalWrite(led, HIGH);
  }else{
    digitalWrite(led, LOW);
  }
  delay(50);
  oldValue = value;

}

作者

RongSon

Graduate Student of CCU COMM Game Development, Network Communication, macOS/Ubuntu/Android, Arduino/Raspberry Pi/Intel Edison, Java/Python/C/C++

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *