NEED help with ir sensor

I am using an analogue sensor (sharp infrared) as my input.

my question is how can use the readings from the ir sensor to control a digital output without individually typing in an if() statement for each HIGH or LOW

In other words, lets say i had an LED as my digital output and i wanted the LED to blink slowly as an object is at its furthest distance from the ir sensor; and then the LED would blink faster and faster as the object moves closer to the ir sensor … what sort of code would i use for this situation

LEDs don't normally blink. They are either off or on. If you make the period between turning the LED on and turning it off (and back on again) a function of the sensor output, you will have accomplished your goal.

how about this??

int sensorPin = A0; int magPin = 13; int sensorValue = 0;

void setup() { pinMode(magPin, OUTPUT); }

void loop() { sensorValue = analogRead(sensorPin); digitalWrite(magPin, HIGH); delay(sensorValue); digitalWrite(magPin, LOW); delay(sensorValue); }

A0 is an alias for when an analog pin is used as a digital pin. Since that is not what you are doing, A0 is the wrong value to be using for the analog pin number. Other than that, yeah, it looks fine.

In other words, lets say i had an LED as my digital output and i wanted the LED to blink slowly as an object is at its furthest distance from the ir sensor; and then the LED would blink faster and faster as the object moves closer to the ir sensor … what sort of code would i use for this situation

it can be done

void loop()
analogRead(ir);
digitalWrite (led,HIGH);
[glow]dealy( 1000-analog valueof ir sensor);[/glow]digitalWrite(led,LOW); 
}

that delay to make it blink faster as object approaches
Note this was just an example three are more (possibly even better) methods of doing this :wink: