(FIXED) ATtiny85 IRremote & fade doesn't work. Please help!

Hello to everyone,

This is my first post so please excuse me if I'm in the wrong section.

I want to make an IR receiver using a vs1838b ir receiver and the attiny85 to switch an led with a fading effect.

The problem that i face is that althought I managed the code to work correct on the arduino board, loading it to the attiny works too but not doing the fading routine.

I use the tinycore from David A. Mellis and the IRremote library ported from N. Gammon for the attiny.
Everything compiles ok and works except the fade routine :frowning: .

I am a newbie on arduinos so everything in the code are bits and pieces from examples and other projects that i have connected.

Here is the code that is being used:

#include <IRremote.h>
#include <IRremoteInt.h>

/*
source: www.electroschematics.com
You'll need to change the led pins and the codes
accordingly to your configuration and IR remote
*/

int RECV_PIN = 4; // the pin where you connect the output pin of TSOP4838
int led1 = 1;
//int led2 = 4;
//int led3 = 7;
int itsONled[] = {
0, 0, 0, 0 };

/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */

#define code1 0xFFA25D // code received from button on/off
#define code2 0xFFA857 // code received from button -
#define code3 0xFF906F // code received from button +

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT);
// pinMode(led2, OUTPUT);
// pinMode(led3, OUTPUT);
// digitalWrite(3, LOW);
}

void loop()
{
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch (value) {
case code1:
if (itsONled[1] == 1) { // if first led is on then
fadeOff();
itsONled[1] = 0; // and set its state as off
}
else { // else if first led is off
fadeOn();
itsONled[1] = 1; // and set its state as on
break;

}
}
/*
case code2:
if (itsONled[1] == 1) {
brightness = brightness - fadeAmount;
analogWrite(led1, brightness);
itsONled[1] = 0;

} else {
digitalWrite(led1, HIGH);
itsONled[2] = 1;
}
break;
/* case code3:
if (itsONled[3] == 1) {
digitalWrite(led3, LOW);
itsONled[3] = 0;
} else {
digitalWrite(led3, HIGH);
itsONled[3] = 1;
}
break;
}
*/
irrecv.resume(); // Receive the next value
}
}

void fadeOff()
{
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(led1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

void fadeOn()
{
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(led1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

Any help will be much appreciated.

Anyone??? Please.... :-[

Are you sure you are running the ATtiny85 @ 8MHz ?

(Select ATtin85 @ 8MHz in the board menu, and burn bootloader)

Hi !

Yes I'm pretty sure about that. I also testes the fade example alone and it works just fine... :o

It would be a good thing if you could test the codes that you actually receive, using Serial.println().

To do this use this tiny core

http://code.google.com/p/arduino-tiny/

With this core you will get the serial output on pin 3 of your ATtiny85.

For more info : Communication with Tiny's

First of all thanks a lot for your time.
The ir receiver works and I know that because it does what it is supposed to do.
The only problem is that it doesn't fade the led while changing states from off to on and vice versa.
My problem is exactly there and that's the only thing I want to fix.

Are you sure the pin you are using for fading has PWM available? If not, you will not get fading happening.

if (itsONled[1] == 1) {       // if first led is on then

The first led is index 0, not 1. This should not be a problem with the code you have at the moment as you are checking the same status at index 1 all the time, but could become an issue later.

I tried the fading example using the same pin and it worked just fine, so I'm sure that it is PWM capable.

Thanks for the, tip you are right about that, I will correct it on my code.

This is getting really confusing I think I have tried almost everything at the code but it just refuses to work... >:(

void loop() 
{
  if (irrecv.decode(&results)) 
  {
    unsigned int value = results.value;
    switch (value) 
    {
      case code1:
      if (itsONled[1] == 1) 
      {       // if first led is on then
        fadeOff();
        itsONled[1] = 0;           // and set its state as off
      }
      else 
      {                      // else if first led is off
        fadeOn();
        itsONled[1] = 1;          // and set its state as on
        break;    // <<<<<<<<<<<<<<<<<<
      }
    }
    irrecv.resume(); // Receive the next value
  }
}

emphasis -

      else 
      {                      // else if first led is off
        fadeOn();
        itsONled[1] = 1;          // and set its state as on
        break;    // <<<<<<<<<<<<<<<<<<
      }

Is that break; in the proper place there, inside the else? If the if condition is satisfied then it won't look to the else stipulation for a break.

versus (better?) -

void loop() 
{
  if (irrecv.decode(&results)) 
  {
    unsigned int value = results.value;
    switch (value) 
    {
      case code1:
      if (itsONled[1] == 1) 
      {       // if first led is on then
        fadeOff();
        itsONled[1] = 0;           // and set its state as off
      }
      else 
      {                      // else if first led is off
        fadeOn();
        itsONled[1] = 1;          // and set its state as on
      }
      break;    // <<<<<<<<<<<<<<<<<<
    }
    irrecv.resume(); // Receive the next value
  }
}

Thanks for the info.

Just changed the "break;" outside the "else" and looks more responsive now but still not fading the led... :frowning:

Now, by using "pin 1", a/k/a D1 et al, you are using IC pin 6 - Yes ?

#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 4; // the pin where you connect the output pin of TSOP4838
int led1 = 1;
int itsONled[] = {0, 0, 0, 0};
#define code1  0xFFA25D // code received from button on/off
#define code2  0xFFA857 // code received from button -
#define code3  0xFF906F // code received from button +
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  irrecv.enableIRIn();  // Start the receiver
  pinMode(led1, OUTPUT);
}
void loop() 
{
  fadeOn();
  delay(500);
  fadeOff();
  delay(500);
}
void fadeOff()
{
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) 
  {
    analogWrite(led1, fadeValue);
    delay(30);
  }
}
void fadeOn()
{
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) 
  {
    analogWrite(led1, fadeValue);
    delay(30);
  }
}

That's your sketch stripped down to alternating the fadeOn and fadeOff functions.
Does that much work?
"Pins" 0, 1, and 4 [IC pins 5, 6, and 3] are PWM'able.

Yes IC pin 6 is what I use for the led.
I will test the stripped down version of the code to see if it works today and inform you asap.
I assume that if this version of code works there is something going wrong with the "switch" statement that confuses ATtiny?

Ok just tested the code but the led just turns on and off without fading!

I cannot understand why the fading exaple works but not this...

tolis18gr:
Ok just tested the code but the led just turns on and off without fading!
I cannot understand why the fading exaple works but not this...

Does that mean that the stripped down sketch that I posted resulted the fadeOn/Off or not?
If it did then there's something not right in the decoding or the branching.
In that event I would start (start over) by getting one thing right first - to receive a "code" to effect one result, consistently, and move on from there.
(Being without those libraries, I don't know if the irrecv-related functions are being used properly.)

No unfortunately it didn't.

If it did then there's something not right in the decoding or the branching.
In that event I would start (start over) by getting one thing right first - to receive a "code" to effect one result, consistently, and move on from there.

Actually I tried that using different buttons for on and off but no matter how i tried, using different coding, the fade never worked.

(Being without those libraries, I don't know if the irrecv-related functions are being used properly.)

I can give you all the libraries and the cores I use if you would like to give it a try. I will be very gratefull if you do!!! :slight_smile:

I know that the code works fine on an atmega chip i tried, but the ATtiny is making the hole project so small and It has just the right amount of pins that i need.

Here is a ATtiny85-tested fade routine -

byte ledPin =1;
byte brightness;
void setup()
{
  pinMode(ledPin,OUTPUT);
}
void loop ()
{
  fadeUp();
  fadeDown();
}
void fadeUp()
{
  for (brightness=0; brightness<255; brightness ++)
  {
    analogWrite(ledPin,brightness);
    delay(10);
  }
}
void fadeDown()
{
  for (brightness=0; brightness<255; brightness ++)
  {
    analogWrite(ledPin,(255-brightness));
    delay(10);
  }
}

There's not a lot of difference between higher values of "brightness", the logarithmic response of the human retina, etc. But now you have a working, tested fade capability.

corrected formatting errors

The IR library use one of the timers, which limits the pins that works with PWM

Erni:
The IR library use one of the timers, which limits the pins that works with PWM

Any idea which?
"Pin 0" and "Pin 1" are OC0A and OC0B, Pin 4 is OC1B.
IF it's not working with OC0 THEN try it with OC1 ("Pin 4") ?

Yes PWM on pin 4 works, so that leaves just one pwm pin.

It worked!!!! :slight_smile: :slight_smile: :slight_smile:

Runaway Pancake I don't know how to thank you man!

I just changed the led to pin4 and the ir receiver on pin2 everything works just fine.

The only thing I have to do know is to program the other two buttons to change the brightness of the led.

Thank you Erni too!