Sunday, May 16, 2010

More Arduino Reading Optical

Here's a code snippet for you, all dirty and messy. It will be cleaned up prior to inclusion in the class. Currently working on including the TSL230R Light-to-Frequency Converter with code shamelessly borrowed from:
http://roamingdrone.wordpress.com/2008/11/13/arduino-and-the-taos-tsl230r-light-sensor-getting-started/

I will probably use the code as-is. It handles this device as an interrupt process, and handling interrupts was an intention of the class.

I have this mystery phototransistor from Tanners. No datasheed can be found by several people working on int and Tanners sells the item "No data". It looks like a TO-18 case with a glass top, so I will assume that the connections are the same. It has a base pin, it would seem, which I thought was odd as I thought that that base was controlled by light. I have several, so I will experiment with a few, but I'm not sure how I will know if I have destroyed one or not. I think I may skip this one for another day and leave it out of the class.

----- Code 4 You  -------

/*
IR1
*

detects moving infrared with PIR. Upon sensing, checks light level with Light Dependent Resistor (Cadmium photocell) and then uses infrared rangefinding to determine range of object.

*/


int IRrangePin = 0; // select the input pin for the potentiometer
int inPin = 1; // select pin for IR detector (analog)
int LRpin = 2; //select analog pin for LDR
int ledPin1 = 13; // select the pin for the LED
int ledPin2 = 12; // second led
int ledPin3 = 11; // third led
int val = 0; // variable to store the value coming from the sensor
int dval = 0; // variable to store valud from IR detect
int Lval = 0; // Variable for use with LDR
int NewVal = 0; // Work variable



void setup() {
Serial.begin(9600);
Serial.println("Setup. ");
pinMode(ledPin1, OUTPUT); // declare the ledPin1 as an OUTPUT
pinMode(ledPin2, OUTPUT); // declare the ledPin2 as an OUTPUT
pinMode(ledPin3, OUTPUT); // declare the ledPin3 as an OUTPUT
pinMode(inPin, INPUT); // declare the Infrared detect pin as an INPUT
pinMode(LRpin, INPUT); // declare teh LDR pin as an INPUT
digitalWrite(ledPin3, LOW); // full on
}

void loop() {
dval=LOW;
detect();
val = analogRead(IRrangePin); // read the value from the sensor

/*
Serial.print("Proximity: "); Serial.println(val);
digitalWrite(ledPin1, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin1, LOW); // turn the ledPin off
*/
delay(val); // stop the program for some time
}


void detect()
{
delay(100);
dval = analogRead(inPin); // read input from IR detector
//Serial.print(dval);
if (dval>0)
{
Serial.println();
Serial.print(" Detection >YES. ");
digitalWrite(ledPin2, HIGH); // turn the ledPin on
LDR();
findRange();
}

if (dval<=0)
{
Serial.print(".");
digitalWrite(ledPin2, LOW); // turn the ledPin off
}
delay(200);
}


void LDR(){
Lval = analogRead(LRpin);
Serial.print(" LDR: "); Serial.print(Lval); Serial.print(" ");
analogWrite(ledPin3,(75+Lval));
delay(100);
}


void findRange(){
val = analogRead(IRrangePin); // read the value from the sensor
NewVal = val/100;
Serial.print("Proximity: "); Serial.print(val);

switch (NewVal) {

case 0:
//far away
Serial.print(" Too far. ");
break;
// break is optional

case 1:
//do something when var == 1
Serial.print(" Zone 1. ");
break;

case 2:
//do something when var == 2
Serial.print(" Zone 2. ");
break;

case 3:
//do something when var == 3
Serial.print(" Zone 3. ");
break;

case 4:
//do something when var == 4
Serial.print(" Zone 4. ");
break;

case 5:
//do something when var == 5
Serial.print(" Zone 5. ");
break;

case 6:
//do something when var == 6
Serial.print(" Zone 6. ");
break;

case 7:
//do something when var == 7
Serial.print(" Zone 7. ");
break;

default:
// if nothing else matches, do the default
// default is optional
Serial.print(" Unknown. ");
}

Serial.println();
digitalWrite(ledPin1, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin1, LOW); // turn the ledPin off
delay(val); // stop the program for some time

}