Go Down

Topic: Getting error in code for ir sensor (Read 605 times) previous topic - next topic

qwerty156

I wanted to control a led from my Sony remote using an arduino with a TSOP1738 Ir sensor. I tried many codes but the only one that worked reliably for me was this one :
Code: [Select]
// --------------------------------------
// SIRCS (Sony IR Control System) Monitor
//
// Jon McPhalen
// www.jonmcphalen.com
// 30 NOV 2007
// --------------------------------------

int irPin = 7;

void setup()
{
  pinMode(13, OUTPUT);     
  pinMode(irPin, INPUT);
  Serial.begin(9600);
  delay(25);
  Serial.println("SIRCS Monitor\n");
}

void loop()
{
  int key;

  key = getSircs();
  Serial.println(key, BIN);
  delay(250);
}

int getSircs() {
  int duration;
  int irCode;
  int mask;

  // wait for start bit
  do {
    duration = pulseIn(irPin, LOW);
  } while (duration < 2160 || duration > 2640);

  // get 12-bit SIRCS code
  irCode = 0;   // clear ir code
  mask = 1;     // set mask to bit 0
  for (int idx = 0; idx < 12; idx++) { // get all 12 bits
    duration = pulseIn(irPin, LOW);     // measure the bit pulse
    if (duration >= 1080)   // 1 bit?
irCode |= mask; // yes, update ir code
    mask <<= 1; // shift mask to next bit
  }
  return irCode;

}



This code gave an output on the serial monitor depending one the button pressed.
I wanted it to toggle an LED on and off so i added the following code at the end, in between "return irCode;"
and "}"
Code: [Select]

}
{ if (key == 10010010) {
    digitalWrite(13, HIGH);}
  if (key == 10010011) {
    digitalWrite(13, LOW);}

Problem is that my code doesn't compile- i get the following error-
"sketch_aug25a:50: error: expected unqualified-id before '{' token"


Could anyone help me out here?

PaulS

Code after the return statement would never be executed, anyway.

Post all of your code, not just the snippet you added. You've most likely added to many { or }.

Noorman

Please post the full code, and the line where it goes wrong.

What i saw in the code is that you are evaluating a binary value and the variable is an int.
That can cause serious trouble.
2B || !2B

... bonding electrons and bits!

laserjockey

You've put your code in the wrong spot, and added too many brackets.  Also, you can't compare binary values as you've typed them in.  Go back to the beginning code (erase your changes, or reload the original file) and put your code in the loop() function, like so:

void loop()
{
  int key;

  key = getSircs();
  Serial.println(key, BIN);
  delay(250);

  if (key == 146) {
      digitalWrite(13, HIGH);
  } else if (key == 147) {
      digitalWrite(13, LOW);
  }
}

Where did I get 146 and 147?  Those are the decimal equivalents to 10010010 and 10010011 in binary.  BTW, the Windows calculator has the ability to change between bases (binary, decimal, hexadecimal) if you change it to programmer view.  There's a way to express binary values in C, I just can't remember what it is.

Good luck,
LJ

PaulS

Quote
There's a way to express binary values in C, I just can't remember what it is.

Prefix the value with 0b, as in 0b10010010 or 0b10010011.

dominikh

Strictly speaking there's no way in C. The 0b prefix is, unfortunately, an unofficial extension by most compilers. But yeah, GCC does support it, and I don't see Arduino ever moving away from GCC :)

Go Up