4

I've got a textbox displaying the sketch edited by the software, with variables being changed dynamically.

https://cdn.discordapp.com/attachments/398780553731506176/468835565304020995/unknown.png

But for the buttons array, I need to use some for loops to write the numbers

The format needs to be like this

byte buttons[NUMROWS][NUMCOLS] = { {0,1,2,3,4,}, {5,6,7,8,9,}, {10,11,12,13,14,},

But all I can manage so far is

byte buttons[NUMROWS][NUMCOLS] = { {0,1,2,3,4,}, {1,2,3,4,5,}, {2,3,4,5,6,},

I need to advance the loop so that the numbers increase. I'm using two nested for loops

        int i;
        for(int x = 0; x < rows; x++) //row
        {
            string buttonbyte = "{";
            for (i = x; i < columns + x; i++) //column
            {
                buttonbyte += i;
                buttonbyte += ",";
            }
            sketch[9 + x] = buttonbyte + "},";
        }

The code is for a program that edits an arduino .ide sketch and uploads it, for ease of use.

Any help would be greatly appreciated!

Cheers, Morgan

2
  • Can you use List<byte> instead of array? Commented Jul 17, 2018 at 18:15
  • I'm not sure, it's for the Keypad library. It requires the use of an two dimensional array. Commented Jul 17, 2018 at 18:17

2 Answers 2

4

I think your second loop needs to be

for (i = (x * columns); i < ((x + 1) * columns); i++)

This will be 0,1,2,3,4 for the first iteration and 5,6,7,8,9 for the second and so on.

Sign up to request clarification or add additional context in comments.

1 Comment

This implies lesser changes than my solution @MorGuux use this
0

If the jump is always this you just need to make it so that

 int i;
        for(int x = 0; x < rows; x++) //row
        {
            string buttonbyte = "{";
            for (i = 0; i < columns; i++) //column
            {
                buttonbyte += (i + columns*x); // column + columns * row
                buttonbyte += ",";
            }
            sketch[9 + x] = buttonbyte + "},";
        }

PS: This might not work as i don't have a compiler now and you might need to convert (i + columns*x) to a string

4 Comments

Yeah that works, but it's one number too high. The output is like {0,1,2,3,4,}, {6,7,8,9,10,}, {12,13,14,15,16,},
Make i start from zero everytime.
You would need to rewrite the second loop for this one to work to not include the X, "for (i = 0; i < columns; i++)" would work if you are building the number with (i + columns * x)
Yes i just noticed that... my bad. The problem of not having a compiler in hand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.