With the TEMT6000 light sensor, I can turn on a motor based on input to the light sensor, similar to the one I had with the DHT-22.
With the TEMT6000, I used an analog write (instead of the digital write with the DHT-22) and the data arrives in float form. Several lines of code convert the reading into a percent. Data smoothing and millis () set up are the same as previous DHT-22 code.
#define lightPin A0 //Ambient light sensor reading
//data smoothing
const int numReadings = 5;
float ligReadings [numReadings];
int ligReadIndex = 0;
float ligTotal = 0;
float ligAverage = 0;
int inputPin = lightPin; //put sensor pin here
//detecting sensor change
int last;
//response to sensor change
//for fan and motor
int speedPin = 5;
int dir1 = 4;
int dir2 = 3;
int mSpeed = 0;
unsigned long lastReadTime = 0;
void setup() {
// put your setup code here, to run once:
//pins for fan and motot
pinMode(speedPin, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(dir2, OUTPUT);
pinMode(lightPin, INPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
ligReadings[thisReading] = 0;
}
Serial.begin(9600);
lastReadTime = millis();
}
void loop() {
//delay(1000);
if ((millis() - lastReadTime) >= 5000UL) {
Serial.println(lastReadTime);
Serial.println(millis());
lastReadTime = millis();
//light sensor read
float light = analogRead(lightPin);
float light_ratio = light / 1023.0;
float light_percent = light_ratio * 100;
ligTotal = ligTotal - ligReadings[ligReadIndex];
ligReadings[ligReadIndex] = light_percent;
ligTotal = ligTotal + ligReadings [ligReadIndex];
ligReadIndex = ligReadIndex + 1;
if (ligReadIndex >= numReadings) {
ligReadIndex = 0;
}
ligAverage = ligTotal / numReadings;
Serial.println(light_percent);
Serial.println(ligAverage);
}
if (ligAverage > 25)
{
digitalWrite(dir1, HIGH);
digitalWrite(dir2, LOW);
analogWrite(speedPin, 225);
}else
{
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
analogWrite(speedPin, 0);
}
}
Next, I plan to use the sensor to turn another motor (the stepper motor, which I think is a better fit for turning the plant turntable).