Fade In + LED High

[SOLVED]

Hi guys, i’m having trouble with a simple code here, I need to ‘fade in’ 4 leds and them stay in the max power for 1 minute, after that ‘fade out’ and stay off for 1 minute too. Actually my actual code is the basic fade code:

int Z1 = 11;
int Z2 = 10;
int V1 = 9;
int V2 = 6;
int brightness = 0;
int fadeAmount = 5;

void setup() {
pinMode(Z1, OUTPUT);
pinMode(Z2, OUTPUT);
pinMode(V1, OUTPUT);
pinMode(V2, OUTPUT);

}

void loop() {
analogWrite(Z1, brightness);

brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}

analogWrite(Z2, brightness);

brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;

}

analogWrite(V1, brightness);

brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}

analogWrite(V2, brightness);

brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}

delay (60)
}

I hope you guys can help me, its for my school’s science fair.

i'm having trouble with a simple code

What sort of trouble?
Will it compile?
Will it run and do the wrong thing? If so what wrong thing.

How is your circuit wired up? An Arduino sketch means little without knowing the schematic.

While you are at it please read the how to use this forum sticky post. It will tell you the correct way to post code on this forum.

As you have written your code the brightness gets increased by four times the fade amount on every loop. Why not simply calculate the brightness once and then write that value to all your LEDs?

I'm sorry, actually the "trouble" is my lack of knowledge. This code is totally functional, the fade thing it's working, but my problem is that I don't know how to still the led high when it hits 255 at brightness amount. The leds are connected in a 12v battery and it passes through a MOSFET TIP122 to get in arduino (it's the common circuit to use 12v LEDs in arduino, I saw a lot of tutorials doing the same). I will try do a schematic but I'm new in this technology area.

Sorry for don't let you help me in the proper way.

This code is totally functional, the fade thing it’s working, but my problem is that I don’t know how to still the led high when it hits 255 at brightness amount.

So it is not total functional is it then?
There is something wrong with it.

Replace your setup and loop functions with this

void setup() {

}

void loop() {
analogWrite(Z1, brightness);
analogWrite(Z2, brightness);
analogWrite(V1, brightness);
analogWrite(V2, brightness);

 brightness = brightness + fadeAmount;

 if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
    delay(60000l); // that is a lower case L on the end of that number
  }
delay(60);
}

No need for a pin mode setting if you are using PWM.
No need to calculate brightness four times, once will do fine.
When you change the direction of the fade simply delay for 60 seconds.

    delay(60000l); // that is a lower case L on the end of that number

That's why I like to use a capital letter. Or in this case, leave it off because it does nothing :wink:

But what is the driver for the led? You can't just power a (high power) led from 12V... Do you use a resistor? What value? What led?

Grumpy_Mike:
Replace your setup and loop functions with this

Thank you for the help, but when the LED 'fade out' he doesn't turn off completely. I mean, the LED still emitting a very low light but I would like that he totally turned off.

I'm using 220 Ohms resistors.

I think the problem is in the "brightness = brightness + fadeamount", when the brightness its supposed to be 0, there is still this "5"value from 'fadeAmount' that don't let the LED turn off.

Why do you think it’s not going to 0?

With brightness = 5 and fadeAmount = -5 you have:
brightness + fadeAmount => 5 + -5 = 0

Only trouble is, you write to the leds only in the next cycle aka after the wait. So change the brightness directly before waiting.

const byte Z1Pin = 11;
const byte Z2Pin = 10;
const byte V1Pin = 9;
const byte V2Pin = 6;
byte brightness = 0;
int fadeAmount = 5;

void setup() {

}

void loop() {
  brightness = contrain(brightness + fadeAmount, 0, 255);
  
  analogWrite(Z1Pin, brightness);
  analogWrite(Z2Pin, brightness);
  analogWrite(V1Pin, brightness);
  analogWrite(V2Pin, brightness);

  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
    delay(60000l); // that is a lower case L on the end of that number
  }
  else{
    delay(60);
  }
}

Also added contrain() to get you out of trouble is you set ‘fadeAmount’ to something that’s not a whole fraction of 255 or a start value of the brightness that’s not a whole of ‘fadeAmount’.

septillion:
Also added contrain() to get you out of trouble is you set ‘fadeAmount’ to something that’s not a whole fraction of 255 or a start value of the brightness that’s not a whole of ‘fadeAmount’.

appers this error message : ‘contrain’ was not declared in this scope

My actual code is

const byte Z1 = 11;
const byte Z2 = 10;
byte brightness = 0;
int fadeAmount = 5;

void setup() {

}

void loop() {
  
brightness = contrain(brightness + fadeAmount, 0, 255);

analogWrite(Z1, brightness);
analogWrite(Z2, brightness);

 if (brightness <= 0|| brightness >= 255) {
    fadeAmount = -fadeAmount;
    delay(5000l);
    
  } 
  else {
    delay(90); 


}
}

The code make the led turn on, but not fade

Uhm, oops. Afterthought and made the same typo twice… Should be constrain()

const byte Z1Pin = 11;
const byte Z2Pin = 10;
const byte V1Pin = 9;
const byte V2Pin = 6;
byte brightness = 0;
int fadeAmount = 5;

void setup() {

}

void loop() {
  brightness = constrain(brightness + fadeAmount, 0, 255);
 
  analogWrite(Z1Pin, brightness);
  analogWrite(Z2Pin, brightness);
  analogWrite(V1Pin, brightness);
  analogWrite(V2Pin, brightness);

  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
    delay(60000l); // that is a lower case L on the end of that number
  }
  else{
    delay(60);
  }
}

Now it's working perfectly, thank you so much for helping me.

I have to end this topic? Or just let it be?

FCruzs:
I have to end this topic? Or just let it be?

Largely leave it as is. But you can change the first post and edit the title to start with "[Solved]" or something :slight_smile: