Trying to make ALL my LEDs breath in and out.

Hello, i am able to make ONE LED light breath…however, I do not know to set set it for ALL of them to breathe.

This is my code:
int ledPin={1,2,3,4,5,6,7,8,9,10,11,12,13};

void setup()
{
for (int i =0; i <13; i++)
{
pinMode(ledPins*,OUTPUT);*

  • }*
    }
    void loop(){
    fadeOn(1000,5);
    fadeOff(1000,5);
    }
    void fadeOn(unsigned int time,int increament){
    for (int i = 0 ; i < 13; i+=increament){
    analogWrite(ledPin, i);
    delay(time/(255/5));
    }
    }
    void fadeOff(unsigned int time,int decreament){
    for (int i = 13; i>0; i-=decreament){
    analogWrite(ledPin, i);
    delay(time/(255/5));
    }
    }
    Can someone please redo the code for me?

For a start, not all pins are PWM. If you have an Uno (you didn't say), you can only analogWrite() on pins 3, 5, 6, 9, 10 & 11.

Is part of your code really in italics or did you forget the advice given in read this before posting a programming question about using code tags when posting code here ?

When you post code that doesn't compile please also post the error messages that tell you what is wrong with it.

E.g. there is ledPin vs ledPins. And (ledPin,i) is not how you address one element from an array. In your fade functions you've confused looping round the pins in your array and looping round decrementing or incrementing the brightness...you need both"

It might also help if you post the code you have that successfully makes one LED "breath".

Steve

“ int ledPin={1,2,3,4,5,6,7,8,9,10,11,12,13};”

Also, it’s never a good idea using pins 1 and 2 as they are used for serial communications.

You mean pin 0 and 1?

(deleted)

MorganS:
You mean pin 0 and 1?

Oh no, 50% right :frowning:

Oh no 50% wrong :frowning:

slipstick:
When you post code that doesn’t compile please also post the error messages that tell you what is wrong with it.

E.g. there is ledPin vs ledPins. And (ledPin,i) is not how you address one element from an array. In your fade functions you’ve confused looping round the pins in your array and looping round decrementing or incrementing the brightness…you need both"

It might also help if you post the code you have that successfully makes one LED “breath”.

Steve

This is what made ONE LED breath…only one

int ledPin = 10;
void setup() {
pinMode(ledPin,OUTPUT);
}
void loop(){
fadeOn(1000,5);
fadeOff(1000,5);
}
void fadeOn(unsigned int time,int increament){
for (byte value = 0 ; value < 255; value+=increament){
analogWrite(ledPin, value);
delay(time/(255/5));
}
}
void fadeOff(unsigned int time,int decreament){
for (byte value = 255; value >0; value-=decreament){
analogWrite(ledPin, value);
delay(time/(255/5));
}
}

This is my code with the code effect…Im new and i dont know how it works so im sorry :frowning:

int ledPin[]={1,2,3,4,5,6,7,8,9,10,11,12,13};


void setup()
{
  for (int i =0; i <13; i++)
  {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop(){
fadeOn(1000,5);
fadeOff(1000,5);
}
void fadeOn(unsigned int time,int increament){
for (int i = 0 ; i < 13; i+=increament){
analogWrite(ledPin, i);
delay(time/(255/5));
}
}
void fadeOff(unsigned int time,int decreament){
for (int i = 13; i>0; i-=decreament){
analogWrite(ledPin, i);
delay(time/(255/5));
}
}

This has an LED on 3 and 5.

//
// millisgammafade01p5
//

extern const uint8_t gamma[];
byte led;       // the PWM pin the LED is attached to
int brightness;    // how bright the LED is
int corrected;     // gamma comp'd pick from PROGMEM table

unsigned long markTime;
unsigned long runningTime;
const unsigned long limit = 5UL;  // "speed"
bool change_it;

bool alternate;

void setup()
{
  pinMode(led, OUTPUT);
}

void loop()
{
  alternate = false;
  for (brightness = 31; brightness < 248; brightness ++)
  {
    makeithappen();
  }
  
  for (brightness = 248; brightness > 31; brightness --)
  {
    makeithappen();
  }

  alternate = true;
  for (brightness = 31; brightness < 248; brightness ++)
  {
    makeithappen();
  }
  
  for (brightness = 248; brightness > 31; brightness --)
  {
    makeithappen();
  }
}

void makeithappen ()
{
  if(alternate == false)
  {
    led = 3;
  }
  else
  {
    led = 5;
  }
  change_it = false;
  markTime = millis();
  runningTime = millis();
  while((millis() - markTime) < limit)
  {
    if (change_it == false)
    {
      corrected = pgm_read_byte(&gamma[brightness]);
      analogWrite(led, corrected);
      change_it = true;
    }
    runningTime = millis();
  }
}

const uint8_t PROGMEM gamma[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255};

array - Arduino Reference will show you how to use arrays.

In your fadeOn/fadeOff functions you will need two for loops, one to increment/decrement the analogWrite value and an inner one to write the value to each LED. Something like:

  for (byte value = 0 ; value <= 255; value += increament) {
    for (int i = 0; i < 13 ; i++) {
      analogWrite(ledPin[i], value);
    }
  }

You used that format for getting to all the LEDs in setup() so I’m not sure why you didn’t see that it was needed here too. Anyway I’ll let you sort out any delays you need and the differences for fadeOff.

Steve

slipstick:
array - Arduino Reference will show you how to use arrays.

In your fadeOn/fadeOff functions you will need two for loops, one to increment/decrement the analogWrite value and an inner one to write the value to each LED. Something like:

  for (byte value = 0 ; value <= 255; value += increament) {

for (int i = 0; i < 13 ; i++) {
      analogWrite(ledPin[i], value);
    }
  }



You used that format for getting to all the LEDs in setup() so I'm not sure why you didn't see that it was needed here too. Anyway I'll let you sort out any delays you need and the differences for fadeOff.

Steve

how do i add this to my whole project though? im a freshman student and our teacher took the whole month off and we have a sub who doesnt teach us at all and im so lost and so is everyone in the class. Can you add it into my actual code please?

(deleted)

spycatcher2k:
Nope, thats not how YOU learning works.

not when our entire class has no teacher and no language experience and a sub that just sits on his phone the whole time LOL. I cant find anything on it like youtube either which sucks

(deleted)

Because I'm in a good mood I'll give you a little more help. The 5 lines of code I gave you last time should replace the 5 lines of code following 'void fadeOn(unsigned int time,int increament){' in your code.

Then you need to do the same thing in void fadeOff with a few changes to make it decrement instead of incrementing.

Give it a try and if you still have trouble post your latest code and describe exactly what happens when you run it. We'll help but you have to put some effort in yourself.

Steve