Pages: [1]   Go Down
Author Topic: Getting error in code for ir sensor  (Read 491 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// --------------------------------------
// 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:
}
 { 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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Have you thought it over before asking?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

2B || !2B

... bonding electrons and bits!

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Germany
Offline Offline
Newbie
*
Karma: 1
Posts: 34
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: