Help Change timer 2 to timer 0

I am attempting to change this code from using timer 2 to using timer 0 so I can use it with an attiny85 that does not have timer 2.

Here is a link to the zip file containing the code: http://arcfn.com/files/IRremote.zip

I am modifying the .cpp file

When I first tried to upload this without having changed the timer I got this error:

/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function ‘void IRsend::mark(int)’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:172:3: error: ‘TCCR2A’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:172:13: error: ‘COM2B1’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function ‘void IRsend::space(int)’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:180:3: error: ‘TCCR2A’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:180:15: error: ‘COM2B1’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function ‘void IRsend::enableIROut(int)’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:198:3: error: ‘TIMSK2’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:198:14: error: ‘TOIE2’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:207:3: error: ‘TCCR2A’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:207:12: error: ‘WGM20’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:208:3: error: ‘TCCR2B’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:208:12: error: ‘WGM22’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:208:25: error: ‘CS20’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:211:3: error: ‘OCR2A’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:212:3: error: ‘OCR2B’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function ‘void IRrecv::enableIRIn()’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:224:3: error: ‘TCCR2A’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:229:3: error: ‘TCCR2B’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:229:3: error: ‘CS22’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:230:3: error: ‘CS21’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:231:3: error: ‘CS20’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:234:3: error: ‘TIMSK2’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:234:3: error: ‘TOIE2’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:236:3: error: ‘TCNT2’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In function ‘void TIMER2_OVF_vect()’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:266:3: error: ‘TCNT2’ was not declared in this scope

I went through the .cpp file and changed each one of the parts mentioned so that the 2s became 0s. Having done that I am still getting this error message:

/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function ‘void IRsend::enableIROut(int)’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:198:3: error: ‘TIMSK0’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function ‘void IRrecv::enableIRIn()’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:234:3: error: ‘TIMSK0’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:236:3: error: ‘TCNT2’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In function ‘void TIMER2_OVF_vect()’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:266:3: error: ‘TCNT2’ was not declared in this scope

I don't understand! Please help.

Thanks,
Will

Have a look at the datasheet. Since there is only one timer it is TIMSK not TIMSK0. And so on for the other things.

Thanks Nick! I have changed the TIMSK0's to TIMSK but i am still getting this error message:

/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function ‘void IRrecv::enableIRIn()’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:236:3: error: ‘TCNT2’ was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In function ‘void TIMER2_OVF_vect()’:
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:266:3: error: ‘TCNT2’ was not declared in this scope

and yet when I search the .cpp file there is no 'TCNT2'

I had changed these to TCNT0. Need I go back and delete all of the zeros inserted at the beginning?

Thank you so much again!

correction: there is no TCNT at all in the .cpp file

Look in IRremoteInt.h which is included by the main library:

// clock timer reset value
#define INIT_TIMER_COUNT2 (CLK - USECPERTICK*CLKSPERUSEC + CLKFUDGE)
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT2

Thank you so much Nick! It compiled! I will test the circuit and report back!

Ok it now compiles! However, I now get this error when attempting to load the sketch onto the attiny85:

avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: verification error, first mismatch at byte 0x03d3
         0xd1 != 0xff
avrdude: verification error; content mismatch

If I keep the output labeled status on pin 13 and change the recieve input to 0 and the button input to 1 I get this error:

avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85

For reference, this is the project I am trying to load to the attiny85: http://www.arcfn.com/2009/09/arduino-universal-remote-record-and.html
I have seen elsewhere that this last error is to be expected when loading sketches to the attiny85 but should not interfere with anything. In any case, I have changed the status pin to pin 4 with a red LED. I have changed the receive pin to pin 1. I have changed the button pin to pin 0. The output signal is sent to the IR LED through pin 3 in the original code so I have attached the IR led here to pin 3 on the attiny85.

I am using an arduino board as and ISP programmer connected to my attiny85 on a breadboard.
When I load the sketch, the tx and rx leds on the arduino board keep blinking long after the sketch is loaded. The led on the arduino board attached to pin 13 also flashes along with the tx and rx leds. After a few seconds of this they all stop and do not come back on. Neither the IR led nor the status led on the breadboard connected to the attiny light up at all. The circuit also does not appear to receive any IR signals directed at the IR receiver.

Code now loads fine after a few seconds. The circuit is totally unresponsive though. It doesn't register any IR signals, status or IR output. I have tested each pin with an LED using the example Blink sketch and they all work fine.

Before I take a look at that, hunt down the source for the TV B Gone gadget. That uses an Attiny and it does IR sending. You might get some clues from that.

The most obvious difference for the tv b gone is that it uses an external 8mhz resonator to get accurate timing. If that were the problem though, wouldn't I be getting at least inaccurate readings and outputs? I'm still learning my way around comparatively simple sketches, but the tv b gone seems to go about sending IR signals differently than the project I am trying to adapt.

I sort-of have it going, here's proof:

Channel 1 is measuring the IR receiver connected to pin 5 (D0) of the Attiny85.

Channel 2 is measuring an LED I have on pin 3 (D4) of the Attiny.

That blip is bringing D4 high for a moment when it recognizes a code on my remote.

Sketch:

#include <IRremote.h>
const int RECV_PIN = 0;
const int LED = 4;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  DDRB |= _BV(4);  // pinMode (4, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    PORTB |= _BV (4);  // digitalWrite (4, HIGH);
    irrecv.resume(); // Receive the next value
    PORTB &= ~_BV (4);  // digitalWrite (4, LOW);
  }
}

I seem to be very short of memory. Even adding a delay(100) gave a compiler error.

The library I compiled against is here:

http://gammon.com.au/Arduino/IRremote_Attiny.zip

I had to change a couple more things, in particular:

#define SYSCLOCK 16000000  // main Arduino clock

to:

#define SYSCLOCK 8000000  // main Arduino clock

After all, it is an 8 MHz clock.

And the ISR, from:

ISR(TIMER2_OVF_vect)

to:

ISR(TIMER0_OVF_vect)

Look for blocks of code like this to see the changes I made:

#if defined (__AVR_ATtinyX5__)
  // code for Attiny
#else 
  // code for Atmega328
#endif

I'm not sure quite how useful it is, you'll need to work out the memory issues before you can get too much further.

Wow amazing job Nick! That's wonderful. You're right about the memory being a problem, but at least we know it is possible for an attiny85 to recognize an IR signal.

Thanks for the great modifications on the library Nick.

I tried to compile a basic sketch using the adapted library but it comes with Wprogram.h not found. Which I changed to Arduino.h but results to a large bunch of errors when compiling again.

Has anyone managed to use this library with the new version of Arduino IDE? Or maybe there is a new version of this library somewhere?

What tiny core are you using?

noeldum:
Thanks for the great modifications on the library Nick.

I tried to compile a basic sketch using the adapted library but it comes with Wprogram.h not found. Which I changed to Arduino.h but results to a large bunch of errors when compiling again.

Has anyone managed to use this library with the new version of Arduino IDE? Or maybe there is a new version of this library somewhere?

What tiny core are you using?

Hello,

Same issue for me with arduino IDE 1.6.6.

So, I decided to use Arduino 1.0 instead with this core Google Code Archive - Long-term storage for Google Code Project Hosting. (read carrefully the readme)

and I get no error with this minimalist sketch:

#include <IRremote.h>

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
}

next step it's to try with this core https://code.google.com/p/arduino-tiny/downloads/detail?name=arduino-tiny-0150-0020.zip on IDE 1.6.6

next step it's to try with this core https://code.google.com/p/arduino-tiny/downloads/detail?name=arduino-tiny-0150-0020.zip on IDE 1.6.6

Don't work... probably because this core is only compatible with arduino IDE 1.5.

Anyway, I will keep Arduino IDE 1.0.

Question: For Irsend (like irsend.sendRC5(0x4c, 12);) which is the default pin used by Iremote library on attiny 85? I know on Arduino Uno is the PWM 3 pin but I don't know for Attiny85 (I have the Digispark version)

Thank you in advance

I tried the library from here: GitHub - TKJElectronics/ATtinyRemote: Universal remote based on an ATtiny85

with IDE 1.6.6 and this core GitHub - damellis/attiny: ATtiny microcontroller support for the Arduino IDE : no error from compiler with this code:

#include <IRremote.h>

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

but there aren't all IR protocols in the library. For instance, RC5 is missing and probably anothers. Gosh!

work in progress....