Hi everyone!
I got the arduino about an hour and a half ago, and after playing with an LED and an IR sensor I came up with this simple program.
It's not very exciting, but it might help anyone wanting to experiment with the sensors. Im using a Sharp GP2Y0A21YK from sk-pang.co.uk.
The script will basically turn the LED on pin 13 on or off when you wave your hand in front of it. The int time is used to make sure that it doesnt constantly flash on or off if you hold your hand or another object there, it should wait for about 1100 miliseconds.
Im hoping to eventually use this as part of a home lighting system
// Written by Chris17
// Date: 8th May 2009
int sensorPin=0; // The signal pin for the sensor
int sensorVal=0; // The value that the sensor returns
int time; // The time since the last switch
int active = 0; // Whether the light is active
int ledPin = 13; // The pin the LED is connected to
void setup() {
pinMode( ledPin, OUTPUT ); // Sets the led pin to an output mode
}
void loop() {
sensorVal = analogRead(sensorPin); // Reads the signal from the sensor, returning a number. The number is higher the closer an object gets.
if( sensorVal > 250 ) // If an object is fairly close (around 25 centemetres or closer for me)
{
if( time > 1100 ) // If it has been more than 1100 miliseconds since the light last switched (ensures it doesnt blink randomly if it detects your hand twice)
{
time = 0; // Set the last time switched back to 0
if( active == 0 ) // If the LED isn't already on
{
active = 1; // Now it is!
digitalWrite( ledPin, HIGH ); // Turn on the power to the LED pin
}
else // Otherwise...
{
active = 0; // Now it isn't
digitalWrite( ledPin, LOW ); // Turn off the power to the LED pin
}
}
}
time += 10; // Adds 10 miliseconds to the time since it was last switched
delay(10); // Waits for 10 miliseconds
}