ジョージの技術備忘録

参考したサイトを忘れないための備忘録

Grove - Light Sensor

暗くなるとLEDが光り、明るくなるとLEDがつくアルゴリズムです。
閾値(thresholdvalue)はセンサによって仕様が変わるので調整が必要です。
Grove - Light Sensor - Wiki

/*
/* Grove - Light Sensor demo v1.0
* 
* signal wire to A0.
* By: http://www.seeedstudio.com
*/
#include <math.h>
const int ledPin=3;                 //Connect the LED Grove module to Pin12, Digital 12
const int thresholdvalue=18;         //The threshold for which the LED should turn on. 
float Rsensor; //Resistance of sensor in K
void setup() {
  Serial.begin(9600);                //Start the Serial connection
  pinMode(ledPin,OUTPUT);            //Set the LED on Digital 12 as an OUTPUT
}
void loop() {
  int sensorValue = analogRead(0); 
  Rsensor=(float)(1023-sensorValue)*10/sensorValue;
  if(Rsensor>thresholdvalue)
  {
    digitalWrite(ledPin,HIGH);
  }
  else
  {
  digitalWrite(ledPin,LOW);
  }
  
  Serial.print("the analog read data");
  Serial.print("\t");
  Serial.print("the sensor resistance");
  Serial.println("\t");
  
  Serial.print(sensorValue);
  Serial.print("\t");
  Serial.print("\t");
  Serial.print("\t");
  Serial.print(Rsensor,DEC);//show the light intensity on the serial monitor;
  Serial.println("\t");
  delay(1000);
}

f:id:yoshida-hideki94:20160522010548j:plain:h200:w150f:id:yoshida-hideki94:20160522010551j:plain:h200:w150