sharp ir switch

hi everyone,after reading allmost all post around here and google,hope 2 get my answers here.I found hundred of examples of Ir conected to Atmegas that light up a Led when get close to object,but not even one example on how to make a toogle switch.Im not the only one looking for codes like this,truth is this subject is avoided,anyway…I wanna do a simple toogle switch,with the arduino Duemilanove and the Sharp 2Y0A21 ir sensor,that can light a Led.So far i can only make it like momentary switch:
+LED on pin 13
+DATA from ir on pin analog 0

int led1 = 13;
int sensor = A0;

void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
}

void loop() {
// we read the sensor Value
int Value = analogRead(A0);
// we maps the value
int sensorValue = map(Value, 0, 1024, 0 , 200);
// turn on the led1 when the value is beetween 50 and 100
if ( sensorValue > 50 && sensorValue <300){
digitalWrite(13, HIGH);

}

else {
// turn off the two leds
digitalWrite(13, LOW);

}
Serial.println(sensorValue);
delay(100);
}

const int led1 = 13;
const int sensor = A0;
 
void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);     
}
 
void loop() {
  static boolean lastSensorHit = false;
  static boolean LEDvalue = LOW;

  int sensorValue = map(analogRead(sensor), 0, 1024, 0 , 200);
  bool sensorHit = sensorValue > 50 && sensorValue <300;

  if (sensorHit && !lastSensorHit)  // Now true, was false
    {
    LEDvalue = !LEDvalue; // Toggle the LED
    digitalWrite(led1, LEDvalue);
    } 
  lastSensorHit = sensorHit;
  delay(100);
}

work like charm,no debounce and very stable,thank you so much johnwasser,i realy apreciate your help :smiley:

I make program using analog sound sensor and DHT22 humidity and temperature sensor. When I see serial monitor, it print sensor value so slowly. In this case the analog sound sensor become not responsive. When I use only analog sound sensor (without DHT22), serial monitor printed analog sensor value so fast. Is there a problem with DHT22 program?

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE);

void setup() {

Serial.begin(9600);
Serial.println("DHTxx test!");

dht.begin();
}

void loop() {

float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.println("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}
int sensorValue = analogRead(A0);//use A0 to read the electrical signal
Serial.println(sensorValue);
}

Hello all i want to do that Arduino IR swich lighting a led, but i dont know how can you help me please.
I see lots of ppl using a 32khz...etc IR leds resivers, im not so good in electronics but in that case i want to use
a "" 5mm 940nm IR infrared Receiving LED Lamp "" not sure how its works but as i see from the name is the same
led IR receiver like the remote_tv. one i took few of that ones http://www.ebay.com/itm/100Pcs-5mm-940nm-IR-infrared-Receiving-LED-Lamp-/140910365482?refid=store and i want to light a 5mm blue led, and yes for that i will need a 5mm IR transmiter too.
I have all that pices but i dont know to combine them all to work.
I readed lots of forums and tested lost of codes but non of them worked for me.

Please help me i need to build that god dam IR led swich :smiley:

Domino60