|
| 1 | +#define irLedPin 4 // IR Led on this pin |
| 2 | +#define irSensorPin 5 // IR sensor on this pin |
| 3 | + |
| 4 | + |
| 5 | +void setup() |
| 6 | +{ |
| 7 | + pinMode(irSensorPin, INPUT); |
| 8 | + pinMode(irLedPin, OUTPUT); |
| 9 | + Serial.begin(9600); |
| 10 | + // prints title with ending line break |
| 11 | + Serial.println("Program Starting"); |
| 12 | + // wait for the long string to be sent |
| 13 | + delay(100); |
| 14 | +} |
| 15 | + |
| 16 | +void loop() |
| 17 | +{ |
| 18 | + Serial.println(irRead(irSensorPin, irLedPin)); //display the results |
| 19 | + delay(1); //wait for the string to be sent |
| 20 | +} |
| 21 | + |
| 22 | +/****************************************************************************** |
| 23 | + * This function returns a zero if something is detected by the sensor, and a 1 otherwise |
| 24 | + * The function bit bangs a 38.5khZ waveform to an IR led connected to the |
| 25 | + * triggerPin for 1 millisecond, and then reads the IR sensor pin to see if |
| 26 | + * the reflected IR has been detected |
| 27 | + * also sends back the analog data read by the sensor |
| 28 | + ******************************************************************************/ |
| 29 | +int irRead(int readPin, int triggerPin) |
| 30 | +{ |
| 31 | + int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds |
| 32 | + int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond |
| 33 | + int i; |
| 34 | + for (i=0; i <=cycles; i++) |
| 35 | + { |
| 36 | + digitalWrite(triggerPin, HIGH); |
| 37 | + delayMicroseconds(halfPeriod); |
| 38 | + digitalWrite(triggerPin, LOW); |
| 39 | + delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead |
| 40 | + } |
| 41 | + return digitalRead(readPin); |
| 42 | + return analogRead(readPin); |
| 43 | +} |
0 commit comments