I happen to have a motor laying around and wanted to try some work with the switch case variables. I have it working with the sensor, it reads brighest and all that. Really just loaded the example and added a pin for the motor and a analog write line for pin 1.
I know the motor works but doesn't spin when on that case. The serial monitor prints the line though. Code and layout are below. The analogwrite line has been set to other value, like 1023, tried digitalwrite at HIGH too.
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 600; // sensor maximum, discovered through experiment
int motorPin = 1;
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
}
void loop() {
pinMode(motorPin, OUTPUT);
// 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
analogWrite(motorPin, sensorMax);
Serial.println("bright");
break;
}
delay(1); // delay in between reads for stability
}