Arduino Analog and Digital Pin scanning permutation

Hello to all.

I see a similar question for my case on this page
Arduino Permutation
and answered by Rob Tillaart with this code.

//    FILE: perm1.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2010-11-23
// PUPROSE: demo permutations
char permstring[12] = "123456789A";

void permutate(int n)
  if (n==0) // end reached print the string
    for (int i = 0; i < strlen(permstring); i++)

  for (int i = 0; i <n; i++) 
    // swap
    char t = permstring[i];
    permstring[i] = permstring[n-1];
    permstring[n-1] = t;
    // permutate substrings
    // swap back
    t = permstring[i];
    permstring[i] = permstring[n-1];
    permstring[n-1] = t;

void setup()
  unsigned long time = millis();
  time = millis() - time;
  Serial.print("TIME: ");

void loop()

In my case, I use 01234 and 23456789 number for the input value char permstring permutation function, which is used for scanning Arduino analog pins A0 - A4, and D2-D9 digital pins.

My question is:
How do temporarily exit from the permutation function, after one number combination is generated.
for example i use two permutation function, and the result is:

  1. result_one = 23140
  2. result_two = 891234567

and then, this two number is used in another function to scan/test the analog or digital pin, for finding the correct LCD data pin configuration (unknown datasheet LCD, usualy from phones) . In this case LCD is use 8 bit paralel configuration.

let's say the correct pin combination for the scan function to match the LCD should be:
for Analog = 01234
for Digital = 891234567

If the result number is not the correct value, the scan function call or go back to the permutation function again to continue/resume again without starting over again from the input string (skip combination that already used).
This process is repeated until the correct combination is found.

this is my sketch with other permutation code,
but similar function (only scan analog pin)

// Modified from Adafruit_TFTLCD library
#include <Adafruit_TFTLCD_Modified.h> 

#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0

#define TFTWIDTH   240   // lcd pixel width
#define TFTHEIGHT  320  // lcd pixel height


uint16_t identifier = 0;

byte val[5]={0,1,2,3,4};
int i=0,j=0,k=0,l=0,m=0;
byte a,b,c,d,e;

void scan_LCD(){
	for (i=0;i<5;i++) {
		for (j=0;j<5;j++){
			if(j!=i) { 
				for(k=0;k<5;k++) {
					if((k!=j)&&(k!=i)) {
						for(l=0;l<5;l++) { 
								for(m=0;m<5;m++) { 
									if((m!=l)&&(m!=k)&&(m!=j)&&(m!=i)) {
										tft.mappingPin(a, b, c, d, e);
										identifier = tft.readID();
									  if ((identifier == 0x9325) || (identifier == 0x9328) || (identifier == 0x7575)
										 || (identifier == 0x0345) || (identifier == 0x0045) || (identifier == 0x0745) 
										 || (identifier == 0x4545) || (identifier == 0x9341) || (identifier == 0x8357)
										 || (identifier == 0x7735)) 
											  Serial.print  (F("LCD ID has found = 0x"));
											  Serial.println(identifier, HEX);
											  Serial.println(F("LCD clock pin = RD, WR, RS, CS, RST"));
											  Serial.print  (F("PIN matching  ="));
											  Serial.print (" A"); Serial.print(a,DEC);
											  Serial.print(", A"); Serial.print(b,DEC);
											  Serial.print(", A"); Serial.print(c,DEC);
											  Serial.print(", A"); Serial.print(d,DEC);
											  Serial.print(", A"); Serial.println(e,DEC);
											  Serial.println(F(" "));
											  return 0;
										 } else {		// unknown ID or not listed in library
											  identifier = 0;

void setup()
  Serial.println(F("Read ID scanning"));
  Serial.println(F(" "));
	//Function to mapping and scan correct lcd pin configuration
	//LCD chip type
  if(identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if(identifier == 0x0345 || identifier == 0x0045 ) {
    Serial.println(F("Found HX8347D low power , will use HX8347G LCD driver"));
  } else if(identifier == 0x0745 || identifier == 0x4545 ) {
    Serial.println(F("Found HX8347D, will use HX8347G LCD driver"));  
  } else if(identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if(identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else if(identifier == 0x7735) {
    Serial.println(F("Found ST7735 LCD driver"));
  } else {
		Serial.print(F("No Match LCD ID"));
		while(1);	//stop the program
  Serial.println(F(" "));
  Serial.print(F("Screen fillscreen test"));

void loop()


Thanks to anybody who want to help give me feedback
Sorry for my bad english
Daniel from Indonesia

This is handled differently in real code.


 index permutation algorithm

and poke around a bit. There are algorithms to go directly from an index to a corresponding permutation.

Then you can call such a function with a number N and get the Nth permutation like reading it right off a list, without the necessity of keeping track of where you were, or suspending and resuming an algorithm that goes one by one from the first to the last.

You can get arbitrary permutations in any order you want.

Your code looks like lotsa fun, but would be a smallish nightmare to accommodate your use case.