10 May 2010

Task 45 - Infra-Red (IR) Send and Receive Software

Well this was the software for Task 45..
..would've been nice if it'd all worked!

/*
* Project: Task 45_IRxMitRecv
* Author: Jane-Maree Howard
* Date: Friday 30/04/2010
* Platform: Arduino 18
* Purpose: To demonstrate variable blink communication
between an infra-red(IR)-LED & an IR-receiver diode.
* Operation: Description:
Connect the IR-receiver diode's anode to +5V,
& its cathode to Analog 0.
Connect the IR-LED's anode to pin 12,
& its cathode through a resistor to ground.
Connect a LED from pin 9 through a resistor to ground.
Declare: 4 pin definition variables, pin-state(HIGH/LOW) vble,
delay vble, analog reading vble, & output brightness vble.
Setup(): output pins (2 off) & the IR input pin;
Loop(): perform various IR-LED transmit/receive operations.
*/

byte obLedPin = 13; // on-board LED as indicator
byte irLedPin = 12; // infra-red LED connected to digital pin 12
byte LEDpin = 9; // connect a LED to pin 09(PWM pin) - output
byte recLedPin = 0; // receiving LED connected to analog pin 0 (a0)
int dLay = 1000; // delay variable
int recLedVal; // the analog reading from the IR-sensor
byte LEDbrightness; // pin 09 output LED brightness
byte pinState; // pins 12 & 13 HIGH/LOW state

void setup()
{
pinMode(obLedPin, OUTPUT); // digital pin 13 as output:
pinMode(irLedPin, OUTPUT); // digital pin 12 as output:
digitalWrite(obLedPin, HIGH); // turn on-board LED on
delay(dLay*4); // wait 4 seconds
Serial.begin(9600); // set up Serial Monitor
}//end setup()

void loop()
{
// transmit
pinState = 1; // pins 12 & 13 HIGH state
irSendReceive(pinState); // IR-LED & ob-LED ON

// don't transmit
pinState = 0; // pins 12 & 13 LOW state
irSendReceive(pinState); // IR-LED & ob-LED OFF
}//end loop()

// IR Send-Receive routine
void irSendReceive(byte pState)
{
digitalWrite(obLedPin, pState); // turn on-board LED on/off
digitalWrite(irLedPin, pState); // turn IR-LED on/off
recLedVal = analogRead(recLedPin); // read IR receiver diode &..
irResultOut(recLedVal); // ..send result to Serial Monitor & brightness LED
delay(dLay); // wait 1 second
}//irSendReceive()

// Serial Monitor print routine
void irResultOut(int pVal)
{
//now we have to map 0-1023 to 0-255
//since that's the range analogWrite() uses
LEDbrightness = map(pVal, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);
Serial.print("IR value is ");
Serial.print(pVal);
if (pVal > 770)
{
Serial.println(" - HIGH");
}//if()
else
{
Serial.println(" - ***LOW***");
}//else..
}//irResultOut()

No comments:

Post a Comment