Cant exit loop. has set number of repeats, but goes on infintely

Hi my program reads in an array and outputs the logic level to a corresponding LED. This produces a sequence and is meant to repeat a set number of times, however it never stops. I'm assuming its stuck in a loop and is not properly exiting.

Im new to programming so go easy. This is my program:

#include <stdlib.h>
#include <stdio.h>


// constants won't change. They're used here to set pin numbers:

const int switch2 = 2; // the number of the pushbutton pin
const int switch3 = 3;
const int switch4 = 4;
const int switch5 = 5;

// the variables that will change:

int switchState2 = 0;  // variable for reading the switch status
int time = 10;
int ledPins[] = {6,7,8,9,10,11,12,13};
int pinCount = 8;

   void setup() {
    // initialize the LED pin as an output:
    for (int column_number = 0; column_number < pinCount; column_number++) {
    pinMode(ledPins[column_number], OUTPUT);
    // initialize the switch pin as an input:
    pinMode(switch2, INPUT);
 }
} 

//when switch 2 is pressed this is the sequence for the leds:
   int array2[16][8] = {{1,0,0,0,0,0,0,0},
                        {1,1,0,0,0,0,0,0},
                        {1,1,1,0,0,0,0,0},
                        {1,1,1,1,0,0,0,0},
                        {1,1,1,1,1,0,0,0},
                        {1,1,1,1,1,1,0,0},
                        {1,1,1,1,1,1,1,0},
                        {1,1,1,1,1,1,1,1},
                        {1,1,1,1,1,1,1,0},
                        {1,1,1,1,1,1,0,0},
                        {1,1,1,1,1,0,0,0},
                        {1,1,1,1,0,0,0,0},
                        {1,1,1,0,0,0,0,0},
                        {1,1,0,0,0,0,0,0},
                        {1,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0}};


void switch2a(int array2[16][8], int MAX_ROW, int repeat)
{
    int row_count = 0;
    int end_of_column = 7;
    
    
    for (int repeat_count = 0; repeat_count < repeat;) 
        for (int column_number = 0; column_number < 8; column_number++) {
            // if the number in the matrix is equal to 1 then the led will turn on
            if (array2[row_count][column_number] == 1) 
            {
                
            
                
    // turn LED on:
        digitalWrite(ledPins[column_number], HIGH);
        delay(time);
        
             
            }
            
            else {
                // turn LED off:
                digitalWrite(ledPins[column_number], LOW);
                delay(time);
                
            }
             
        
            //this checks if it is at the end of the sequence
            if (column_number == end_of_column) {
                column_number = -1;
                row_count++;
            }
            
            if (row_count == MAX_ROW) {
                row_count = 0;
                repeat_count++;
            }
 }
}

void loop()
{ 
  switch2a(array2,16,10);
  
}

As you can see I wanted the sequence to repeat 10 times.

Any help is most appreciated.

for (int repeat_count = 0; repeat_count < repeat;) something missing here?

Auto format is a useful tool in the IDE

Sorry what is IDE?

The IDE is the editor/compiler (Integrated Development Environment)

#include <stdlib.h>
#include <stdio.h>

Now that is something one doesn't normally see in an arduino sketch! :wink:

Lefty

Also, loop() is repeatedly called by the arduino, so you will need to stop calling your switch2a function:

void loop()
{
  static bool looped_already = false;

  if (looped_already == false)
  {
    switch2a(....,10);
    looped_already = true;
  }
}

Or leave loop empty, and call your function at the end of setup.

AndyP93 , you appear not have liked my response in your other thread -

http://arduino.cc/forum/index.php/topic,161713.msg1209394.html#msg1209394

Also to save a lot of precious memory, see my edit in the first post of this thread :slight_smile:

http://arduino.cc/forum/index.php/topic,162122.0.html

Your 128 ints replaced by a single byte :wink:

OP, do you notice any similarity between my first reply to this thread, and the response here?

Can you see why cross-posting WASTES TIME?

DON'T CROSS-POST.

Sorry for cross posting I didn't mean too. I thought as this was a different area of coding it justified a new post.

lloyddean:
AndyP93 , you appear not have liked my response in your other thread -

http://arduino.cc/forum/index.php/topic,161713.msg1209394.html#msg1209394

Thank you for your response but I didnt really get it. There was a lot of things I hadnt seen before, so I didnt feel comfortable going down that route.

Is your focus merely short term and to get your project working and then your interest in the Arduino is fulfilled, or do you plan to do more projects down the line and wish to learn how to program?

This project im doing is short term and I need to complete it soon, but this is by no means a cry for help. I do very much like the arduino and its applications, so yeah I would carry on with it.

My programming is quite poor but I've only been doing this for 2 weeks and prior to this I've had a play around with C.