ir sensor and led question

Hello People,

I’m working ona project here that invovles an IR sensor and LEDs.
Here’s how it’s supposed to work.
i wave my hand over a IR sensor
An LED will fade on with PWM.
It will hold maximum brightness for about 10 seconds.
Then it will fade off.

Below is my code. This is the problem:

When I try it the led only fades on while my hand is over the sensor. I tried a few variations and got it to the point where it won’t stop fading on and off.

(I have to credit the getfeedback from the sensor portion of this code to Mr. Joel Murphy.)

int sensor = 0;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
boolean onoroff = false; // if this is true fade leds one time

void setup() {
// initialize serial communication:
pinMode(9, OUTPUT);
Serial.begin(9600);
}

void loop() {
sensor = getFeedback(); // getFeedback is joels smoothing code
Serial.println(sensor);
delay(100);
if (sensor > 120) { // sensor reading higher than 120 when object is blocking sensor
onoroff = true; // change my boolean state
}
if (onoroff == true){ //if boolean state is true run this fade code
analogWrite(9, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
if (brightness == 0) // if fade has run through completely once then reset my boolean?
onoroff = false;
}
}

int getFeedback(){ // feedback reading from sensor
int j;
int mean;
int result;
int test;
int reading[20];
int distance;
int resultinches;
boolean done;

for (j=0; j<20; j++){
reading[j] = analogRead(0); //get raw data from servo potentiometer
delay(10);
} // sort the readings low to high in array
done = false; // clear sorting flag
while(done != true){ // simple swap sort, sorts numbers from lowest to highest
done = true;
for (j=0; j<20; j++){
if (reading[j] > reading[j + 1]){ // sorting numbers here
test = reading[j + 1];
reading [j+1] = reading[j] ;
reading[j] = test;
done = false;
}
}
}
mean = 0;
for (int k=6; k<14; k++){ //discard the 6 highest and 6 lowest readings
mean += reading[k];
// float finalreading = mean5/1024;
//float distance = 27
pow(finalreading, -1.10);
}
result = mean/8; //average useful readings
resultinches = result/2.54;
return(resultinches);

}// end getFeedback()dback()

help??

You could do it by having three different modes, fade up, fade down, and off. Switch each one off and activate the next one after each one is completed.

Does your hand actually emit infrared light?

lol, no my hand blocks an infrared light. but that would be cool if it did :)

figured it out. thanks to everyone for helping

correct code:

int sensor = 0;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
boolean onoroff = false; // if this is true fade leds one time

void setup() {
// initialize serial communication:
pinMode(9, OUTPUT);
Serial.begin(9600);
}

void loop() {
sensor = getFeedback(); // getFeedback is joels smoothing code
Serial.println(sensor);
delay(100);
if (sensor > 120) { // sensor reading higher than 120 when object is blocking sensor
onoroff = true; // change my boolean state
}
if (onoroff == true){ fadeLights(); }
onoroff=false;
}
void fadeLights() {
int fadeSpeed = 1; // lower = faster
for(int i=0; i < 255; i++) {
analogWrite(9, i);
delay(fadeSpeed);
}

delay(10000);

for(int i=255; i > 0; i–) {
analogWrite(9, i);
delay(fadeSpeed);

}
}

int getFeedback(){
int j;
int mean;
int result;
int test;
int reading[20];
int distance;
int resultinches;
boolean done;

for (j=0; j<20; j++){
reading[j] = analogRead(0); //get raw data from servo potentiometer
delay(10);
} // sort the readings low to high in array
done = false; // clear sorting flag
while(done != true){ // simple swap sort, sorts numbers from lowest to highest
done = true;
for (j=0; j<20; j++){
if (reading[j] > reading[j + 1]){ // sorting numbers here
test = reading[j + 1];
reading [j+1] = reading[j] ;
reading[j] = test;
done = false;
}
}
}
mean = 0;
for (int k=6; k<14; k++){ //discard the 6 highest and 6 lowest readings
mean += reading[k];
// float finalreading = mean5/1024;
//float distance = 27
pow(finalreading, -1.10);
}
result = mean/8; //average useful readings
resultinches = result/2.54;
return(resultinches);

}// end getFeedback()dback()