(SOLVED) Trying to fade 2 LED's in and out at the same time and only one fades.

I have 2 leds on an ATTINY85. the LED’s go through a 220 Ohm resistor to pins 3 and 4 on the IC. I have some other stuff connected to get a motor to do some things, that works fine. The LED’s I just want to fade in and out. One does as I expect, the other does not. I did some checking online and people suggested to do what I had already done.(here) So here’s the code:

const int Input1A = 0;
const int Input2A = 1;
const int ledL = 3;
const int ledR = 4;


void setup() {
  pinMode(Input1A, OUTPUT);
  pinMode(Input2A, OUTPUT);


}

void loop() {
  for (int fadeValue = 0 ; fadeValue <=255; fadeValue += 5){
    analogWrite(ledL, fadeValue);
    analogWrite(ledR, fadeValue);
    delay(50);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(ledL, fadeValue);
    analogWrite(ledR, fadeValue);
    delay(50);
  }
  digitalWrite(Input1A, LOW);
  digitalWrite(Input2A, HIGH);
  delay(2000);
  digitalWrite(Input1A, HIGH);
  digitalWrite(Input2A, LOW);
  delay(2000);

}

ledR will fade in and ledL will turn on quickly. Then ledL goes out and then ledR fades out.

I don’t have a diagram of the circuit, but I figure it’s simple enough that one shouldn’t be needed. I’m not sure what the issue is here. I’m not very good with code. :confused:

Couple of things:

const int Input1A = 0;

As a matter of style, it is better not to call a variable Input if it is used for output. This will get confusing when you graduate on to larger programs.

const int ledL = 3;
const int ledR = 4;

These are not initialised to outputs. Also, you cannot 'fade' pin 4 as it is not a PWM pin (this is the answer to your question). On an Uno these are pins 3, 5, 6, 9, 10, 11.

Also not a good idea to use delay() unless this is the only code you will write for this application. I am sure you have seen that a lot already :)

Thanks. That name was the name of my h-bridge pin. But I see your point. Thank you.

I read that on analogWrite you don't need to initialize as input/output. Also this is on an ATTINY85 and only pins 2 & 5 aren't PWM, so I should still be good. I don't know what I'd use instead of delay().

Perhaps the info I have is wrong though.

const int Input1A = 3;
const int Input2A = 1;
const int ledL = 0;
const int ledR = 4;

Switching pins 0 and 3 to what I have here makes everything work now(well that and moving some wires around). Thanks bro!

red913: I read that on analogWrite you don't need to initialize as input/output.

pinMode(pin, OUTPUT) is the first thing analogWrite() does on AVR boards.

red913:
Also this is on an ATTINY85 and only pins 2 & 5 aren’t PWM, so I should still be good.

Are you sure? According to http://www.atmel.com/Images/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet-Summary.pdf :

Peripheral Features
– 8-bit Timer/Counter with Prescaler and Two PWM Channels

Maybe you have it the wrong way around? rather than all but two pins being PWM, it’s actually only two that are?

Just for giggles, try a different pin.

PaulMurrayCbr: Are you sure? According to http://www.atmel.com/Images/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet-Summary.pdf :

Maybe you have it the wrong way around? rather than all but two pins being PWM, it's actually only two that are?

Just for giggles, try a different pin.

ya, I got it solved. I tried a different pin and it worked.

oqibidipo: pinMode(pin, OUTPUT) is the first thing analogWrite() does on AVR boards.

So you're saying the analogWrite function does that for you and that's why you don't have to setup the pinMode yourself?

red913: So you're saying the analogWrite function does that for you and that's why you don't have to setup the pinMode yourself?

Correct. It's the very first line in the analogWrite function.

I see where the pin confusion was. I was referencing the pins according to how they are addressed in the code. I was looking at the data sheet and realizing that the actual pins numbers is what I should have been using to reference them. So when I say I'm using pins 1 and 3, those are D1 and D3 or pins 2 and 6 as they appear on the datasheet. Sorry about the confusion there. I'll keep that in mind in the future.