Noob seeks helps with void functions.

Hi guys,

ill preface this with i’m pretty new to arduino and coding so bare with my ignorance! I’ve put together some code to do run some lighting shows. I’ve had it running but as ive added to it ive run into a problem. The basic project is running 5 individual led strips, x2 of which run one pattern and x3 of which run another pattern simultaneously. Unless a button is pressed and then it switches to the same groups of strips just running different patterns.

I had this set up with just 2 strips and it was fine but now ive added the extra strips, one set of patterns (void fireMode) it one runs the pattern once then ends but (voidRgb) runs perfectly fine. The loops is running continuously as i’ve checked with a serial.print. Just the code of the actual pattern seems to cease. I also commented out the other effects to try it with just one but it made no difference. It may also help to know im using a switch function for the change in pattern, which i may add more cases to later.

Ill post my code and if theres a more efficient way of doing what i’m trying to do then i’m all ears. I havnt tried it yet, but id like to try an interrupt for the switch, as during the same sets of patterns i’m having issues with, a switch press isn’t noticed until one loop has been completed. Although i’m not sure thats neccessary?

Code is attached because of character limit.
Thanks in Advance!

Case_lighting_with_cycler.ino (9.13 KB)

The keyword "void" placed before a function name specifies that the function does not return a parameter.

I see plenty of delay statements in your code. The computer does nothing else while it is delaying, so that is a major problem.

Please study the "blink without delay" example that comes with the Arduino IDE to learn how to avoid that problem.

Thanks for your reply, yeah i think i understand it a bit better now in that context…

so the unsigned long currentMillis needs to be in the loop so it keeps a perpetual count and then the unsigned long previousMillis gets updated everytime you use it and is just compared to its previous state and then the value you actual want, so if the time has already elapsed it can procede through the code?

Also just for a sanity check is this how i would implement that? Assuming that all my variables are declared etc…

void EnergyTrailsUp()
{
    tempSensorValue = analogRead(THERMAL_PIN);
  if (tempSensorValue <50){
    dimTime = (25);
    ledSpeed = 80;
}
 else if (tempSensorValue >=50 && tempSensorValue <200){
    dimTime = (tempSensorValue/4);
    ledSpeed = 70;
}
  else if (tempSensorValue >=200 && tempSensorValue <500){
    dimTime = (tempSensorValue/2);
    ledSpeed = 60;
}
  else {
    dimTime = (350);
    ledSpeed = 50;
}
  
  for (int i=0; i<= UP_VERTICAL_NUM_LEDS; i++) {

     upVerticalLeds[i] = CRGB( 255, 128, 0);
     
     FastLED.show();

     if (currentMillis - previousMillis >= 10){
      previousMillis = currentMillis;

      upVerticalLeds[i] -= CRGB( 0, 64, 0);
     }
     
       FastLED.show();
     
      if (currentMillis - previousMillis >=ledSpeed) {
        previousMillis = currentMillis;
     
     fadeToBlackBy(upVerticalLeds, UP_VERTICAL_NUM_LEDS, dimTime);
      }
  }
}

Also just for a sanity check is this how i would implement that?

The loop function loops. Your function will NOT have a for loop in it. It will check the current time against the last time something happened, to decide if it is time to do something. If it is, it will determine what to do, based on global or static variable values.

In the 40 minutes between the last two posts, you can not have spent any serious time working out how YOU (NOT the Arduino) would do what needs to be done. So, take that time now.

Take a look at Using millis() for timing. A beginners guide

Thanks for all your guys, all solid advice especially UKHeliBobs tutorial. So for the benefit of future viewers of this thread, read the above advice, take your time (no pun intended) and try and understand whats actually happening with your code.

Also use Serial.print…

Ill also include the revised code for people to look at it to compare the before and afters, and also for further constructive criticism and advice.

Thanks Again

Case_lighting_with_cycler.ino (11.4 KB)