[FIXED] Code works with delay() but not with millis()?

Hello,

I am building a day/night system for my modelrailroad. Now I made a RGB Controller for simulating day/night. But I got 12 pins left on the Arduino.
So I thought to made a lightning add-on. I used SoftPWM. I don’t have problems in the rest of my code with TimeAction and SoftPWM. But the author wrote that millis() (TimedAction) gives problems with SoftPWM.

Is it possible to change the Timer that SoftPWM used so I don’t have any problems more? Or an alternative to the small delay in my code?

#include <SoftPWM_timer.h>
#include <SoftPWM.h>
#include <TimedAction.h>

// ArduSPI_slave RGB Controller V0.0.4

////////////////////////////////////////////////////////////////////
/// ONLY the following line needs to be changed for each slave !!!!
#define THIS_SLAVE 1
//// end of slave specifics
////////////////////////////////////////////////////////////////////

#define SLAVE_ID THIS_SLAVE
#define SELECT_PIN 2
#define ACK_PIN 3
#define LED_PIN 13
#define RED_PIN 5
#define GREEN_PIN 4
#define BLUE_PIN 6 

char data;
boolean day = false; // Day process OFF
boolean night = false; // Night process OFF
boolean lightning = false; // Lightning process OFF
boolean ledON[] = {
  false,false,false,false,false,false,false,false,false,false,false,false,false}; //number of LED's that needs to be controlled!
int lightningLED[] = {
  7,8,9,10,11,12,13,14,15,16,17,18 };
int nbLightningLED = 12;
int factor = 1; // With the variable 'factor' we can dim the RGB LED's.
int red = 230;
int green = 230;
int blue = 75;
int redValue = 255;
int greenValue = 255;
int blueValue = 255;

TimedAction RGBTimer = TimedAction(100,RGB);
TimedAction lightningStrikeTimer = TimedAction(random(500,1250),lightningStrike);


void setup(){
  Serial.begin(9600);
  while(!Serial){
    ;
  } // necessary on some Arduinos, may as well keep it.
  pinMode(SELECT_PIN, INPUT); // we will read from the 'select' pin
  pinMode(ACK_PIN, OUTPUT); // and write to the ACK pin
  digitalWrite(ACK_PIN, LOW); // init it to NACK
  SoftPWMBegin();
  pinMode(LED_PIN, OUTPUT); // set LED_PIN to writing
  digitalWrite(LED_PIN, LOW);

  pinMode(RED_PIN, OUTPUT); // setup RED_PIN for SoftPWM
  SoftPWMSet(RED_PIN, 0);
  SoftPWMSetFadeTime(RED_PIN, 4000, 4000);

  pinMode(GREEN_PIN, OUTPUT); // setup GREEN_PIN for SoftPWM
  SoftPWMSet(GREEN_PIN, 0);
  SoftPWMSetFadeTime(GREEN_PIN, 4000, 4000);

  pinMode(BLUE_PIN, OUTPUT); // setup BLUE_PIN for SoftPWM
  SoftPWMSet(BLUE_PIN, 0);
  SoftPWMSetFadeTime(BLUE_PIN, 4000, 4000);

  for(int i; i <= nbLightningLED; i++)
  {
    pinMode(lightningLED[i],OUTPUT);
    SoftPWMSet(lightningLED[i],255);
    SoftPWMSetFadeTime(lightningLED[i], 150, 350);
  }
  randomSeed(A5);

}

void loop(){
  // all we do is get some data and do what it says
  // if there is data, do something, otherwise loop
  if(getData()){
    doSomethingWithData();
  }
  if(night == true) // Repeats until night = false
  {
    redValue = 50;
    greenValue = 25;
    blueValue = 125;
    RGBTimer.check();
  }
  if(day == true) // Repeats until day = false
  {
    redValue = 230;
    greenValue = 230;
    blueValue = 75;
    RGBTimer.check();
  }
  if(lightning == true) // Repeats until lightning = false
  {
    redValue = 10;
    greenValue = 10;
    blueValue = 30;
    lightningStrikeTimer.check();
    RGBTimer.check();
  }
  else
  {
    for(int pinArrayIndexLightning; pinArrayIndexLightning <= nbLightningLED; pinArrayIndexLightning++)
    {
    SoftPWMSet(lightningLED[pinArrayIndexLightning],255);
    
  }
}
}
boolean getData(){
  // if not selected, ensure ACK_PIN is LOW and do nothing!
  if(digitalRead(SELECT_PIN)==LOW){
    digitalWrite(ACK_PIN,LOW);
    return false;
  }
  // if we got here, the the SELECT_PIN is HIGH,
  // so we need to read serial
  // first wait for something to be available to read
  while(!Serial.available()){
    ;
  }
  // then read 1 byte into the 'data' variable
  while(Serial.readBytes(&data,1) == 0) {
    ;
  }
  // ACK the serial read
  digitalWrite(ACK_PIN,HIGH);
  return true;
}

// just to demonstrate that the slave is doing something
// change the LED state at every reception of data.
void doSomethingWithData(){
  switch(data){
  case 'A': // Day
    night = false;
    lightning = false;
    day = true;
    break;
  case 'B': // Night
    day = false;
    lightning = false;
    night = true;
    break;
  case 'C': // Lightning
    day = false;
    night = false;
    lightning = true;
    break;
  }
}
void RGB() // this function controls the RGB LEDstrip.
{
  if(red <= redValue)
  {
    red = red + 5;
    SoftPWMSet(RED_PIN,red); 
  }
  else
  {
    red = red - 5;
    SoftPWMSet(RED_PIN,red);
  }

  if(green <= greenValue)
  {
    green = green + 5;
    SoftPWMSet(GREEN_PIN,green); 
  }
  else
  {
    green = green - 5;
    SoftPWMSet(GREEN_PIN,green);
  }

  if(blue <= blueValue)
  {
    blue = blue + 5;
    SoftPWMSet(BLUE_PIN,blue); 
  }
  else
  {
    blue = blue - 5;
    SoftPWMSet(BLUE_PIN,blue);
  }
}

void lightningStrike()
{
  int pinArrayIndexLightning = random(0,nbLightningLED); // Choose randomly an output from the digital pins (D7 -> D18) every time we call this function.
  int brightness  = random(0,50);
  
    SoftPWMSet(lightningLED[pinArrayIndexLightning],brightness);
    delay(100);
    SoftPWMSet(lightningLED[pinArrayIndexLightning],255);
}

Thanks in advance,
Dylan

 for(int i; i <= nbLightningLED; i++)

Start counting at some arbitrary point, and count one more than elements in your array?
Fix that first.

#include <SoftPWM_timer.h>
#include <SoftPWM.h>
#include <TimedAction.h>

// ArduSPI_slave RGB Controller V0.0.4

////////////////////////////////////////////////////////////////////
/// ONLY the following line needs to be changed for each slave !!!!
#define THIS_SLAVE 1
//// end of slave specifics
////////////////////////////////////////////////////////////////////

#define SLAVE_ID THIS_SLAVE
#define SELECT_PIN 2
#define ACK_PIN 3
#define LED_PIN 13
#define RED_PIN 5
#define GREEN_PIN 4
#define BLUE_PIN 6 

char data;
boolean day = false; // Day process OFF
boolean night = false; // Night process OFF
boolean lightning = false; // Lightning process OFF
boolean ledON[] = {
  false,false,false,false,false,false,false,false,false,false,false,false,false}; //number of LED's that needs to be controlled!
int lightningLED[] = {
  7,8,9,10,11,12,13,14,15,16,17,18 };
int nbLightningLED = 12;
int factor = 1; // With the variable 'factor' we can dim the RGB LED's.
int red = 230;
int green = 230;
int blue = 75;
int redValue = 255;
int greenValue = 255;
int blueValue = 255;

TimedAction RGBTimer = TimedAction(100,RGB);
TimedAction lightningStrikeTimer = TimedAction(random(500,1250),lightningStrike);


void setup(){
  Serial.begin(9600);
  while(!Serial){
    ;
  } // necessary on some Arduinos, may as well keep it.
  pinMode(SELECT_PIN, INPUT); // we will read from the 'select' pin
  pinMode(ACK_PIN, OUTPUT); // and write to the ACK pin
  digitalWrite(ACK_PIN, LOW); // init it to NACK
  SoftPWMBegin();
  pinMode(LED_PIN, OUTPUT); // set LED_PIN to writing
  digitalWrite(LED_PIN, LOW);

  pinMode(RED_PIN, OUTPUT); // setup RED_PIN for SoftPWM
  SoftPWMSet(RED_PIN, 0);
  SoftPWMSetFadeTime(RED_PIN, 4000, 4000);

  pinMode(GREEN_PIN, OUTPUT); // setup GREEN_PIN for SoftPWM
  SoftPWMSet(GREEN_PIN, 0);
  SoftPWMSetFadeTime(GREEN_PIN, 4000, 4000);

  pinMode(BLUE_PIN, OUTPUT); // setup BLUE_PIN for SoftPWM
  SoftPWMSet(BLUE_PIN, 0);
  SoftPWMSetFadeTime(BLUE_PIN, 4000, 4000);

  for(int i; i < nbLightningLED; i++)
  {
    pinMode(lightningLED[i],OUTPUT);
    SoftPWMSet(lightningLED[i],255);
    SoftPWMSetFadeTime(lightningLED[i], 150, 350);
  }
  randomSeed(A5);

}

void loop(){
  // all we do is get some data and do what it says
  // if there is data, do something, otherwise loop
  if(getData()){
    doSomethingWithData();
  }
  if(night == true) // Repeats until night = false
  {
    redValue = 50;
    greenValue = 25;
    blueValue = 125;
    RGBTimer.check();
  }
  if(day == true) // Repeats until day = false
  {
    redValue = 230;
    greenValue = 230;
    blueValue = 75;
    RGBTimer.check();
  }
  if(lightning == true) // Repeats until lightning = false
  {
    redValue = 10;
    greenValue = 10;
    blueValue = 30;
    lightningStrikeTimer.check();
    RGBTimer.check();
  }
  else
  {
    for(int pinArrayIndexLightning; pinArrayIndexLightning <= nbLightningLED; pinArrayIndexLightning++)
    {
    SoftPWMSet(lightningLED[pinArrayIndexLightning],255);
    
  }
}
}
boolean getData(){
  // if not selected, ensure ACK_PIN is LOW and do nothing!
  if(digitalRead(SELECT_PIN)==LOW){
    digitalWrite(ACK_PIN,LOW);
    return false;
  }
  // if we got here, the the SELECT_PIN is HIGH,
  // so we need to read serial
  // first wait for something to be available to read
  while(!Serial.available()){
    ;
  }
  // then read 1 byte into the 'data' variable
  while(Serial.readBytes(&data,1) == 0) {
    ;
  }
  // ACK the serial read
  digitalWrite(ACK_PIN,HIGH);
  return true;
}

// just to demonstrate that the slave is doing something
// change the LED state at every reception of data.
void doSomethingWithData(){
  switch(data){
  case 'A': // Day
    night = false;
    lightning = false;
    day = true;
    break;
  case 'B': // Night
    day = false;
    lightning = false;
    night = true;
    break;
  case 'C': // Lightning
    day = false;
    night = false;
    lightning = true;
    break;
  }
}
void RGB() // this function controls the RGB LEDstrip.
{
  if(red <= redValue)
  {
    red = red + 5;
    SoftPWMSet(RED_PIN,red); 
  }
  else
  {
    red = red - 5;
    SoftPWMSet(RED_PIN,red);
  }

  if(green <= greenValue)
  {
    green = green + 5;
    SoftPWMSet(GREEN_PIN,green); 
  }
  else
  {
    green = green - 5;
    SoftPWMSet(GREEN_PIN,green);
  }

  if(blue <= blueValue)
  {
    blue = blue + 5;
    SoftPWMSet(BLUE_PIN,blue); 
  }
  else
  {
    blue = blue - 5;
    SoftPWMSet(BLUE_PIN,blue);
  }
}

void lightningStrike()
{
  int pinArrayIndexLightning = random(0,nbLightningLED); // Choose randomly an output from the digital pins (D7 -> D18) every time we call this function.
  int brightness  = random(0,50);
  
    SoftPWMSet(lightningLED[pinArrayIndexLightning],brightness);
    delay(100);
    SoftPWMSet(lightningLED[pinArrayIndexLightning],255);
}

Small problem fixed but not the real problem.
Somebody who knows a solution?

Dylan

for(int i; i < nbLightningLED; i++)

Start counting at some arbitrary point…
I think I’ve mentioned this.

Sorry my english isn’t so good so I thought you mean this: <= to this <.

for(int i; i < nbLightningLED; i++)

Doe you mean this then:

for(int i = 0; i < nbLightningLED; i++)

Dylan

Yup, that's what I mean

Tried it but it doesn't fix it.

Dylan

Tried something different.
I set a TimedAction Timer to set the pins back to LOW.
I add it in the loop and not in the function which turns ON the LED’s.

#include <SoftPWM_timer.h>
#include <SoftPWM.h>
#include <TimedAction.h>

// ArduSPI_slave RGB Controller V0.0.4

////////////////////////////////////////////////////////////////////
/// ONLY the following line needs to be changed for each slave !!!!
#define THIS_SLAVE 1
//// end of slave specifics
////////////////////////////////////////////////////////////////////

#define SLAVE_ID THIS_SLAVE
#define SELECT_PIN 2
#define ACK_PIN 3
#define LED_PIN 13
#define RED_PIN 5
#define GREEN_PIN 4
#define BLUE_PIN 6 

char data;
boolean day = false; // Day process OFF
boolean night = false; // Night process OFF
boolean lightning = false; // Lightning process OFF
boolean ledON[] = {
  false,false,false,false,false,false,false,false,false,false,false,false,false}; //number of LED's that needs to be controlled!
int lightningLED[] = {
  7,8,9,10,11,12,13,14,15,16,17,18 };
int nbLightningLED = 12;
const int TIMES = 6;
const int BETWEEN = 2952;
const int DURATION = 7;
int red = 230;
int green = 230;
int blue = 75;
int redValue = 255;
int greenValue = 255;
int blueValue = 255;
unsigned long lastTime = 0;
int ONTime = 0;
int OFFTime = 0;
int pinArrayIndexLightning = 0;

TimedAction RGBTimer = TimedAction(100,RGB);
TimedAction lightningStrikeTimer = TimedAction(random(500,1250),lightningStrike);
TimedAction lightningStrikeDelayTimer = TimedAction(random(77,1664),lightningStrikeDelay);

void setup(){
  Serial.begin(9600);
  while(!Serial){
    ;
  } // necessary on some Arduinos, may as well keep it.
  pinMode(SELECT_PIN, INPUT); // we will read from the 'select' pin
  pinMode(ACK_PIN, OUTPUT); // and write to the ACK pin
  digitalWrite(ACK_PIN, LOW); // init it to NACK
  SoftPWMBegin();
  pinMode(LED_PIN, OUTPUT); // set LED_PIN to writing
  digitalWrite(LED_PIN, LOW);

  pinMode(RED_PIN, OUTPUT); // setup RED_PIN for SoftPWM
  SoftPWMSet(RED_PIN, 0);
  SoftPWMSetFadeTime(RED_PIN, 4000, 4000);

  pinMode(GREEN_PIN, OUTPUT); // setup GREEN_PIN for SoftPWM
  SoftPWMSet(GREEN_PIN, 0);
  SoftPWMSetFadeTime(GREEN_PIN, 4000, 4000);

  pinMode(BLUE_PIN, OUTPUT); // setup BLUE_PIN for SoftPWM
  SoftPWMSet(BLUE_PIN, 0);
  SoftPWMSetFadeTime(BLUE_PIN, 4000, 4000);

  for(int i; i <= nbLightningLED; i++)
  {
    pinMode(lightningLED[i],OUTPUT);
    SoftPWMSet(lightningLED[i],255);
    SoftPWMSetFadeTime(lightningLED[i], 150, 350);
  }
  randomSeed(A5);

}

void loop(){
  // all we do is get some data and do what it says
  // if there is data, do something, otherwise loop
  if(getData()){
    doSomethingWithData();
  }
  if(night == true) // Repeats until night = false
  {
    redValue = 50;
    greenValue = 25;
    blueValue = 125;
    RGBTimer.check();
  }
  if(day == true) // Repeats until day = false
  {
    redValue = 230;
    greenValue = 230;
    blueValue = 75;
    RGBTimer.check();
  }
  if(lightning == true) // Repeats until lightning = false
  {
    redValue = 10;
    greenValue = 10;
    blueValue = 30;
    lightningStrikeTimer.check();
    lightningStrikeDelayTimer.check();
    RGBTimer.check();
  }
  else
  {
    for(int pinArrayIndexLightning; pinArrayIndexLightning <= nbLightningLED; pinArrayIndexLightning++)
    {
    SoftPWMSet(lightningLED[pinArrayIndexLightning],255);
    
  }
}
}
boolean getData(){
  // if not selected, ensure ACK_PIN is LOW and do nothing!
  if(digitalRead(SELECT_PIN)==LOW){
    digitalWrite(ACK_PIN,LOW);
    return false;
  }
  // if we got here, the the SELECT_PIN is HIGH,
  // so we need to read serial
  // first wait for something to be available to read
  while(!Serial.available()){
    ;
  }
  // then read 1 byte into the 'data' variable
  while(Serial.readBytes(&data,1) == 0) {
    ;
  }
  // ACK the serial read
  digitalWrite(ACK_PIN,HIGH);
  return true;
}

// just to demonstrate that the slave is doing something
// change the LED state at every reception of data.
void doSomethingWithData(){
  switch(data){
  case 'A': // Day
    night = false;
    lightning = false;
    day = true;
    break;
  case 'B': // Night
    day = false;
    lightning = false;
    night = true;
    break;
  case 'C': // Lightning
    day = false;
    night = false;
    lightning = true;
    break;
  }
}
void RGB() // this function controls the RGB LEDstrip.
{
  if(red <= redValue)
  {
    red = red + 5;
    SoftPWMSet(RED_PIN,red); 
  }
  else
  {
    red = red - 5;
    SoftPWMSet(RED_PIN,red);
  }

  if(green <= greenValue)
  {
    green = green + 5;
    SoftPWMSet(GREEN_PIN,green); 
  }
  else
  {
    green = green - 5;
    SoftPWMSet(GREEN_PIN,green);
  }

  if(blue <= blueValue)
  {
    blue = blue + 5;
    SoftPWMSet(BLUE_PIN,blue); 
  }
  else
  {
    blue = blue - 5;
    SoftPWMSet(BLUE_PIN,blue);
  }
}

void lightningStrike()
{
  pinArrayIndexLightning = random(0,nbLightningLED); // Choose randomly an output from the digital pins (D7 -> D18) every time we call this function.
  int brightness  = random(0,50);
    // adjust timing params
    lastTime += ONTime;
    ONTime = random(BETWEEN);

    for (int i=0; i< random(TIMES) && (millis() - ONTime > lastTime); i++)
    {
      SoftPWMSet(lightningLED[pinArrayIndexLightning],brightness);
    }
  }

void lightningStrikeDelay()
{
  SoftPWMSet(lightningLED[pinArrayIndexLightning],255);
}

Dylan

  for (int i=0; i< random(TIMES) && (millis() - ONTime > lastTime); i++)

What are you trying to do here with the comparison?

Oh that's an older code :confused: I changed it just after I posted it sorry...

Dylan

minitreintje:
Oh that's an older code :confused: I changed it just after I posted it sorry...

Okay, so we're debugging what code then?

Oh sorry… But I said the problem was fixed :stuck_out_tongue:

Dylan

#include <SoftPWM_timer.h>
#include <SoftPWM.h>
#include <TimedAction.h>

// ArduSPI_slave RGB Controller V0.0.4

////////////////////////////////////////////////////////////////////
/// ONLY the following line needs to be changed for each slave !!!!
#define THIS_SLAVE 1
//// end of slave specifics
////////////////////////////////////////////////////////////////////

#define SLAVE_ID THIS_SLAVE
#define SELECT_PIN 2
#define ACK_PIN 3
#define LED_PIN 13
#define RED_PIN 5
#define GREEN_PIN 4
#define BLUE_PIN 6 

char data;
boolean day = false; // Day process OFF.
boolean night = false; // Night process OFF.
boolean lightning = false; // Lightning process OFF.
boolean lightningLEDIsON = false; // All lightning LED's are OFF.
boolean ledON[] = {
  false,false,false,false,false,false,false,false,false,false,false,false,false}; //number of LED's that needs to be controlled!
int lightningLED[] = {
  7,8,9,10,11,12,13,14,15,16,17,18 };
int nbLightningLED = 12;
const int TIMES = 5;
const int BETWEEN = 2952;
const int DURATION = 7;
int red = 230;
int green = 230;
int blue = 75;
int redValue = 255;
int greenValue = 255;
int blueValue = 255;
unsigned long lastTime = 0;
int ONTime = 0;
int OFFTime = 0;
int pinArrayIndexLightning = 0;

TimedAction RGBTimer = TimedAction(100,RGB);
TimedAction lightningStrikeONTimer = TimedAction(random(800,900),lightningStrikeON);
TimedAction lightningStrikeOFFTimer = TimedAction(random(928,1053),lightningStrikeOFF);
TimedAction lightningStrikeChooseTimer = TimedAction(random(708,767),lightningStrikeChoose);

void setup(){
  Serial.begin(9600);
  while(!Serial){
    ;
  } // necessary on some Arduinos, may as well keep it.
  pinMode(SELECT_PIN, INPUT); // we will read from the 'select' pin
  pinMode(ACK_PIN, OUTPUT); // and write to the ACK pin
  digitalWrite(ACK_PIN, LOW); // init it to NACK
  SoftPWMBegin();
  pinMode(LED_PIN, OUTPUT); // set LED_PIN to writing
  digitalWrite(LED_PIN, LOW);

  pinMode(RED_PIN, OUTPUT); // setup RED_PIN for SoftPWM
  SoftPWMSet(RED_PIN, 0);
  SoftPWMSetFadeTime(RED_PIN, 4000, 4000);

  pinMode(GREEN_PIN, OUTPUT); // setup GREEN_PIN for SoftPWM
  SoftPWMSet(GREEN_PIN, 0);
  SoftPWMSetFadeTime(GREEN_PIN, 4000, 4000);

  pinMode(BLUE_PIN, OUTPUT); // setup BLUE_PIN for SoftPWM
  SoftPWMSet(BLUE_PIN, 0);
  SoftPWMSetFadeTime(BLUE_PIN, 4000, 4000);

  for(int i; i <= nbLightningLED; i++)
  {
    pinMode(lightningLED[i],OUTPUT);
    SoftPWMSet(lightningLED[i],255);
    SoftPWMSetFadeTime(lightningLED[i], 150, 175);
  }
  randomSeed(A5);

}

void loop(){
  // all we do is get some data and do what it says
  // if there is data, do something, otherwise loop
  if(getData()){
    doSomethingWithData();
  }
  if(night == true) // Repeats until night = false
  {
    redValue = 50;
    greenValue = 25;
    blueValue = 125;
    RGBTimer.check();
  }
  if(day == true) // Repeats until day = false
  {
    redValue = 230;
    greenValue = 230;
    blueValue = 75;
    RGBTimer.check();
  }
  if(lightning == true) // Repeats until lightning = false
  {
    redValue = 10;
    greenValue = 10;
    blueValue = 30;
    RGBTimer.check();
    lightningStrikeChooseTimer.check();
    for(int i; i < random(TIMES); i++)
    {
    lightningStrikeONTimer.check();
    }
    if(lightningLEDIsON == true)
    {
    lightningStrikeOFFTimer.check();
    }
  }
  else
  {
    for(int pinArrayIndexLightning; pinArrayIndexLightning <= nbLightningLED; pinArrayIndexLightning++)
    {
    SoftPWMSet(lightningLED[pinArrayIndexLightning],255);
    
  }
}
}
boolean getData(){
  // if not selected, ensure ACK_PIN is LOW and do nothing!
  if(digitalRead(SELECT_PIN)==LOW){
    digitalWrite(ACK_PIN,LOW);
    return false;
  }
  // if we got here, the the SELECT_PIN is HIGH,
  // so we need to read serial
  // first wait for something to be available to read
  while(!Serial.available()){
    ;
  }
  // then read 1 byte into the 'data' variable
  while(Serial.readBytes(&data,1) == 0) {
    ;
  }
  // ACK the serial read
  digitalWrite(ACK_PIN,HIGH);
  return true;
}

// just to demonstrate that the slave is doing something
// change the LED state at every reception of data.
void doSomethingWithData(){
  switch(data){
  case 'A': // Day
    night = false;
    lightning = false;
    day = true;
    break;
  case 'B': // Night
    day = false;
    lightning = false;
    night = true;
    break;
  case 'C': // Lightning
    day = false;
    night = false;
    lightning = true;
    break;
  }
}
void RGB() // this function controls the RGB LEDstrip.
{
  if(red <= redValue)
  {
    red = red + 5;
    SoftPWMSet(RED_PIN,red); 
  }
  else
  {
    red = red - 5;
    SoftPWMSet(RED_PIN,red);
  }

  if(green <= greenValue)
  {
    green = green + 5;
    SoftPWMSet(GREEN_PIN,green); 
  }
  else
  {
    green = green - 5;
    SoftPWMSet(GREEN_PIN,green);
  }

  if(blue <= blueValue)
  {
    blue = blue + 5;
    SoftPWMSet(BLUE_PIN,blue); 
  }
  else
  {
    blue = blue - 5;
    SoftPWMSet(BLUE_PIN,blue);
  }
}

void lightningStrikeChoose()
{
 pinArrayIndexLightning = random(0,nbLightningLED); // Choose randomly an output from the digital pins (D7 -> D18) every time we call this function. 
}

void lightningStrikeON()
{
  int brightness  = random(0,50);
    // adjust timing params
    lastTime += ONTime;
    ONTime = random(BETWEEN);
    SoftPWMSet(lightningLED[pinArrayIndexLightning],brightness);
    lightningLEDIsON = true;
  }

void lightningStrikeOFF()
{
  for(int pinArrayIndexLightning; pinArrayIndexLightning <= nbLightningLED; pinArrayIndexLightning++)
    {
    SoftPWMSet(lightningLED[pinArrayIndexLightning],255);
    }
   lightningLEDIsON = false;
}

This is the good code BTW.

Dylan

int lightningLED[] = {
  7,8,9,10,11,12,13,14,15,16,17,18 };
int nbLightningLED = 12;
....
for(int i; i <= nbLightningLED; i++)
....
for(int pinArrayIndexLightning; pinArrayIndexLightning <= nbLightningLED; pinArrayIndexLightning+

This is the good code BTW

No, it really isn’t.