Author Topic: Arduino: Using IRremote.h with softwareserial.h  (Read 18437 times)

0 Members and 1 Guest are viewing this topic.

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Arduino: Using IRremote.h with softwareserial.h
« on: April 29, 2013, 07:32:39 pm »
Hello!

I am stuck on a project I'm working on. The idea is to have an android tablet control an arduino over bluetooth and have the arduino in turn control an IR led. The idea is to make an universal remote that you can point your original IR remote to and record codes, then have the tablet trigger these without having to point the tablet at the device being controlled (one of the drawbacks of using an internal IR led on the tablet/phone IMO).

I have:
Grove BlueTooth module
IR reciever
IR led
 and an arduino uno.

The example code provided with the bluetooth module works fine and i can pair with it.
I also tested out the IRremote library with a test program called IRrecord
This also works a treat and i can record codes from my tv remote and play them back through the IR led and control the tv.

The problem is in when i try to stitch these two examples together I cant get the IR part to work. And if i comment out the bluetooth code it works. This leaves me to believe that they both use the same interrupt and/or timers. I cant find any refrense to the timers used in the SoftwareSerial library used by the bluetooth.

Anyone have any ideas on where to start digging? Seems some other people have had the same issues, but i cant find any answers.

Ive started a my first Repo for this project.

My first attempt to stitch these two together:

Code: [Select]
/*
 * IRrecord: record and play back IR signals as a minimal
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * An IR LED must be connected to the output PWM pin 3.
 * A button must be connected to the input BUTTON_PIN; this is the
 * send button.
 * A visible LED can be connected to STATUS_PIN to provide status.
 *
 * The logic is:
 * If the button is pressed, send the IR code.
 * If an IR code is received, record it.
 *
 * Version 0.11 September, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include <IRremote.h>
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6  //BT RX
#define TxD 7  //BT TX

int RECV_PIN = 11;    //IR in
int BUTTON_PIN = 12;  //Button to send IR
int STATUS_PIN = 13;  //Indicate IR activity
                      //IR TX is hardcoded to pin 3 on Arduino Uno. Doesnt nativly work with Arduino Mega

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(BUTTON_PIN, INPUT);
  pinMode(STATUS_PIN, OUTPUT);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
 setupBlueToothConnection();
}

// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state

// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
  codeType = results->decode_type;
  int count = results->rawlen;
  if (codeType == UNKNOWN) {
    Serial.println("Received unknown code, saving as raw");
    codeLen = results->rawlen - 1;
    // To store raw codes:
    // Drop first value (gap)
    // Convert from ticks to microseconds
    // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
    for (int i = 1; i <= codeLen; i++) {
      if (i % 2) {
        // Mark
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
        Serial.print(" m");
      }
      else {
        // Space
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
        Serial.print(" s");
      }
      Serial.print(rawCodes[i - 1], DEC);
    }
    Serial.println("");
  }
  else {
    if (codeType == NEC) {
      Serial.print("Received NEC: ");
      if (results->value == REPEAT) {
        // Don't record a NEC repeat value as that's useless.
        Serial.println("repeat; ignoring.");
        return;
      }
    }
    else if (codeType == SONY) {
      Serial.print("Received SONY: ");
    }
    else if (codeType == RC5) {
      Serial.print("Received RC5: ");
    }
    else if (codeType == RC6) {
      Serial.print("Received RC6: ");
    }
    else {
      Serial.print("Unexpected codeType ");
      Serial.print(codeType, DEC);
      Serial.println("");
    }
    Serial.println(results->value, HEX);
    codeValue = results->value;
    codeLen = results->bits;
  }
}

void sendCode(int repeat) {
  if (codeType == NEC) {
    if (repeat) {
      irsend.sendNEC(REPEAT, codeLen);
      Serial.println("Sent NEC repeat");
    }
    else {
      irsend.sendNEC(codeValue, codeLen);
      Serial.print("Sent NEC ");
      Serial.println(codeValue, HEX);
    }
  }
  else if (codeType == SONY) {
    irsend.sendSony(codeValue, codeLen);
    Serial.print("Sent Sony ");
    Serial.println(codeValue, HEX);
  }
  else if (codeType == RC5 || codeType == RC6) {
    if (!repeat) {
      // Flip the toggle bit for a new button press
      toggle = 1 - toggle;
    }
    // Put the toggle bit into the code to send
    codeValue = codeValue & ~(1 << (codeLen - 1));
    codeValue = codeValue | (toggle << (codeLen - 1));
    if (codeType == RC5) {
      Serial.print("Sent RC5 ");
      Serial.println(codeValue, HEX);
      irsend.sendRC5(codeValue, codeLen);
    }
    else {
      irsend.sendRC6(codeValue, codeLen);
      Serial.print("Sent RC6 ");
      Serial.println(codeValue, HEX);
    }
  }
  else if (codeType == UNKNOWN /* i.e. raw */) {
    // Assume 38 KHz
    irsend.sendRaw(rawCodes, codeLen, 38);
    Serial.println("Sent raw");
  }
}

int lastButtonState;

void loop() {
 

  // If button pressed, send the code.
  int buttonState = digitalRead(BUTTON_PIN);
  if (lastButtonState == HIGH && buttonState == LOW) {
    Serial.println("Released");
    irrecv.enableIRIn(); // Re-enable receiver
  }

  if (buttonState) {
    Serial.println("Pressed, sending");
    digitalWrite(STATUS_PIN, HIGH);
    sendCode(lastButtonState == buttonState);
    digitalWrite(STATUS_PIN, LOW);
    delay(50); // Wait a bit between retransmissions
  }
  else if (irrecv.decode(&results)) {
    digitalWrite(STATUS_PIN, HIGH);
    storeCode(&results);
    irrecv.resume(); // resume receiver
    digitalWrite(STATUS_PIN, LOW);
  }
  lastButtonState = buttonState;
 
     char recvChar;
   while(1){
     if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
       recvChar = blueToothSerial.read();
       Serial.print(recvChar);
     }
     if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
       recvChar = Serial.read();
       blueToothSerial.print(recvChar);
     }
   }
}
void setupBlueToothConnection()
{
 blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
 blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 blueToothSerial.print("\r\n+STPIN=0000\r\n");//Set SLAVE pincode"0000"
 blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
 blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
 delay(2000); // This delay is required.
 blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
 Serial.println("The slave bluetooth is inquirable!");
 delay(2000); // This delay is required.
 blueToothSerial.flush();
}


Thanks for your time.

Edit: Fixed links
« Last Edit: April 29, 2013, 09:34:37 pm by Isamun »
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #1 on: April 30, 2013, 07:26:25 am »
In IRremote.cpp, void IRsend::enableIROut

Code: [Select]
// Disable the Timer2 Interrupt (which is used for receiving IR)
  TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt

So it uses timer 2.
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #2 on: April 30, 2013, 10:40:24 am »
In IRremote.cpp, void IRsend::enableIROut

Code: [Select]
// Disable the Timer2 Interrupt (which is used for receiving IR)
  TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt

So it uses timer 2.

Thank you for your response.

I realize that IRremote uses timer 2 to generate timing on multiple instances. There are also some timer overflow interrupts there, but the question is why does this conflict with SoftwareSerial, when it seems not to use any timers? It does have some interrups, but they seem redundant. There's nothing in my code that specifies which interrupt its using, which makes me really confused.

I tried changing the TX and RX pins for the BT module to see if the interrups were tied to a specific pin, but there was so difference.

Does SoftwareSerial use timer2 as well?

Thanks
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #3 on: April 30, 2013, 09:58:53 pm »
The only thing that I see that might be a problem is that the SoftwareSerial::write() method turns off interrupts when it transmits:

Code: [Select]
size_t SoftwareSerial::write(uint8_t b)
{
  if (_tx_delay == 0) {
    setWriteError();
    return 0;
  }

  uint8_t oldSREG = SREG;
  cli();  // turn off interrupts for a clean txmit

  .
  .
  .

So that when you call

Code: [Select]
       blueToothSerial.print(recvChar);

You risk missing timer interrupts which the IR receive code depends on when you transmit via SoftwareSerial.
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h [FIXED]
« Reply #4 on: May 01, 2013, 08:16:19 pm »
Hi again. Thank you for the replies. It seems that my fault was in using the example code without criticism. The following code was in the void loop() and obviously clogged it up with the while(1).

All seems to work now that i removed the while loop.

Code: [Select]
     char recvChar;
   while(1){
     if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
       recvChar = blueToothSerial.read();
       Serial.print(recvChar);
     }
     if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
       recvChar = Serial.read();
       blueToothSerial.print(recvChar);
     }
   }

Hopefully there's a lesson to be learned and some other young player can find it.

Thanks again
 

Offline Chet T16

  • Frequent Contributor
  • **
  • Posts: 535
  • Country: ie
    • Retro-Renault
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #5 on: May 01, 2013, 09:17:37 pm »
I done this before, it may be of interest: http://www.chet.ie/?p=42
Chet
Paid Electron Wrestler
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #6 on: May 02, 2013, 08:35:46 am »
I done this before, it may be of interest: http://www.chet.ie/?p=42

Very nice! I will treat it as my bible :)
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #7 on: May 06, 2013, 01:54:49 pm »
Hey all!

My project has been moving forward and we now have a somewhat working prototype. Just thought I'd post the update if anyone's interested.

https://github.com/Isamun/Remoturino
 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h [FIXED]
« Reply #8 on: January 14, 2014, 01:07:48 pm »
Hey. i tried what u have said. the led went on when i press on the remote control. put nothing appears on the putty serial monitor. can you please send me the finalized code that u have used? with all respect and appreciation


Hi again. Thank you for the replies. It seems that my fault was in using the example code without criticism. The following code was in the void loop() and obviously clogged it up with the while(1).

All seems to work now that i removed the while loop.

Code: [Select]
     char recvChar;
   while(1){
     if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
       recvChar = blueToothSerial.read();
       Serial.print(recvChar);
     }
     if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
       recvChar = Serial.read();
       blueToothSerial.print(recvChar);
     }
   }

Hopefully there's a lesson to be learned and some other young player can find it.

Thanks again
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h [FIXED]
« Reply #9 on: January 15, 2014, 03:00:23 pm »
Hey. i tried what u have said. the led went on when i press on the remote control. put nothing appears on the putty serial monitor. can you please send me the finalized code that u have used? with all respect and appreciation


Hi again. Thank you for the replies. It seems that my fault was in using the example code without criticism. The following code was in the void loop() and obviously clogged it up with the while(1).

All seems to work now that i removed the while loop.

Code: [Select]
     char recvChar;
   while(1){
     if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
       recvChar = blueToothSerial.read();
       Serial.print(recvChar);
     }
     if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
       recvChar = Serial.read();
       blueToothSerial.print(recvChar);
     }
   }

Hopefully there's a lesson to be learned and some other young player can find it.

Thanks again

Hi mate!

I'd love to help you in any way i can. But I dont quite understand the question. Everything I did on this project software-wise is on that github link. What do you mean by putty serial monitor? What serial bus are you monitoring? blueToothSerial?

What are you trying to do? Do you just want to save an ir code from a tv remote? In which case use the IR receive example in the IRremote library.
 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h [FIXED]
« Reply #10 on: January 15, 2014, 04:12:33 pm »
Hey. i tried what u have said. the led went on when i press on the remote control. put nothing appears on the putty serial monitor. can you please send me the finalized code that u have used? with all respect and appreciation


Hi again. Thank you for the replies. It seems that my fault was in using the example code without criticism. The following code was in the void loop() and obviously clogged it up with the while(1).

All seems to work now that i removed the while loop.

Code: [Select]
     char recvChar;
   while(1){
     if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
       recvChar = blueToothSerial.read();
       Serial.print(recvChar);
     }
     if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
       recvChar = Serial.read();
       blueToothSerial.print(recvChar);
     }
   }

Hopefully there's a lesson to be learned and some other young player can find it.

Thanks again

Hi mate!

I'd love to help you in any way i can. But I dont quite understand the question. Everything I did on this project software-wise is on that github link. What do you mean by putty serial monitor? What serial bus are you monitoring? blueToothSerial?

What are you trying to do? Do you just want to save an ir code from a tv remote? In which case use the IR receive example in the IRremote library.

i have created a code to detect ir pulses and transmit ir pulses. it worked but the issue that i was asked to make it work on bluetooth shield, and i am stuck at this point if u please can take a look
Code: [Select]

#include <IRremote.h>
#include <SoftwareSerial.h> //Software Serial Port

IRsend irsend;

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
String readString;
long unsigned value;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
 
 
 
  while(Serial.available() && Serial.read() == '0') {
    //Serial.print("Transmitting");
    while (Serial.available() == 0) {
    }
    delay(50);
    while (Serial.available() > 0 ) {
      readString += (char)Serial.read();
    }
    value = readString.toInt();
    //Serial.println("readstring"+readString);
      //  Serial.println("value");
        Serial.println(value);

    readString = "";
   irrecv.enableIRIn(); // Start the receiver
         } 
if (irrecv.decode(&results)) {
                   //Serial.println("Receiving");
              Serial.println(results.value);
                   irrecv.resume();
         
       
  }
}

help is urgently needed
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #11 on: January 15, 2014, 11:13:08 pm »

i have created a code to detect ir pulses and transmit ir pulses. it worked but the issue that i was asked to make it work on bluetooth shield, and i am stuck at this point if u please can take a look
Code: [Select]

#include <IRremote.h>
#include <SoftwareSerial.h> //Software Serial Port

IRsend irsend;

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
String readString;
long unsigned value;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
 
 
 
  while(Serial.available() && Serial.read() == '0') {
    //Serial.print("Transmitting");
    while (Serial.available() == 0) {
    }
    delay(50);
    while (Serial.available() > 0 ) {
      readString += (char)Serial.read();
    }
    value = readString.toInt();
    //Serial.println("readstring"+readString);
      //  Serial.println("value");
        Serial.println(value);

    readString = "";
   irrecv.enableIRIn(); // Start the receiver
         } 
if (irrecv.decode(&results)) {
                   //Serial.println("Receiving");
              Serial.println(results.value);
                   irrecv.resume();
         
       
  }
}

help is urgently needed

Im still having some trouble understanding what the goal is.
  • Do you want it to save ONE IR command and then transmit that command when triggered to do so over BT?
  • Or do you want to save multiple commands and then transmit them?
  • Does the commands need to be saved dynamically? Or do you pre-program them?
  • How are you going to communicate over BT? App (enums) or strings form a serial terminal?

If you're gonna pre program a set of IR commands and trigger them via BT, it is very simular to what I did in the Remoteorino sketch that I made for this project(on github)

In any case to get the BT shield to work you need to set up a serial bus to it and send it some configuration commands on startup like this (call this in void setup):

Code: [Select]
void setupBlueToothConnection()
{
 blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
 blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 blueToothSerial.print("\r\n+STPIN=0000\r\n");//Set SLAVE pincode"0000"
 blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
 blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
 delay(2000); // This delay is required.
 blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
 Serial.println("The slave bluetooth is inquirable!");
 delay(2000); // This delay is required.
 blueToothSerial.flush();
}

 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #12 on: January 15, 2014, 11:23:05 pm »

i have created a code to detect ir pulses and transmit ir pulses. it worked but the issue that i was asked to make it work on bluetooth shield, and i am stuck at this point if u please can take a look
Code: [Select]

#include <IRremote.h>
#include <SoftwareSerial.h> //Software Serial Port

IRsend irsend;

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
String readString;
long unsigned value;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
 
 
 
  while(Serial.available() && Serial.read() == '0') {
    //Serial.print("Transmitting");
    while (Serial.available() == 0) {
    }
    delay(50);
    while (Serial.available() > 0 ) {
      readString += (char)Serial.read();
    }
    value = readString.toInt();
    //Serial.println("readstring"+readString);
      //  Serial.println("value");
        Serial.println(value);

    readString = "";
   irrecv.enableIRIn(); // Start the receiver
         } 
if (irrecv.decode(&results)) {
                   //Serial.println("Receiving");
              Serial.println(results.value);
                   irrecv.resume();
         
       
  }
}

help is urgently needed

Im still having some trouble understanding what the goal is.
  • Do you want it to save ONE IR command and then transmit that command when triggered to do so over BT?
  • Or do you want to save multiple commands and then transmit them?
  • Does the commands need to be saved dynamically? Or do you pre-program them?
  • How are you going to communicate over BT? App (enums) or strings form a serial terminal?

If you're gonna pre program a set of IR commands and trigger them via BT, it is very simular to what I did in the Remoteorino sketch that I made for this project(on github)

In any case to get the BT shield to work you need to set up a serial bus to it and send it some configuration commands on startup like this (call this in void setup):

Code: [Select]
void setupBlueToothConnection()
{
 blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
 blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 blueToothSerial.print("\r\n+STPIN=0000\r\n");//Set SLAVE pincode"0000"
 blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
 blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
 delay(2000); // This delay is required.
 blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
 Serial.println("The slave bluetooth is inquirable!");
 delay(2000); // This delay is required.
 blueToothSerial.flush();
}

the idea is to decode ir code using ir receiver then transmit it through Bluetooth shield to the laptop. after that it will be saved to a database that i can use later and choose what code to send through bluetooth to the shield and then transmit it through ir transmitter.
so i want to receive 1 code and transmit 1 code
no reprogramming happens to the command
i will use serial terminal

please if u can modify my code as am stuck at that point. it tried merging but no luck

1 more thing, am having trouble in getting my bluetooth shield to work in a proper way. can u help me with that too
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #13 on: January 16, 2014, 12:05:18 am »
Did you get the decoding fucntion to work? Do you know what kind of IR commands you are dealing with? (NEC, SONY etc) I know that Samsung remotes are not supported by the decoder in this library and are a pain to save and work with. The get saved as RAW data.

Im not a good programmer by any stretch of the imagination, but your project seems like an intresting one and I think it sounds very doable =)

I dont have any of the gear I used to develop this on at this time so I dont know what code I could possibly modify for you. However, I'd like to share some development tactics to help you on your way.

Set some small and achievable goals:
  • Get the IR receive to dump a command to your terminal. This will confirm that the decode is working and that it can also be sendt through the BT serial bus. Copy this command and use it in the next step.
  • Next get the ir send function to send out anything you write back through the terminal
  • Then you should start a new sketch and try to get some communication with the BT shield. Try some of the example sketches and just stitch them together once you have things working.


Maybe I can have a look at this tomorrow once Im in the lab.

Only thing that seems out of place to me in your code is
Code: [Select]
    while (Serial.available() == 0) {
    }

Wont this line make the loop stuck once the serial read is over? =/
 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #14 on: January 16, 2014, 12:14:39 am »
Did you get the decoding fucntion to work? Do you know what kind of IR commands you are dealing with? (NEC, SONY etc) I know that Samsung remotes are not supported by the decoder in this library and are a pain to save and work with. The get saved as RAW data.

Im not a good programmer by any stretch of the imagination, but your project seems like an intresting one and I think it sounds very doable =)

I dont have any of the gear I used to develop this on at this time so I dont know what code I could possibly modify for you. However, I'd like to share some development tactics to help you on your way.

Set some small and achievable goals:
  • Get the IR receive to dump a command to your terminal. This will confirm that the decode is working and that it can also be sendt through the BT serial bus. Copy this command and use it in the next step.
  • Next get the ir send function to send out anything you write back through the terminal
  • Then you should start a new sketch and try to get some communication with the BT shield. Try some of the example sketches and just stitch them together once you have things working.


Maybe I can have a look at this tomorrow once Im in the lab.

Only thing that seems out of place to me in your code is
Code: [Select]
    while (Serial.available() == 0) {
    }

Wont this line make the loop stuck once the serial read is over? =/

the code works for the local serial cable, but not BT. that's why i need serious help in BT configuration. also do u have anything in mind to modify the while loop?
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #15 on: January 16, 2014, 12:25:25 am »
You atleast need something like this for the BT config. + some implementation of the IR receive functions.
 
Code: [Select]
#include <IRremote.h>
#include <SoftwareSerial.h> //Software Serial Port

IRsend irsend;

int TxD = 7; //According to http://www.seeedstudio.com/wiki/Bluetooth_Shield
int RxD = 6;
int RECV_PIN = 11; //TX = 3 (hardcoded in IRremote)
IRrecv irrecv(RECV_PIN);
decode_results results;
String readString;
long unsigned value;
SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(BUTTON_PIN, INPUT);
  pinMode(STATUS_PIN, OUTPUT);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
setupBlueToothConnection(); //forgot this
 
}

void loop() {
 
 
 
  while(Serial.available() && Serial.read() == '0') {
    //Serial.print("Transmitting");
    while (Serial.available() == 0) {
    }
    delay(50);
    while (Serial.available() > 0 ) {
      readString += (char)Serial.read();
    }
    value = readString.toInt();
    //Serial.println("readstring"+readString);
      //  Serial.println("value");
        Serial.println(value);

    readString = "";
   irrecv.enableIRIn(); // Start the receiver
         } 
if (irrecv.decode(&results)) {
                   //Serial.println("Receiving");
              Serial.println(results.value);
                   irrecv.resume();
         
       
  }
}
void setupBlueToothConnection()
{
 blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
 blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 blueToothSerial.print("\r\n+STPIN=0000\r\n");//Set SLAVE pincode"0000"
 blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
 blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
 delay(2000); // This delay is required.
 blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
 Serial.println("The slave bluetooth is inquirable!");
 delay(2000); // This delay is required.
 blueToothSerial.flush();
}

Edit forgot to call setupBlueToothConnection() in setup
« Last Edit: January 16, 2014, 12:28:57 am by Isamun »
 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #16 on: January 16, 2014, 12:41:08 am »
You atleast need something like this for the BT config. + some implementation of the IR receive functions.
 
Code: [Select]
#include <IRremote.h>
#include <SoftwareSerial.h> //Software Serial Port

IRsend irsend;

int TxD = 7; //According to http://www.seeedstudio.com/wiki/Bluetooth_Shield
int RxD = 6;
int RECV_PIN = 11; //TX = 3 (hardcoded in IRremote)
IRrecv irrecv(RECV_PIN);
decode_results results;
String readString;
long unsigned value;
SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(BUTTON_PIN, INPUT);
  pinMode(STATUS_PIN, OUTPUT);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
setupBlueToothConnection(); //forgot this
 
}

void loop() {
 
 
 
  while(Serial.available() && Serial.read() == '0') {
    //Serial.print("Transmitting");
    while (Serial.available() == 0) {
    }
    delay(50);
    while (Serial.available() > 0 ) {
      readString += (char)Serial.read();
    }
    value = readString.toInt();
    //Serial.println("readstring"+readString);
      //  Serial.println("value");
        Serial.println(value);

    readString = "";
   irrecv.enableIRIn(); // Start the receiver
         } 
if (irrecv.decode(&results)) {
                   //Serial.println("Receiving");
              Serial.println(results.value);
                   irrecv.resume();
         
       
  }
}
void setupBlueToothConnection()
{
 blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
 blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 blueToothSerial.print("\r\n+STPIN=0000\r\n");//Set SLAVE pincode"0000"
 blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
 blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
 delay(2000); // This delay is required.
 blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
 Serial.println("The slave bluetooth is inquirable!");
 delay(2000); // This delay is required.
 blueToothSerial.flush();
}

Edit forgot to call setupBlueToothConnection() in setup

done with the edit, but didn't work at all :(
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #17 on: January 16, 2014, 12:43:13 am »
Did you try the example code for the BT shield?
 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #18 on: January 16, 2014, 12:45:06 am »
Did you try the example code for the BT shield?

yes, and it printed setup completed successfully, but amn't sure am communicating with the shield correctly
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #19 on: January 16, 2014, 12:54:58 am »
Meaning you cant pair with it? What example are you using and what BT shield is it?
 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #20 on: January 16, 2014, 12:57:53 am »
Meaning you cant pair with it? What example are you using and what BT shield is it?

it's paired and i can see the last
Code: [Select]
  Bt.begin(38400); // this sets the the module to run at the default bound rate
 Bt.print("\r\n+STWMOD = 0\r\n");        //set the bluetooth work in slave mode
 Bt.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 Bt.print("\r\n+STOAUT=1\r\n");          // Permit Paired device to connect me
 Bt.print("\r\n+STAUTO=0\r\n");          // Auto-connection should be forbidden here
 delay(2000);                            // This delay is required.
 Bt.print("\r\n+INQ=1\r\n");             //make the slave bluetooth inquirable
 delay(2000);                            // This delay is required.
 Bt.flush();
 Bt.print("Bluetooth connection established correctly!")
bluetooth connection established correctly. í use slave.pde from bluetoothshield demo
but there is another example where i can send commends to the shield. i couldn't compile it
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #21 on: January 16, 2014, 01:04:36 am »
Try to merge that example with the IRrecord and print the received code with bt.print(). That should get you half way to your goal :)
 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #22 on: January 16, 2014, 01:09:13 am »
Try to merge that example with the IRrecord and print the received code with bt.print(). That should get you half way to your goal :)
This is what i did so far and now am getting nothing
Code: [Select]
#include <SoftwareSerial.h>

int rx_pin = 2;     // setting digital pin 6 to be the receiving pin
int tx_pin = 3;     // setting the digital pin 7 to be the transmitting pin

SoftwareSerial Bt(rx_pin,tx_pin); // this creates a new SoftwareSerial object on
                                  // the selected pins!
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
String readString;
long unsigned value;
IRsend irsend;

void setup(){
 Serial.begin(9600); // this is a connection between the arduino and
                       // the pc via USB
 irrecv.enableIRIn();
 pinMode(rx_pin, INPUT);  // receiving pin as INPUT
 pinMode(tx_pin, OUTPUT); // transmitting pin as OUTPUT
 

 bluetoothInitiate(); // this function will initiate our bluetooth (next section)
 } 

void bluetoothInitiate(){
 // this part is copied from the Seeeduino example*
 Bt.begin(38400); // this sets the the module to run at the default bound rate
 Bt.print("\r\n+STWMOD = 0\r\n");        //set the bluetooth work in slave mode
 Bt.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 Bt.print("\r\n+STOAUT=1\r\n");          // Permit Paired device to connect me
 Bt.print("\r\n+STAUTO=0\r\n");          // Auto-connection should be forbidden here
 delay(2000);                            // This delay is required.
 Bt.print("\r\n+INQ=1\r\n");             //make the slave bluetooth inquirable
 delay(2000);                            // This delay is required.
 Bt.flush();
 Bt.print("Bluetooth connection established correctly!"); // if connection is successful then print to the master device
 }
 
 String buffer; // this is where we are going to store the received character
 
 void loop(){
   if(Bt.available()){
     buffer +=(char)Bt.read();
   }
   value = buffer.toInt();
    for (int i = 0; i < 3; i++) {
            irsend.sendNEC(value,32);
            delay(40);
         }

    buffer = "";
   irrecv.enableIRIn();

 // this is for recieving on the device with bluetooth, now we can make it send stuff too!
 if(Serial.available()){   // this will check if any data is sent
   while(Serial.available()==0){
   }
   delay(50);
   while(Serial.available()>0){
     buffer +=(char)Serial.read();
   }
  Bt.print(buffer);
 }
   if (irrecv.decode(&results)) {
              Bt.println("Receiving");
              Serial.println(results.value);
                   irrecv.resume();
     }
  }
               
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #23 on: January 16, 2014, 01:21:26 am »
Can you confirm that you get "Bluetooth connection established correctly! on your laptop through BT?

Next,
Code: [Select]
if(Serial.available()){   // this will check if any data is sent
   while(Serial.available()==0){
   }
   delay(50);
   while(Serial.available()>0){
     buffer +=(char)Serial.read();
   }
  Bt.print(buffer);
Should print everything you type on your arduino serial terminal to the BT channel. Does this work? Try to remove the first while loop, as could possibly block the rest of the program.
 

Offline IsamunTopic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: no
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #24 on: January 16, 2014, 03:41:39 am »
Im going to bed now.. Ill be at the lab tomorrow. I can will try out this project then. Hopefully I can come up with something useful for you. In any case, good luck :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf