Calling code from outside loop or even the sketch.

Hey there. Not completely new to this coding thing but it's been a while since I have really put my head into it. Also, pretty new to arduino. Have C experience from like 15 years ago...

So here is what I am doing.
I am building a lighting system for my bicycle. Headlights, tail lights, signals, and break light. I have everything in the loop and it is working well but the code is a lot or the same stuff repeated over and over again. The repeated code is what I am referring to as the "program" for each light. I am starting with 3 main programs I can switch between for each group of lights. Program 0 = off, program 1 = solid on, program 2 = normal blink with no delay, and program 4 = fast blink with no delay.

What I want to do.
I want to take the code for the programs and pull them out so they don't need to be written in the loop but each light group can run them, sometimes 3 lights individually triggered to the program but running them at the same time.

Is this something that can be done and if so can someone point me in the right direction for research or even a bit of code to get me on the right path. I just don't know the term I am looking for.

Here is a sample of the code I want to split out so it can be used for all lights.

// Checking the program and setting the lights to do stuff
TailcurrentMillis = millis();
if (TailProgram == 1) {
if (TailcurrentMillis - TailpreviousMillis >= NormalBlinkSpeed) {
TailpreviousMillis = TailcurrentMillis;
if (TailLightState == LOW) {
TailLightState = HIGH;
} else {
TailLightState = LOW;
}
digitalWrite(TailLight, TailLightState);
}
} else if (TailProgram == 2) {
if (TailLightState == LOW)
TailLightState = HIGH;
else
TailLightState = LOW;
digitalWrite(TailLight, TailLightState);
} else {
digitalWrite(TailLight, LOW);
}

Thank you in advance and stay safe people.

I just don't know the term I am looking for.

"function"

There must be an event that calls or enables a specific subroutine. The event may be a keypress or pin change, whatever you like.

A function can simply be called in loop and this will be called every time the loop occurs. It can also be called subject to conditions such as in an if statement. You can take everything outside the simple sketch and put it in tabs and reference it like a library. To do this look up library in the reference at the top of this page.

Another thing you will probably want to use are arrays. Luckily those are documented under “array” in the list of data types in the online language documentation on this site. But as a quick sample, you can have arrays to hold multiple tail light variables, for example:

byte TailLightPin[] = {3, 7, 9};
byte TailLightState[] = {false, false, false};
...
for (i = 0; i < 3; i++)
{
 TailcurrentMillis[i] = millis();
  if (TailProgram[i] == 1) {
    if (TailcurrentMillis[i] - TailpreviousMillis[i] >= NormalBlinkSpeed[i]) {
      TailpreviousMillis[i] = TailcurrentMillis[i];
      if (TailLightState[i] == LOW) {...

will “plug in” the value of i for each tail light. The for() loop above performs the same actions with tail light #0, 1, 2. It’s an oversimplification but this is generally how it works.

Thanks for all the different solutions to my problem. I can't wait to dive down this rabbit hole and see which one works best for this instance. Keep em coming.

jasonkew79:
Thanks for all the different solutions to my problem.

Well, I would call what I gave you more of, "a suggestion"... Considering the "my problem" that you mention, actually the nature of the problem has not been fully explained. Although you did explain some stuff in your first post, it lacks a concrete context because the full code has not been provided. It is the norm to do so in cases such as yours. If I'm not mistaken, you will need help integrating a solution into your code.

The best thing you could do right now, is do some digging based on what has been posted, and try some simple test sketches to try out some concepts. If they work, move the working parts to a development sketch. If you have problems with it, post here for help.

I tried to quote my original code but it wouldn't let me. After a couple of posts I figured that I wasn't giving all the info. I need to look into how to post something with code in it that goes over the 9000 whatever it is limit (number could be wrong). I just wanted to try and thank all who have helped me find some road markings to follow cause google maps can't help me here. If you feel so inclined to help me with posting my entire code I would love the help but if not I am sure I can figure it out (I hope).

oh and as for the giving me different solutions comment, that was meant to be to everyone. I honestly have no idea how to navigate this message forum. Give me a bit of drowning in it and I'll get the idea. Slow off the start but once past the gate I just get faster, or at least I did when I was in my 20's.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

If you have greater than 9000 characters you need to attach things as a file. However it is usually better not to have >9000 characters! Look at breaking your code up as pointed to earlier and then you will end up with multiple sketches which all compile and any one is a manageable size and easy to post in code tags. Your main sketch should have no problems because you only put stuff in it that has been debugged and works. If you find integrating something causes a problem then start simplifying it until you have a minimal working example that demonstrates the problem. Remember all these things should, at the very least, compile so people can help you. It really helps with the organisation this way too as you don’t end up with massive, unreadable, complex code with random ‘needle in haystack’ type problems.

Maybe have a look at Planning and Implementing a Program

…R

9000 characters of code just to control three lights? Do you have ten lines of comments for each line of code?