Sony IR Remote Library

Hi I’m new to the arduino ( got it yesterday :slight_smile: ) and after blinking a LED and playing with some CdS cells and a servo I got the arduino reading a sony remote. The code worked great, it was giving me the codes when i pressed buttons on my remote.

Now I wanted to try and learn how to make my own libraries so I thought I would turn this one into a library using the tutorial on the arduino website, problem is I’m stuck. Any help would be appreciated as I really want to know what I’m doing wrong.

First working code:

/*
Program that reads key presses of a sony remote control
Created by Kurtis Waterston, March 6 2010
Realesed into the public domain
*/

int irDet = 2;
int key = 0;
int data[12];
int state;

void setup() {
Serial.begin(9600); //For debugging
pinMode(irDet, INPUT); //IR detector connected to digital pin 2
}

void loop() { //Main loop
if (pulseIn(irDet, LOW) > 2200) { //Check for start pulse
getIRkey();
Serial.print("Key press is = ");
Serial.println(key);
}

}

int getIRkey() { //Read pulses
data[0] = pulseIn(irDet, LOW);
data[1] = pulseIn(irDet, LOW);
data[2] = pulseIn(irDet, LOW);
data[3] = pulseIn(irDet, LOW);
data[4] = pulseIn(irDet, LOW);
data[5] = pulseIn(irDet, LOW);
data[6] = pulseIn(irDet, LOW);
data[7] = pulseIn(irDet, LOW);
data[8] = pulseIn(irDet, LOW);
data[9] = pulseIn(irDet, LOW);
data[10] = pulseIn(irDet, LOW);
data[11] = pulseIn(irDet, LOW);

for(int x = 0; x <= 11; x++) { //Decide wether pulses are 1’s or 0’s
if(data > 1000) {
data = 1;
}
else {
data = 0;
}
}

int result = 0; //Convert array into interger
int seed = 1;
for(int i=0;i<11;i++) {
if(data == 1) {

  • result += seed;*
  • }*
    _ seed = seed * 2;_
  • }*
  • key = result;*
  • return result;*
    }
    [/quote]
    Header file:
    > /*
    > IrRemote.h - A library for Sony tv remote control
    > Created by Kurtis Waterston, March 6 2010
    > Realeased into the public domain
    > */
    >
    > #ifndef IrRemote_h
    > #define IrRemote_h
    >
    > #include “WProgram.h”
    >
    > class IrRemote
    > {
    > public:
    > IrRemote(int pin);
    > int IrReady();
    > int IrKey();
    > private:
    > int _pin;
    > int _data[12];
    > int _seed;
    > int _result;
    > };
    >
    > #endif
    CPP file:
    > /*
    > IrRemote.cpp - A library for sony tv remote control
    > Created by Kurtis Waterston, March 6 2010
    > Released into the public domain
    > */
    >
    > #include “WProgram.h”
    > #include “IrRemote.h”
    >
    > IrRemote::IrRemote(int pin)
    > {
    > pinMode(pin, INPUT);
    > _pin = pin;
    > }
    >
    > int IrRemote::IrReady()
    > {
    > if(pulseIn(_pin, LOW) > 2200) {
    > return 1;
    > }
    > else {
    > return 0;
    > }
    > }
    >
    > int IrRemote::IrKey()
    > {
    > _data[0] = pulseIn(_pin, LOW);
    > _data[1] = pulseIn(_pin, LOW);
    > _data[2] = pulseIn(_pin, LOW);
    > _data[3] = pulseIn(_pin, LOW);
    > _data[4] = pulseIn(_pin, LOW);
    > _data[5] = pulseIn(_pin, LOW);
    > _data[6] = pulseIn(_pin, LOW);
    > _data[7] = pulseIn(_pin, LOW);
    > _data[8] = pulseIn(_pin, LOW);
    > _data[9] = pulseIn(_pin, LOW);
    > _data[10] = pulseIn(_pin, LOW);
    > _data[11] = pulseIn(_pin, LOW);
    > *
    > for(int x = 0; x <= 11; x++) {
    > if(_data[x] > 1000) {
    > _data[x] = 1;
    > }
    > else {
    > _data[x] = 0;
    > }
    > }
    > *
    > _result = 0;
    > _seed = 1;
    > for(int i=0;i<11;i++) {
    > if(_data == 1) {
    > * _result += _seed;

    _> * }
    _
    > _seed = _seed * 2;
    > * }*
    > * return _result;*
    > }
    > [/quote]
    > New Code using created library:
    > > #include <IrRemote.h>
    > >
    > > int key;
    > > int State = 0;
    > > IrRemote IrDet(2);
    > >
    > > void setup() {
    > > Serial.begin(9600);
    > > }
    > >
    > > void loop() {
    > > State = IrRemote.IrReady();
    > > key = IrRemote.IrKey();
    > > Serial.print("Remote Key = ");
    > > Serial.println(key);
    > > }
    > The problem is when I try and verify the new code I get this error:
    > > In function ‘void loop()’:
    > > error: expected primary-expression before ‘.’ token
    > Again any help would be appreciated as I really want to learn how to work with this awesome tool and create more libraries. Thanks in advance.

Hi Curtis,

Use the instance of class in your skecth,

void loop() {
 State = IrDet.IrReady();
 key = IrDet.IrKey();
 Serial.print("Remote Key = ");
 Serial.println(key);
}

Thanks a lot Nadir I got it working now, although its still probably not the best way to do things, but that's why I'm still learning ;D .