Problem with Attiny85, SoftwareSerial & IRremote

Hi all, i am trying to build a IR remote control using Attiny85 and controlled by Bluetooth (Android phone). I had tried build with Arduino Uno and Bluetooth (HC-05) and able to get the result i wanted. Now i want to make it smaller (and cheaper :slight_smile: ) by using Attiny85 and BT HC-05. I used my Uno as programmer and successful to send and receive data from Attiny85 (using both HC-05 and Arduino Serial Monitor). But when i include IRremote library in my sketch, i was unable to send and receive all the data (using Arduino Serial Monitor). Below is my sketch.

#include <SoftwareSerial.h>
#include <IRremote.h>
const int Rx = 3; // this is physical pin 2
const int Tx = 4; // this is physical pin 3

SoftwareSerial mySerial(Rx, Tx);

char junk;
String inputString="";
int OFF = 0; //toggle for TV On Off

IRsend irsend;

void setup()
{
pinMode(2, OUTPUT);
pinMode(Rx, INPUT);
pinMode(Tx, OUTPUT);
mySerial.begin(9600);
mySerial.println("Ready");
}

void loop()
{
if(mySerial.available())
{
while(mySerial.available())
{
delay(3);
char inChar = (char)mySerial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
mySerial.println(inputString);
mySerial.println(OFF);
while (mySerial.available() > 0)
{ junk = mySerial.read() ; } // clear the serial buffer

if(inputString =="01")
{
if(OFF == 0)
{
irsend.sendRC6(0x1000C,20);
digitalWrite(2,HIGH);
mySerial.println("LED ON");
}

else
{
irsend.sendRC6(0xC,20);
digitalWrite(2,LOW);
mySerial.println("LED OFF");
}
OFF = 1 - OFF;
}

inputString = ""; //clear inputString
}

}

For above sketch, i only able to get 0 as return for whatever character i sent via Serial Monitor to Attiny85.

But when i delete all IRremote library related lines, the sketch work properly. LED On and i get result as below.

01
0
LED ON
01
1
LED OFF
02
0

i am using Arduino 1.6.11, latest IRremote lib, latest ATtiny Core.

Is that IRremote conflict with SoftwareSerial when uploaded to Attiny85? Or my sketch have problem when using IRremote lib? i am newbie in electronic and programming.

Thanks for helps. Appreciate

That's funny, I just made the same thing.
IRremote lib is big, but edit out the protocols you don't need and it shrinks to a fraction of its size.
Open IRremote.h in a text editor, change the unneeded 1's to 0's.
I have an IR controlled stepper motor via ATTiny85 and only at 60ish% mem usage.

Hi INTP, you mean the problem of software serial when using IRremote lib is because of the memory? I will try it later and hope can solve my problem.

The IR led needs to be on a PWM pin, and I think for a sending IR LED, IRremote expects pin 3, so you'll have to change the pin you're using and code accordingly.

To see if your LED is doing what it should, look at it through a phone camera.

Actually i haven't test about IR LED yet since i still can't send command to trigger IR yet.

From the GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols, the output Pin for Attiny85 should be pin 1 as below?

Board/CPU Send Pin Timers
ATtiny84 6 1
ATtiny85 1 TINY0

Do we need to set/define in sketch to tell IRremote lib which mcu we are using? i burned Attiny85 with internal 8Mhz.

Thanks

Check IRremoteInt.h which timer is used (#if defined ATtiny... --> IR_USE_TIMERx). It must not conflict with the timer used by other libraries, like SoftwareSerial. As currently only a single timer is offered for the ATtiny's, you are on your own if you request a different timer.

Don't worry about the library size, if you only use IRsend. The big part is added only if you use the IRrecv receiver class.

If somebody is interested, I've modified the IRremote library in a way that only the required receiver protocol is added. Unfortunately these simple modifications have not yet been added to the original library (z3t0), and my version has to be updated for newer controllers.

I checked IRremoteInt.h and is stated using TINY0

#elif defined(AVR_ATtiny85)
#define IR_USE_TIMER_TINY0 // tx = pin 1

From what you say, does it mean that SoftwareSerial and IRremote can't use together with Attiny85 due to conflict of timer? Sorry, i am newbie for programming and electronic.

Do you have any suggestion to use bluetooth, IR and Attiny85 to accomplish my goal?

I'm not sure but I guess that you can use the hardware Serial for connection to the BT module, instead of SoftwareSerial. I'm not a ATtiny expert, though.

Nick Gammon has a software-serial-sendonly and a software-serial-receiveonly implementation that I have used with the trinket which is t85 based.

Ray

Nick Gammon software-serial won't interrupt with IRremote library? I will try it later.

Tried ReceiveOnlySoftwareSerial with IRremote but no luck. The same problem is still exits. Any suggestion how to move forward?

cklim01:
Tried ReceiveOnlySoftwareSerial with IRremote but no luck. The same problem is still exits. Any suggestion how to move forward?

Visit Adafruit (Trinket) or DigiStump (DigiSpark) libs. Here is Trinket IR:

cklim01:
Any suggestion how to move forward?

I can't find any reason why you can't alternate between receiving a BT command and sending an IR command.

Which ATtiny core are you using? Where did you get it?

I would certainly change your loop structure. Serial Input Basics shows the correct structure, where you keep looping until the "end marker" arrives. Then you should process the BT command and send an IR command. Maybe you've got a command "framing" error, and a better loop structure would fix that.

Also, you should probably stopListening() while you are sending the IR command. Because SoftwareSerial disables interrupts for a loooong time, it could interfere with the IR timing. Then listen() again after the command is sent. (This may depend on the ATtiny core answer.)

Also, Don't use Stringâ„¢. You really can't afford the 1.6k library and the long-term instability, especially when you don't have that much RAM to begin with. Look at Serial Input Basics to see how to store the command in a character array (aka a C string). Then you can test the command with

    if (strcmp( inputCmd, "01" ))

DrDiettrich:
I'm not sure but I guess that you can use the hardware Serial

In the category of "Things I wish I didn't know", Serial is really a TinySoftwareSerial instance in the ATTinyCore I'm looking at... Gak.

Actually, the OP should use the built-in USI (not quite a full UART) instead of SoftwareSerial. SoftwareSerial is a real CPU-killer. The USI output would be on D1 (aka PB1 or DIP pin 6), which is called DO (the letter O) in the Atmel docs. The USI input would come in on D0 (aka PB0/DIP pin 7), which is called DI (the letter I) in the Atmel docs. :stuck_out_tongue: Here's a thread where you can get some code for this almost-HardwareSerial.

Unfortunately, pin D1 is also used by the IRremote library for PWM via TIMER0's OCR0B. IRremote could be modified to use TIMER1's OCR1B & OCR1C, output on D3 and/or D4, but that's not really a job for a beginner.

Sigh,
/dev

Ouch, so many disabilities in the a single controller :frowning:

Even if I'm familiar with the details of AltSoftSerial and IRremote, I wouldn't dare to modify anything for an ATtiny, except for my private use. If only one serial device is available in hardware, and only two 8 bit timers, library conflicts are more than likely.

@cklim01, I forgot to mention that you could try NeoSWSerial as a replacement for SoftwareSerial. It does not disable interrupts while receiving, and it can transmit and receive at the same time. It's worth a shot.

Although I have not personally tested it, others have reported that it does work on the ATtiny.

Cheers,
/dev

DrDiettrich:
Ouch, so many disabilities in the a single controller :frowning:
<...>

... and yet the handicapped little uC spawned Digistump with the Digispark board and the Adafruit Trinket and maybe other boards. Kind-of insane if you ponder on the idea.

But, for some reason, there is a group of people that practice their art on under-powered microcontrollers. Reminds me a bit of the late 1970's when folks would abuse the MOS 6502.

I will admit to using a number of these little uC's many years ago as a kind of amusement and a couple of real projects emerged unexpectingly! I suspect an obese individual trying to get into a Speedo is akin to using the t85 under Arduino.

Ray

I didn't say that an ATtiny is unusable at all, but compared to the other AVR controllers it's of very limited use. At least a need for more than one serial connection, like Serial and IRremote, defeats the use of the standard libraries, so that you are back to coding on bare metal. That's a task for old school low-level experts, not for an Arduino user.

The old micros were extensible, with RAM, EPROM, and any number of timers or UARTs, just as required for any specific project. That's a very different approach, compared to nowadays single chip microcontrollers.

BTW my first µC was a SC/MP :slight_smile:

/dev, i am using latest Attinycore from GitHub - SpenceKonde/ATTinyCore: Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8.

Thanks your all your suggestion. i definitely will try out all your suggestion when i have free time. Hopefully it can solve my problem.

Finally my IR emitter is start emitting although the signal is not correct (might be frequency not correct, plan to use external crystal) and unable to send RC6 36 bit signal (IRremote library limitation).