Hi
I have one personal project where I try to control an RGB LED with an IR sensor and an Attiny85 processor.
The idea is to have a PWM in PB0, PB1 and PB4.
But I have noticed that the IR Library that I'm using (IRremote_Attiny modified to accept Samsung codes) is usint the Timer2 to control the IR Sensor.
So, that causes problems with the PB0 and PB1.
I'm a newbie, specialy in interupt questions, and I'll try to explain what I have understand of all the posts, and web pages I have read.
Tha Attiny85 has only 2 timers and one is used for the IRremote library, so the other can controll 2 of the PB. Now is controlling the PB4, and the PWM is working on PB4.
I think there are some solutions (it doesn'nt mean that I know how to do it)
1 - Change the IRremote library to use the other timer and this way I will have a PWM on PB0 and PB1, probably I lose the PWM on PB4 but I'm not sure that will be happen.
2 - Change the IRremote library to manage the PWM on PB0 and PB1 at the same time of the IR sensor (I don't know if this is possible or not).
3 - Change the desing and try to do it in a different way. Try to use PWM with PB3 and PB4, and use the PB2 to read the IRR code.
The part of the code that I thing I need to modify is
// initialization
void IRrecv::enableIRIn() {
// setup pulse clock timer interrupt
TCCR0A = 0; // normal mode
//Prescale /8 (16M/8 = 0.5 microseconds per tick)
// Therefore, the timer interval can range from 0.5 to 128 microseconds
// depending on the reset value (255 to 0)
cbi(TCCR0B,CS02);
sbi(TCCR0B,CS01);
cbi(TCCR0B,CS00);
//Timer2 Overflow Interrupt Enable
sbi(TIMSK,TOIE0);
RESET_TIMER2;
sei(); // enable interrupts
// initialize state machine variables
irparams.rcvstate = STATE_IDLE;
irparams.rawlen = 0;
// set pin modes
pinMode(irparams.recvpin, INPUT);
}
// TIMER2 interrupt code to collect raw data.
// Widths of alternating SPACE, MARK are recorded in rawbuf.
// Recorded in ticks of 50 microseconds.
// rawlen counts the number of entries recorded so far.
// First entry is the SPACE between transmissions.
// As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues.
// As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts
ISR(TIMER0_OVF_vect)
{
RESET_TIMER2;
uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
irparams.timer++; // One more 50us tick
if (irparams.rawlen >= RAWBUF) {
// Buffer overflow
irparams.rcvstate = STATE_STOP;
}
switch(irparams.rcvstate) {
case STATE_IDLE: // In the middle of a gap
if (irdata == MARK) {
if (irparams.timer < GAP_TICKS) {
// Not big enough to be a gap.
irparams.timer = 0;
}
else {
// gap just ended, record duration and start recording transmission
irparams.rawlen = 0;
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
}
}
break;
case STATE_MARK: // timing MARK
if (irdata == SPACE) { // MARK ended, record time
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_SPACE;
}
break;
case STATE_SPACE: // timing SPACE
if (irdata == MARK) { // SPACE just ended, record it
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
}
else { // SPACE
if (irparams.timer > GAP_TICKS) {
// big SPACE, indicates gap between codes
// Mark current code as ready for processing
// Switch to STOP
// Don't reset timer; keep counting space width
irparams.rcvstate = STATE_STOP;
}
}
break;
case STATE_STOP: // waiting, measuring gap
if (irdata == MARK) { // reset gap timer
irparams.timer = 0;
}
break;
}
}
To compile all the project I use the Arduino 1.0.5-r2 with the IRremote_Attiny library to optimize the code.
Thanks for your time and any help is wellcome.
Sorry for my english, but I'm not a native speaker ![]()
cr_bb_samsung_85_01.ino (7.99 KB)
IRremote.zip (5.42 KB)