TSOP4038 / TSAL7400 emitter detector pair

http://www.vishay.com/docs/81014/tsal7400.pdf

right now i have the tsop4038 and a ir emitter similar to a tsal7400 set up with my Arduino Nano, however i do not think that the values i am receiving are correct. i believe it is an error with my code, i found this code from the arduino playground website:

/*---------------------------
IR LED and phototransistor beam test and calibration

debug stuff:
min/max val = whatever the value is when it changes
LED is always on
----------------------------*/
// defines for pins
#define IRoutputPin 2 // digital output pin
#define IRinputAnalogPin 0 // analog input pin
#define LEDsignalPin 13 // LED signal pin

// int and long values
int val = 0; // variable to store the value read from input pin
int minVal = 1000;
int maxVal = 0;
int oldVal= 0;

// strings
String outputString;
String intro = "JTD IR test and calibration init";
boolean needIntro = true;
String signalBase = "Signal Strength: ";
String minBase = "Min: ";
String maxBase = "Max: ";

void setup()
{
pinMode(IRoutputPin,OUTPUT);
digitalWrite(IRoutputPin,HIGH);
pinMode(LEDsignalPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// print header
if (needIntro == true)
{Serial.println(intro);
needIntro = false;}
digitalWrite(LEDsignalPin, HIGH); // turn signal LED on

// read value of analog input
val = analogRead(IRinputAnalogPin); // read the input pin
//Serial.println(val); // debug value

if (val != oldVal) // if the new value is different than the old value, then do stuff
{

String outputString = signalBase + val; // concatenates output for signal strength
Serial.println(outputString);
outputString = minBase + minVal; // concatenates output for min
Serial.println(outputString);
outputString = maxBase + maxVal; // concatenates output for max
Serial.println(outputString);
oldVal = val;

delay(50); // wait half a second
}

if (val < minVal) // conditional if val is less than prior minVal
{ //String outputString = minBase + val; // concatenates output
Serial.println(outputString);
minVal = val;}

if (val > maxVal) // conditional if val is greater than prior maxVal
{ String outputString = maxBase + val; // concatenates output
//Serial.println(outputString);
maxVal = max(maxVal, val);}
}

I am aware that you are supposed to assign a square wave signal emitting at 38.5 kHz to the emitter, but how would i do this with the code? and i'm not even sure if i am reading values from the sensor correctly.

could someone pls help steer me in the right direction, i have a critical design review for my micromouse and i'm really running short on time.

thank you
ejay

i also tried this code as well, but i'm not even sure if the ir led is giving off an ir light.

//define pins. I used pins 4 and 5
#define irLedPin 4 // IR Led on this pin
#define irSensorPin 5 // IR sensor on this pin

int irRead(int readPin, int triggerPin); //function prototype

void setup()
{
pinMode(irSensorPin, INPUT);
pinMode(irLedPin, OUTPUT);
Serial.begin(9600);
// prints title with ending line break
Serial.println("Program Starting");
// wait for the long string to be sent
delay(100);
}

void loop()
{
Serial.println(irRead(irSensorPin, irLedPin)); //display the results
delay(10); //wait for the string to be sent
}

/******************************************************************************

  • This function can be used with a panasonic pna4602m ir sensor
  • it returns a zero if something is detected by the sensor, and a 1 otherwise
  • The function bit bangs a 38.5khZ waveform to an IR led connected to the
  • triggerPin for 1 millisecond, and then reads the IR sensor pin to see if
  • the reflected IR has been detected
    ******************************************************************************/
    int irRead(int readPin, int triggerPin)
    {
    int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
    int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
    int i;
    for (i=0; i <=cycles; i++)
    {
    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPin, LOW);
    delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead
    }
    return digitalRead(readPin);
    }