int potPin = A3; // Potentiometer output connected to analog pin 3
int potVal = 0;
int redPin = 9; // Red LED,
int grnPin = 10; // Green LED,
int bluPin = 11; // Blue LED,
int redVal = 0;
int grnVal = 0;
int bluVal = 0;
const int sensorMin = 0;
const int sensorMax = 600;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
void loop() {
// read the sensor:
potVal = analogRead(potPin);
// map the sensor range to a range of four options:
int range = map(potVal, sensorMin, sensorMax, 0, 3);
// do something different depending on the range value:
switch (range) {
case 0: // your hand is on the sensor
digitalWrite(redPin, HIGH);
digitalWrite(grnPin, LOW);
digitalWrite(bluPin, LOW);
break;
case 1: // your hand is close to the sensor
digitalWrite(redPin,LOW );
digitalWrite(grnPin, HIGH);
digitalWrite(bluPin, LOW);
break;
case 2: // your hand is a few inches from the sensor
digitalWrite(redPin,LOW );
digitalWrite(grnPin,LOW );
digitalWrite(bluPin, HIGH);
break;
case 3: // your hand is nowhere near the sensor
digitalWrite(redPin, HIGH);
digitalWrite(grnPin, HIGH);
digitalWrite(bluPin, HIGH);
break;
}
delay(1); // delay in between reads for stability
}