Pages: [1]   Go Down
Author Topic: Help on how to use the IRremote  (Read 1089 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all!
I am trying to make a project (for my son that loves photography) to rotate a camera a certain amount of degrees and triger it, the goal is to make 360 deg. photos.

To avoid a keyboard I am trying to use a IRremote command to interface with the machine.

Now, my problem is the following:
 I am using the IRremote library and try to use " (irrecv.decode(&results))" as function, because, I need also to work with the display and stepping motor, but I get a few errors. Can I use this as a function or I must include the remaining code after close the ' if ' bracket ' } '. The code is below:

Thanks in adbance for your help.
Manuel


Code:

#include <DogLcd.h>
// initialize the library with the numbers of the interface pins
DogLcd lcd(9, 10, 11, 12);
#include <IRremote.h>
int receiver = 8; // Conectar IR Receiver na porta digital 8
IRrecv irrecv(receiver); // Criar objeto 'irrecv'
decode_results results;



void setup()
{
  lcd.begin(DOG_LCD_M163,0x28);
  lcd.clear();
  delay(100);
 // Serial.begin(9600); // for serial monitor output
  irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
 for(;;)
{
 
IR_code();

}

IR_code()
  {
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    //lcd.print(results.value, DEC); // Imprimir na porta serial o valor do controle
    switch(results.value)
  {
    case 59: lcd.print(" Menu      "); break;
    case 2107: lcd.print(" Menu      "); break;
    case 32: lcd.print(" up      "); break;
    case 2080: lcd.print(" up      "); break;
    case 33: lcd.print(" down     "); break;
    case 2081: lcd.print(" down      "); break;
    case 16: lcd.print(" right     "); break;
    case 2064: lcd.print(" right      "); break;
    case 17: lcd.print(" left    "); break;
    case 2065: lcd.print("left    "); break;
    case 37: lcd.print(" OK/Enter    "); break;
    case 2085: lcd.print("OK/Enter    "); break;
    case 55: lcd.print(" Exit    "); break;
    case 2103: lcd.print(" Exit    "); break;       
    case 2049: lcd.print(" 1         "); break;
    case 1:  lcd.print(" 1         "); break;
    case 2: lcd.print(" 2    "); break;
    case 2050: lcd.print(" 2    "); break;     
    case 3: lcd.print(" 3    "); break;
    case 2051: lcd.print(" 3    "); break;     
    case 4: lcd.print(" 4    "); break;
    case 2052: lcd.print(" 4    "); break;     
    case 5: lcd.print(" 5    "); break;
    case 2053: lcd.print(" 5    "); break;
    case 6: lcd.print(" 6    "); break;
    case 2054: lcd.print(" 6    "); break;     
    case 7: lcd.print(" 7    "); break;
    case 2055: lcd.print(" 7    "); break;     
    case 8: lcd.print(" 8    "); break;
    case 2056: lcd.print(" 8    "); break;         
    case 9: lcd.print(" 9    "); break;
    case 2057: lcd.print(" 9   "); break;
    case 0: lcd.print(" zero    "); break;
    case 2048: lcd.print(" zero    "); break;         
 

    default: lcd.print(" other button   ");
  }
  delay(500);
    irrecv.resume(); // receive the next value
 }  // Your loop can do other things while waiting for an IR command
}
}

The error codes it gives are the following:
Code:
.cpp: In function 'void loop()':
:64: error: 'IR_code' was not declared in this scope
:65: error: 'IR_code' was not declared in this scope
:70: error: expected ';' before '{' token
:118 error: expected '}' at the end of input
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
 for(;;)
{
Blocks of code are ended with the opposing curly bracket '}'.

Code:
IR_code();

}
What are you trying to do here?
 
Code:
} // Closes the switch
  delay(500);
    irrecv.resume(); // receive the next value
 }  // Closes the if
} // Closes the function
} // Not supposed to be here.
See comments.
« Last Edit: May 09, 2012, 11:22:28 am by Arrch » Logged

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

Arrch,

Thanks for your comments, I understood them, but, I do not understand were we call the function to read the IRcode.

 
Code:
{
IR_code();
}

I tryied to make a function to read the code. Is this wrong?

thanks,
Manuel
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:

Code:
#include <DogLcd.h>
// initialize the library with the numbers of the interface pins
DogLcd lcd(9, 10, 11, 12);
#include <IRremote.h>
int receiver = 8; // Conectar IR Receiver na porta digital 8
IRrecv irrecv(receiver); // Criar objeto 'irrecv'
decode_results results;



void setup()
{
  lcd.begin(DOG_LCD_M163,0x28);
  lcd.clear();
  delay(100);
  // Serial.begin(9600); // for serial monitor output
  irrecv.enableIRIn(); // Start the receiver
}
void loop()
{

  IR_code();

}

void IR_code()
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    //lcd.print(results.value, DEC); // Imprimir na porta serial o valor do controle
    switch(results.value)
    {
    case 59:
      lcd.print(" Menu      ");
      break;
    case 2107:
      lcd.print(" Menu      ");
      break;
    case 32:
      lcd.print(" up      ");
      break;
    case 2080:
      lcd.print(" up      ");
      break;
    case 33:
      lcd.print(" down     ");
      break;
    case 2081:
      lcd.print(" down      ");
      break;
    case 16:
      lcd.print(" right     ");
      break;
    case 2064:
      lcd.print(" right      ");
      break;
    case 17:
      lcd.print(" left    ");
      break;
    case 2065:
      lcd.print("left    ");
      break;
    case 37:
      lcd.print(" OK/Enter    ");
      break;
    case 2085:
      lcd.print("OK/Enter    ");
      break;
    case 55:
      lcd.print(" Exit    ");
      break;
    case 2103:
      lcd.print(" Exit    ");
      break;       
    case 2049:
      lcd.print(" 1         ");
      break;
    case 1: 
      lcd.print(" 1         ");
      break;
    case 2:
      lcd.print(" 2    ");
      break;
    case 2050:
      lcd.print(" 2    ");
      break;     
    case 3:
      lcd.print(" 3    ");
      break;
    case 2051:
      lcd.print(" 3    ");
      break;     
    case 4:
      lcd.print(" 4    ");
      break;
    case 2052:
      lcd.print(" 4    ");
      break;     
    case 5:
      lcd.print(" 5    ");
      break;
    case 2053:
      lcd.print(" 5    ");
      break;
    case 6:
      lcd.print(" 6    ");
      break;
    case 2054:
      lcd.print(" 6    ");
      break;     
    case 7:
      lcd.print(" 7    ");
      break;
    case 2055:
      lcd.print(" 7    ");
      break;     
    case 8:
      lcd.print(" 8    ");
      break;
    case 2056:
      lcd.print(" 8    ");
      break;         
    case 9:
      lcd.print(" 9    ");
      break;
    case 2057:
      lcd.print(" 9   ");
      break;
    case 0:
      lcd.print(" zero    ");
      break;
    case 2048:
      lcd.print(" zero    ");
      break;         


    default:
      lcd.print(" other button   ");
    }
    delay(500);
    irrecv.resume(); // receive the next value
  }  // Your loop can do other things while waiting for an IR command
}

Logged

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

  I apologise because, it may be stupid a question, but, I have very litle knowledge of C++ and I do not know where I insert the remaining code? This sketch is has a lot of code that is not familiar to me.
I only know write the very basic code.

Thanks,
Manuel
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just fixed your code based on what Arrch found and posted the whole thing again. You could replace your code with my post and it probably will compile (if I not forgot another bug).
Logged

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

Pylon,

Thanks, I compiled it without errors. Now How I do call the function and get the key pressed instead of print it?

thanks
Manuel
Logged

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

This makes no sense:
Code:
void loop()
{
  IR_code();
}
Putting everything that loop() has to do in a function that is called (potentially) millions of times per second is not a bright idea.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS

I know it's not very bright but he told us he explicitly chose that design and he might add stuff to loop() later on, don't you think?
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Manuel Silva

You have at least two options: replace or extend the lcd.print(...) statements by a function call to do whatever you like. Or you may just set some status variable and react later in the loop() function to it.
Logged

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

Pauls,

Thanks for your inputs now and for some other times that you helped me. I am going to include more function calls in the loop. What I want is to get from this function the key pressed, in the IR command, display it and use it to do other stuff, like get the number of degrees to run the motor, etc.

Thanks
Manuel
Logged

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

Pylon,

Thanks for your suggestions. They gave me some clues and helped.

Thanks,
Manuel
Logged

Pages: [1]   Go Up
Jump to: