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

0 Members and 1 Guest are viewing this topic.

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #25 on: January 16, 2014, 06:17:24 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.

yes sometimes it work sometimes it doesn't. now it doesn't even print the success statement
 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #26 on: January 17, 2014, 08:05:45 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.

yes sometimes it work sometimes it doesn't. now it doesn't even print the success statement

ok, i have some good news and some bad news. the receiver started so well but am still having a problem with the transmitter. it turns on and keeps on since i connect the power without transmitting anything.

1 other thing the when i disconnect the the arduino then reconnect it again. sometimes the code doesn't work. or acts weird? is that normal ?
 
Code: [Select]
/*
 * IRremote: IRsendDemo - demonstrates sending IR codes with IRsend
 * An IR LED must be connected to Arduino PWM pin 3.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */
//16455911

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

IRsend irsend;
#define RxD 2
#define TxD 3
SoftwareSerial Bt(RxD,TxD);

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

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

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=Spartan\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(50);                            // This delay is required.
 Bt.print("\r\n+INQ=1\r\n");             //make the slave bluetooth inquirable
 delay(50);                            // This delay is required.
 Bt.flush();
 Bt.println("Bluetooth connection established correctly!"); // if connection is successful then print to the master device
 }

String Buffer;
void loop() {
  //delay(50);
  while(Bt.available() && Serial.read() == '0') {//Serial
    //Serial.print("Transmitting");
    while (Serial.available() == 0) {//Serial
    }
    delay(50);
    while (Serial.available() > 0 ) {//Serial
     
    }
    Buffer = (String)Bt.read();//Serial
    //value = Buffer.toInt();
    Serial.println(value);
    Bt.println(value);  //Serial.println
    //for (int i = 0; i < 3; i++) {
          //  irsend.sendNEC(value,32);
           // delay(40);
         //}
    Buffer = "";
    irrecv.enableIRIn(); // Start the receiver
         } 
         if (irrecv.decode(&results)) {
           Bt.println("Receiving");//Serial
           Serial.println(results.value);
           Bt.println(results.value);
           irrecv.resume();
         
       
  }
}

 

Offline whippoorwill

  • Contributor
  • Posts: 11
Re: Arduino: Using IRremote.h with softwareserial.h
« Reply #27 on: February 02, 2014, 01:11:34 pm »
Dear Sir
I am stuck in the sending part. i use SSCOM32 window monitor to transmit. the issue is as follow when i enter zero the code should know that it's in the transmitting phase. else it's receiving. what i noticed is that when i connect to pin 0,1,2,3 and 4 the transmitting led stays on without doing any thing, also when i transmit the led send something very different of what i have entered "’928 þÊ928 ʹʒÂ" i don't know what is that. so can u please help
Code: [Select]
while(blueToothSerial.available() && blueToothSerial.read() == '0') {
    blueToothSerial.println("Transmitting");
    while (blueToothSerial.available() == 0) {
    }
    delay(50);
    while (blueToothSerial.available() > 0 ) {
      readString += (char)blueToothSerial.read();
    }
    value = readString.toInt();
    for (int i = 0; i < 3; i++) {
            irsend.sendNEC(value,32);
            delay(40);
            blueToothSerial.println(value,HEX);
         }

    readString = "";



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
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf