Hi, the project I am working on is using a TSL230r to sense light at different frequencies, and then to tell the servo that I have set up to spin depending on the values that the sensor reads. The problem I am having ladies and gentlemen is figuring out how to insert the code for the light sensor into a switch command code I have set up. I am using the switch command simply because I think it will allow the servo to move how it wants to(or how I tell it to), under multiple conditions, or in my case, under multiple conditions of light.(i.e. between a sensor reading of 1200 to 1300, writeMicroseconds(1300) for 4 seconds, then if its between 1300 and 1400, writeMicroseconds(1300) for 5 seconds, etc.) I have attached the code and any help would be appreciated in this issue. Thanks.
int TSL230_Pin = 4; //TSL230 output
int TSL230_s0 = 3; //TSL230 sensitivity setting 1
int TSL230_s1 = 2; //TSL230 sensitivity setting 2
int TSL230_samples = 6; //higher = slower but more stable and accurate
void setup(){
Serial.begin(9600);
setupTSL230();
}
void loop(){
float lightLevel = readTSL230(TSL230_samples);
Serial.println(lightLevel);
}
void setupTSL230(){
pinMode(TSL230_s0, OUTPUT);
pinMode(TSL230_s1, OUTPUT);
//configure sensitivity - Can set to
//S1 LOW | S0 HIGH: low
//S1 HIGH | S0 LOW: med
//S1 HIGH | S0 HIGH: high
digitalWrite(TSL230_s1, LOW);
digitalWrite(TSL230_s0, HIGH);
}
float readTSL230(int samples){
//sample light, return reading in frequency
//higher number means brighter
float start = micros();
int readings = 0;
while(readings < samples){
pulseIn(TSL230_Pin, HIGH);
readings ++;
}
float length = micros() - start;
float freq = (1000000 / (length / samples)) * 10;
return freq;
}
// these constants won't change. They are the
// lowest and highest readings you get from your sensor:
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 600; // sensor maximum, discovered through experiment
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// do something different depending on the
// range value:
switch (range) {
case 0: // your hand is on the sensor
Serial.println("dark");
break;
case 1: // your hand is close to the sensor
Serial.println("dim");
break;
case 2: // your hand is a few inches from the sensor
Serial.println("medium");
break;
case 3: // your hand is nowhere near the sensor
Serial.println("bright");
break;
}
delay(1); // delay in between reads for stability
}