Pages: [1]   Go Down
Author Topic: Sony IR Remote Library  (Read 1588 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi I'm new to the arduino ( got it yesterday smiley ) 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:
Quote
/*
 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;
}


Header file:
Quote
/*
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:
Quote
/*
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
  • > 1000) {
       _data
  • = 1;
     }
      else {
        _data
  • = 0;
     }
    }
    
  _result = 0;
  _seed = 1;
  for(int i=0;i<11;i++) {
    if(_data == 1) {
      _result += _seed;
    }
    _seed = _seed * 2;
  }
  return _result;
}

New Code using created library:
Quote
#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:
Quote
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.
« Last Edit: March 07, 2010, 03:24:15 am by KWaterston » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Curtis,

Use the instance of class in your skecth,

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 .
Logged

Pages: [1]   Go Up
Jump to: