I'm trying to tidy some very basic but bulky code

Hello all,
I’m hoping someone can help me with to tidy up some very basic C+ code for an Arduino Mega based project.

I have 2 rows of LEDs – 15 LEDs in both row, using Pins 22-51 as Outputs. I have written some very basic code to create 3 separate patterns / loops(). Each separate code works fine (on an emulator).

The patterns are just different versions of turning on and off LEDS as needed.

An example is
<void loop() {
// single LED chase
digitalWrite(22, HIGH);
delay(200); // Wait for 200 millisecond(s)

digitalWrite(22, LOW);
digitalWrite(24, HIGH);
delay(200); // Wait for 200 millisecond(s)

digitalWrite(24, LOW);
digitalWrite(26, HIGH);
delay(200); // Wait for 200 millisecond(s)

digitalWrite(26, LOW);
digitalWrite(27, HIGH);
delay(200); // Wait for 200 millisecond(s)

.

.

.

digitalWrite(27, LOW);
digitalWrite(25, HIGH);
delay(200); // Wait for 200 millisecond(s)

digitalWrite(25, LOW);
digitalWrite(23, HIGH);
delay(200); // Wait for 200 millisecond(s)

digitalWrite(23, LOW);
delay(200); // Wait for 200 millisecond(s)

}

etc…

If I put all the patterns in one loop, the code will be very long, especially if I want to run Pattern #1 three times, run Pattern #2 once, run Pattern #3 four times, or whatever.

I remember from years ago that actual loop() code to run the patterns; can be put at the bottom of the code and ‘pulled’ in when needed but I can’t remember how to do it (or the correct terminology)

I think the layout should look

void setup()
{
pinModes:
}

void loop()
{
Run Pattern #1
Run 3 times
Run Pattern #2
Run 8 times
Run Pattern #3
Run 10 times
Run Pattern #2
Run 2 times
}

{
Code for Pattern #1
}
{
Code for Pattern #2
}
{
Code for Pattern #3
}

I'm sure the brackets are in the wrong places here.

Thanks in advance.

edit your post. put the code between CODE tags
for often repeating lines of code, especially if something get bigger consequent you can using FOR cycle, like:

for (int i = 22; i<52; i++){
  digitalWrite(i, HIGH);
  delay(200);
  digitalWrite(i, LOW);
}

this will iterate i from 22 to 51.

second, you may using functions:

void cycleIncrement(){ // name it properly
  for (int i = 22; i<52; i++){
    digitalWrite(i, HIGH);
    delay(200);
    digitalWrite(i, LOW);
  }
}
void cycleDecrement(){
  for (int i = 51; i>21; i--){
    digitalWrite(i, HIGH);
    delay(200);
    digitalWrite(i, LOW);
  }
}

The word you are looking for is "functions"

loop is a function. setup is a function. They're called from a function called main. Those functions can call other functions that you write the same way.

Google "C++ functions" and you will learn what you need.

When you get this working, look at arrays and millis() timing for the next version.

You probably don’t need a MEGA, but could use shift registers or other hardware strategy to interface to all the LEDs with three wires… dion’s forget your resistors and power supply!

Hi Kolaha, Thank you. I'll definately play around with increments and give them a try.

@Delta_G. Thanks, Functions are exactly what I was looking for.

Hi Lastchancename. Thanks for the feedback. If I get the functions working I'll have a look at arrays and millis()

Hello kolaha,

I can get the increments to work perfectly (on a wokwi project) but when I try and use functions, the code will compile but won't turn on / off the LEDs. I'm probably doing something very basic wrong.

If you have couple of minutes, would you mind having a look at the wokwi project as this has the code - it should be public

Thanks in advance

Mick

Try instead of int main()

void loop() {
  cycleIncrement(); // call the function
  cycleDecrement();
  // return 0; no return from a function returning void
}

HTH

a7

@alto777 , thank you. I knew it would be something simple. appreciate your help.

I just noticed and removed the return statement, which is an error even if it only gives a warning.

a7

Loops!

void setup() 
{
  for (byte ii = 22; ii <= 51; ii++)
    pinMode(ii, OUTPUT);
}

HTH

a7

Thanks, that is a much tidier version of the setup.

I removed the return 0; but it didn't seem to make difference if it there or not, but I'll leave it out.

suggest you post your latest code ... to show others that you implemented teir suggestions correctly or to show what changes don't work

If you are talking about this:

…then the void versus int change is also important.