SACC

/ / navigation

Light Detector
Duino Tag

LED as input



First

Solder together and Ardweeny





You can use an LED to detect light. For best results, use a colored LED (such as red) and a normal white flashlight (the brighter the better).

Connect the anode into the 0 Analog In pin and connect the cathode to Ground.

Here is code to show you that the LED is detecting light:
/* LED Light Sensor 
* by Duane O'Brien 
* for IBM Developer Works 
*/ 
int recvPin = 0; 
int wait = 1000; 
int val = 0; 
void setup() { 
	// Initialize the Serial Interface 
	Serial.begin(9600); 
} 
void loop() { 
	// Take a reading from the Analog Pin 
	val = analogRead(recvPin); 
	// Output the detected value 
	Serial.print("DETECT : "); 
	Serial.println(val); 
	// Wait to take the next reading. 
	delay(wait); 
} 
Shinning a flashlight on the LED that is in the dark and is in the light will give you very different readings. This demonstrates the difficulty of ambient light.

Now you are going to modify the code to learn a little bit about frequency.

Infrared is everywhere, so sensors need to filter out anything not being transmitted at a specific frequency. The code below will show you, in a rudimentary way, how transmitting frequency works. This isn't intended to be a comprehensive scientific experiment, but it may help illustrate the concept.

You are going to change the code so that when the light reading jumps by more than 5 percent of the baseline, a cycle is started that looks at the next two readings, outputting a specific code based on the codes detected. You'll need some additional variables:
  • a variable to determine how many readings should be taken to establish the baseline number for ambient light
  • a variable to hold the minimum reading jump you are expecting
  • an iterator
  • an array to hold the different codes
  • and a tracking int to tell where you are in a cycle.
int recvPin = 0; 
int wait = 1000; 
int val = 0; 
int readings = 5; 
int jump = 0; 
int i = 0; 
int code[2]; 
int incycle = 0; 


void setup() { 
	/*Next, you need to take a set of readings during setup, average them, and determine 
	what 5 percent means. Generally, the first reading is always abnormally high, 
	so  throw that one out right away. */
	// Initialize the Serial Interface 
	Serial.begin(9600); 
	Serial.print("establishing baseline... "); 
	val = analogRead(recvPin); // throw out the first one, it's usually high. 
	delay(wait); 
	for (i = 0; i < readings;i++) { 
	     jump += analogRead(recvPin); 
	     delay(wait); 
	} 
	jump = (jump / readings)*1.05; 
	Serial.println(jump); 
	// Output the detected value 
	delay(wait); 
} 

void loop() { 
	/*you read the pin. If you're in a cycle, you need to save the 
	code, and if you have received all the code, output a result. If you're not in a cycle, 
	and the reading is 5 percent above the baseline, you start a cycle. Listing 10 could 
	be more compact, but it's spelled out a little more clearly so you can see what's 
	going on. */
	// Take a reading from the Analog Pin 
	val = analogRead(recvPin); 
	switch (incycle) { 
	case 0: 
	if (val > jump) { 
	     Serial.println("In Cycle"); 
	     incycle = 1; 
	} else { 
	     Serial.println("Out Of Cycle"); 
	} 
	break; 
	
	case 1: 
	if (val > jump) { 
	     code[0] = 1; 
	} else { 
	     code[0] = 0; 
	} 
	incycle = 2; 
	Serial.println("Read One"); 
	break; 
	
	case 2: 
	if (val > jump) { 
	     code[1] = 1; 
	} else { 
	     code[1] = 0; 
	} 
	incycle = 3; 
	Serial.println("Read Two"); 
	break; 
	
	case 3: 
	if (code[0] == 0 && code[1] == 0) { 
	     Serial.println("Reset"); 
	} else if (code[0] == 1 && code[1] == 0) { 
	     Serial.println("Turn On"); 
	} else if (code[0] == 0 && code[1] == 1) { 
	     Serial.println("Turn Off"); 
	} else if (code[0] == 1 && code[1] == 1) { 
	     Serial.println("Explode"); 
	} 
	code[0] = 0; 
	code[1] = 0; 
	incycle = 0; 
	break; 
	}
	
	delay(wait); 
} 
Upload this code , you should see when the readings are being done. By shining a flashlight on the LED, you can trigger a cycle. The next two readings (one second apart) determine what final code is output. You can trigger the different outputs by alternately turning the flashlight on and off at a frequency of once per second. For example, if you just shine your flashlight on the LED for three seconds, you should trigger the "Explode" message. If you turn your flashlight on for one second, off for one second, and on for one second, you should trigger the "Turn Off" code.

What you're doing here is signaling a binary message at a very low frequency.


http://aterribleidea.com/2009/02/25/building-an-arduino-based-laser-tag-game/