Skip to content

Instantly share code, notes, and snippets.

@cfsghost
Created October 13, 2016 14:41
Show Gist options
  • Save cfsghost/18dfa7b0a9b3b894cb4b68b18dbbfdfe to your computer and use it in GitHub Desktop.
Save cfsghost/18dfa7b0a9b3b894cb4b68b18dbbfdfe to your computer and use it in GitHub Desktop.
light-sensor.ino
/*
* 此範例
* A2接Light Sensor v1.1
* D4接Buzzer v1.2
* analogPin=撈取A2 Sensor值
* 計算方式 5v/1024每個刻度4.9mv左右
* 實際量測過這顆sensor 輸出最高數值約為2.08v左右
* 依照這個基準點算起來2.08v/4.9mv所以就會得到425左右的值輸入
* 此範例程式將撈取到的值供Delay使用
* 所以當沒有光線的時候Delay越少
*/
int analogPin = A2;
int digitalPin = 4;
int sensorValue = 0;
void setup() {
pinMode(digitalPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogPin);
digitalWrite(digitalPin, HIGH);
delay(sensorValue);
Serial.println(sensorValue);
digitalWrite(digitalPin, LOW);
delay(sensorValue);
Serial.println(sensorValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment