A little help adding to sketch

Hi all,

Trying add additional LEDS to my existing sketch. On the micro board Id like to use all 8 PWM outputs which is 13,12,11,10,9,6,5,4. As you can see the sketch Ive been given has only 6 outputs. Ive tried to just copy and paste plus make the needed changes for the extra LEDs but it keeps getting kicked back.
Any help with this so much appreciated.
Thanks.

/*

  This script flashes 6 LEDs (yellow, of course) in random order at random intervals using PWM.  Each LED is controled by its own function.

*/


int led1 = 3;    // LED connected to PWM pin 3, etc. I used all 6 PWM pins.
int led2 = 5;
int led3 = 6;
int led4 = 9;
int led5 = 10;
int led6 = 11;


long randnum;      // randnum controls the time interval between flashes and randbug controls which bug lights up.
long randbug;


void setup()  {
  pinMode(led1, OUTPUT);     //Setting all PWM pins as outputs.
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led6, OUTPUT);
}

void loop() {
  randbug = random(3, 12);                //randbug randomly chooses a function to execute, thus randomly chooses a bug to light up.

  if (randbug == 3) {
    bug1();
  }
  if (randbug == 5) {
    bug2();
  }
  if (randbug == 6) {
    bug3();
  }
  if (randbug == 9) {
    bug4();
  }
  if (randbug == 10) {
    bug5();
  }
  if (randbug == 11) {
    bug6();
  }
}

void bug1() {
  randnum = random(10, 2000);
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {     //each of these functions work the same way.  for loops increase then decrease the output of that pin to control LED brightness.
    analogWrite(led1, fadeValue);                                        //randnum is a random time interval between 10 and 3000 ms and chooses a time interval between bug flashes.
    delay(10);                                                           //delay 10 is just for the fade effect.
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(led1, fadeValue);
    delay(10);
  }
  delay (randnum);
}

void bug2() {
  randnum = random(10, 3000);
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(led2, fadeValue);
    delay(10);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(led2, fadeValue);
    delay(10);
  }
  delay (randnum);
}

void bug3() {
  randnum = random(10, 3000);
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(led3, fadeValue);
    delay(10);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(led3, fadeValue);
    delay(10);
  }
  delay (randnum);
}

void bug4() {
  randnum = random(10, 3000);
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(led4, fadeValue);
    delay(10);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(led4, fadeValue);
    delay(10);
  }
  delay (randnum);
}

void bug5() {
  randnum = random(10, 3000);
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(led5, fadeValue);
    delay(10);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(led5, fadeValue);
    delay(10);
  }
  delay (randnum);
}

void bug6() {
  randnum = random(10, 2000);
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(led6, fadeValue);
    delay(10);
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(led6, fadeValue);
    delay(10);
  }
  delay (randnum);
}

Please provide the exact error message. Include it within a set of code tags. Thanks.

1 Like
int led1 = 3;    // LED connected to PWM pin 3, etc. I used all 6 PWM pins.
int led2 = 5;
int led3 = 6;
int led4 = 9;
int led5 = 10;
int led6 = 11;

I'm missing something. The pin numbers you list and use don't match.

Post the sketch where you tried to add two pins and two functions.

There are some 2000 figures in some and 3000 in others, are those typos?

It is probably possible to copy/paste/edit some more to extend the functionality, but this is a bad track. Better you should slow down and learn how one function can be told which LED to blink.

Who gave you this code? It has quite a bit of room for being very shorter… the fact that you already have 6 nearly identical functions is a clue that whoever gave it to you is being cruel or knows not too much about programming.

It might be a good exercise, I think a better use of your time would be to find a better learning source that whoever/whatever dropped this "gift" in your lap.

a7

/*

  This script flashes 6 LEDs (yellow, of course) in random order at random intervals using PWM.  Each LED is controled by its own function.

*/

const int ledCount = 6;
const int led[ledCount] = {3,5,6,9,10,11};    // LED connected to PWM pin 3, etc. I used all 6 PWM pins.
const unsigned long delayTime[ledCount] = {2000,3000,3000,3000,3000,2000};

void setup()  {
  for (int l=0; l<ledCount; l++)
    pinMode(led[l], OUTPUT);     //Setting all PWM pins as outputs.
}

void loop() {
  int randbug = random(ledCount);                //randbug randomly chooses a function to execute, thus randomly chooses a bug to light up.

  bug(led[randbug], delayTime[randbug]);
}

void bug(int pin, unsigned long maxDelay) {
  unsigned long randnum = random(10, maxDelay);
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {     //each of these functions work the same way.  for loops increase then decrease the output of that pin to control LED brightness.
    analogWrite(pin, fadeValue);                                        //randnum is a random time interval between 10 and 3000 ms and chooses a time interval between bug flashes.
    delay(10);                                                           //delay 10 is just for the fade effect.
  }
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(pin, fadeValue);
    delay(10);
  }
  delay (randnum);
}

Ok Thank you.
The code was done for me on this forum and it actually works perfect. Just wanted to add those 2 additional LEDs. I know I need to learn this for myself but I only have a few days a year to work on projects using my arduino and what Ive learned the year before is vaporized from my memory.

Im confused as well. But the code works.
Thanks for your feed back.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.