Merge Random Flash and Fade Sketches into one

Hello,

Trying to understand and learn how to program my Trinket Pro using Arduino IDE.
This is my first post asking for some assistance.

First, I have been successful in merging two sketches that cause different pairs of leds to randomly flash at different rates.

I would like to combine this merged sketch with another that causes an led to fade-in, fade-out.

I'm sure the solution is something simple, but I believe once I see the solution; this will help me progress in my learning.

Here is my first merged sketch, this one operates how I want.

//This Sketch works for Communication Wall and Main Console Plue Freezing Tubes

//Communication Wall Random Blink Variables Leds 9, 10
//Main Console and Freezing Tubes Variables Leds 11, 12, 13

int CommLeds[2] = {8, 10};
int ConsoleLeds[3] = {11,12,13};

void setup() {
//Void Setup for Communication Wall
for (int jj; jj<sizeof(CommLeds) /sizeof(int);jj++) {
pinMode(CommLeds[jj], OUTPUT);
delay(10);

//void Setup for Main Console and Freezing Tubes
for (int kk; kk < sizeof(ConsoleLeds) / sizeof(int); kk++) {
pinMode(ConsoleLeds[kk], OUTPUT);
delay(10);
}

}
}

void loop() {
//Loop for Communication wall
digitalWrite(CommLeds[random(0, sizeof(CommLeds) / sizeof(int))], HIGH);
delay(random(500, 1000));
digitalWrite(CommLeds[random(0, sizeof(CommLeds) / sizeof(int))], LOW);

//Loop for Main Console and Freezing Tubes
digitalWrite(ConsoleLeds[random(0, sizeof(ConsoleLeds) / sizeof(int))], HIGH);
delay(random(20,200));
digitalWrite(ConsoleLeds[random(0, sizeof(ConsoleLeds) / sizeof(int))], LOW);

}

This sketch is my Fade-In/Fade-Out that I would like to merge into the sketch above.

int ledPin = 9; //LED connected to output digital pin 9

void setup() {
// Nothing happens in setup

}

void loop() {
// fade in from min to max in increments of 1 points:

for (int fadeValue = 20; fadeValue <= 255; fadeValue += 1) {
// Sets the value (range from 0 to 255);

analogWrite(ledPin, fadeValue);
// Wait for 30 milliseconds to see the dimming effect

delay(20);

}

// Fade out from max to min in increments of 5 points;

for (int fadeValue = 255; fadeValue >= 20; fadeValue -= 1){
  // Sets the value (range from 0 to 255);

  analogWrite(ledPin, fadeValue);
  // Wait for 30 milliseconds to see the dimming effect

  delay(20);
}

}

Appreciate any assistance,

Oops

Please remember to use code tags when posting code

All,

Please ignore my previous post, tags were not included in my original posting.

Fresh start...
Reposting with TAGS

Hello,

Trying to understand and learn how to program my Trinket Pro using Arduino IDE.
This is my first post asking for some assistance.

First, I have been successful in merging two sketches that cause different pairs of leds to randomly flash at different rates.

I would like to combine this merged sketch with another that causes an led to fade-in, fade-out.

I'm sure the solution is something simple, but I believe once I see the solution; this will help me progress in my learning.

Here is my first merged sketch, this one operates how I want to

SKETCH 1

//This Sketch works for Communication Wall and Main Console Plue Freezing Tubes

//Communication Wall Random Blink Variables Leds 9, 10
//Main Console and Freezing Tubes Variables Leds 11, 12, 13

int CommLeds[2] = {8, 10};
int ConsoleLeds[3] = {11, 12, 13};

void setup() {
//Void Setup for Communication Wall
for (int jj; jj < sizeof(CommLeds) / sizeof(int); jj++) {
pinMode(CommLeds[jj], OUTPUT);
delay(10);

//void Setup for Main Console and Freezing Tubes
for (int kk; kk < sizeof(ConsoleLeds) / sizeof(int); kk++) {
  pinMode(ConsoleLeds[kk], OUTPUT);
  delay(10);
}

}
}

void loop() {
//Loop for Communication wall
digitalWrite(CommLeds[random(0, sizeof(CommLeds) / sizeof(int))], HIGH);
delay(random(500, 1000));
digitalWrite(CommLeds[random(0, sizeof(CommLeds) / sizeof(int))], LOW);

//Loop for Main Console and Freezing Tubes
digitalWrite(ConsoleLeds[random(0, sizeof(ConsoleLeds) / sizeof(int))], HIGH);
delay(random(20, 200));
digitalWrite(ConsoleLeds[random(0, sizeof(ConsoleLeds) / sizeof(int))], LOW);

}

This next sketch, it my led fade-in/fade-out. I would like to merge this with the sketch shown above.
SKETCH 2
int ledPin = 9; //LED connected to output digital pin 9

void setup() {
// Nothing happens in setup

}

void loop() {
// fade in from min to max in increments of 1 points:

for (int fadeValue = 20; fadeValue <= 255; fadeValue += 1) {
// Sets the value (range from 0 to 255);

analogWrite(ledPin, fadeValue);
// Wait for 30 milliseconds to see the dimming effect

delay(20);

}

// Fade out from max to min in increments of 5 points;

for (int fadeValue = 255; fadeValue >= 20; fadeValue -= 1) {
// Sets the value (range from 0 to 255);

analogWrite(ledPin, fadeValue);
// Wait for 30 milliseconds to see the dimming effect

delay(20);

}

}

Any help is appreciated

No tags.
Still the same "Oops"

post using </> (tell no one)

I thought all I had to do was to Auto Format my code in IDE, the copy and paste.

What am I missing?

Code tags, and initialisers in your for loops

consider


#undef MyHW
#ifdef MyHW
byte ledPins []  = { 10, 11, 12, 13 };
#else
byte ledPins []  = { 9, 8, 10, 11, 12, 13 };
#endif

unsigned long msec;
char          s [80];

void randLeds (void)
{
    static unsigned long msecRandLst;
    static unsigned long msecRandInterval;

    if ( (msec - msecRandLst) > msecRandInterval)  {
        msecRandLst      = msec;
        msecRandInterval = random (100, 1000);

        int pin = ledPins [random (1, sizeof (ledPins))];
        digitalWrite (pin, ! digitalRead (pin));

        static int pinLst = pin;

        sprintf (s, "%s: %6ld msec", __func__, msec);
        Serial.println (s);
    }
}

void fadeLed (void)
{
    static unsigned long msecFadeLst;
    static unsigned long msecFadeInterval = 20;

#define FadeRate 10
    static int           fadeDir   = FadeRate;
    static int           fadeValue = 0;

    if ( (msec - msecFadeLst) > msecFadeInterval)  {
        msecFadeLst = msec;

        analogWrite (ledPins [0], fadeValue);
        fadeValue += fadeDir;

        if (0 >= fadeValue)
            fadeDir =  FadeRate;
        if (255 <= fadeValue)
            fadeDir = -FadeRate;

        sprintf (s, "%s: %6ld msec", __func__, msec);
        Serial.println (s);
    }
}

void loop (void)
{
    msec = millis ();

    fadeLed ();
    randLeds ();
}

void setup () {
    Serial.begin (9600);

    for (unsigned n; n < sizeof (ledPins); n++)
        pinMode (ledPins [n], OUTPUT);
}

The random flashing patterns seems okay. But the fade in/out is not smooth as I originally had it. The led gradually fades up, then will flash at full intensity...fades all the ways down to off. I had it set to fade from 20 to 255.

The random flashing, it looks like all output are randomly flashing at the same rate. I wanted pins 8 and 10 to flash slower and pins 10, 11 and 13 to randomly flash faster.

Was i suppose to fill in some of the variables to make this work?

you should understand the concepts and make it your own

you might consider a different fade interval depending on the value

SKETCH 1: 2 SEPERATE RANDOM FLASHING LED CODE INTO A SINGLE SKETCH
//This Sketch works for Communication Wall and Main Console Plue Freezing Tubes


//Communication Wall Random Blink Variables Leds 8, 10
//Main Console and Freezing Tubes Variables Leds 11, 12, 13

int CommLeds[2] = {8, 10};
int ConsoleLeds[3] = {11, 12, 13};



void setup() {
  //Void Setup for Communication Wall
  for (int jj; jj < sizeof(CommLeds) / sizeof(int); jj++) {
    pinMode(CommLeds[jj], OUTPUT);
    delay(10);

    //void Setup for Main Console and Freezing Tubes
    for (int kk; kk < sizeof(ConsoleLeds) / sizeof(int); kk++) {
      pinMode(ConsoleLeds[kk], OUTPUT);
      delay(10);
    }

  }
}

void loop() {
  //Loop for Communication wall
  
  digitalWrite(CommLeds[random(0, sizeof(CommLeds) / sizeof(int))], HIGH);
  delay(random(500, 1000));
  digitalWrite(CommLeds[random(0, sizeof(CommLeds) / sizeof(int))], LOW);

  //Loop for Main Console and Freezing Tubes
  
 
  digitalWrite(ConsoleLeds[random(0, sizeof(ConsoleLeds) / sizeof(int))], HIGH);
  delay(random(20, 200));
  digitalWrite(ConsoleLeds[random(0, sizeof(ConsoleLeds) / sizeof(int))], LOW);
  
}

SKETCH 2: LED FADE-IN / FADE-OUT

int ledPin = 9; //LED connected to output digital pin 9

void setup() {
  // Nothing happens in setup

}

void loop() {
  // fade in from min to max in increments of 1 points:

  for (int fadeValue = 20; fadeValue <= 255; fadeValue += 1) {
    // Sets the value (range from 20 to 255);

    analogWrite(ledPin, fadeValue);

    // Wait for 20 milliseconds to see the dimming effect
    delay(20);
  }

  // Fade out from max to min in increments of 5 points;

  for (int fadeValue = 255; fadeValue >= 20; fadeValue -= 1) {
    // Sets the value (range from 0 to 255);

    analogWrite(ledPin, fadeValue);
    // Wait for 30 milliseconds to see the dimming effect

    delay(20);
  }

}type or paste code here

Believe I finally got my code posted using TAGS.

I am asking for assistance how to combine these two sketches so that each operates the same.

Still very much Oops.

@vcholman this topic had a rough start, I hope you still are as enthusiastic about Arduino as we are.

The "code-tags" are for the forum, so we can read your sketch.

Some give very short answers and directly pinpoint the problem and others (like me) sometimes apologize for giving such a long reply.

Scroll back to reply #7 by TheMemberFormerlyKnownAsAWOL. What is he telling you ? He is asking to use code-tags on this forum and he is telling you that initializers are missing in your for-loops.

For a simple for-loop, you can use a simple variable, and you can use the same variable over and over again.

for( int i=0; i<10; i++)
{
  // eat 10 apples
}

for( int i=0; i<5; i++)
{
  // eat 5 bananas
}

for( int i=0; i<2; i++)
{
  // drink 2 cups of tea
}

The next step is to read the BlinkWithoutDelay example.

Hello,

I have decided to try combining 2 sketches. The fade-in/fade-put operation is working as expected.

My 3 led random flash code is working, but incredibility slow. If I place my random flash code into a separate sketch, it runs at normal speed.

What am I doing wrong and how do I fix this?

int leds[3] = {11, 12, 13};
int ledPin = 9;

void setup() {
  for (int jj; jj < sizeof(leds) / sizeof(int); jj++) {
    pinMode(leds[jj], OUTPUT);
    delay(10);
  }

}

void loop() {

  digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], HIGH);
  delay(random(20, 200));
  digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], LOW);

  // fade in from min to max in increments of 1 points:

  for (int fadeValue = 20; fadeValue <= 255; fadeValue += 1) {
    // Sets the value (range from 20 to 255);

    analogWrite(ledPin, fadeValue);

    // Wait for 20 milliseconds to see the dimming effect
    delay(20);
  }

  // Fade out from max to min in increments of 5 points;

  for (int fadeValue = 255; fadeValue >= 20; fadeValue -= 1) {
    // Sets the value (range from 0 to 255);

    analogWrite(ledPin, fadeValue);
    // Wait for 20 milliseconds to see the dimming effect

    delay(20);
  }

}

Your fade loops add a lot of delay. Read the advice @Koepel gave you.

How many more times?

How many more times what?

How many more times are you going to post that junk before you fix it?

I told everyone when i first posted that this was all new to me. Was hoping to find users that didn't mind sharing their knowledge.

I am not a programmer and it is not my intentions to offend others with my posting. Just trying to solve some simple code.

Sorry you feel my lack of experience is insulting or my questions are annoying. I was concerned I would run into users as yourself who feel they are smart to be bothered with those who are not skilled with Arduino.

I will close my account and look for help else where.