Oiy dunna non-standard state machine.

It has a switch-case followed by other state-operating code.
The switch-case part reads numbers and watches for EOL and entry limit and sets state to run the others that may change the state the switch-case sees next, it only runs when serial is available.

So I got more than a straight switch-case-pedigree state machine when I crossed a non-blocking serial data entry with an index sort example. It reads and sorts 20 ints at 250000 baud. Index limit is 256 using 768 bytes of RAM... maybe use memmove() instead of for-loops to move 40+ indexes.

// 223 -55 87 -126 9999 21000 -863 7512 444 11 -1260 -5078 768 -15 -2555 5 4 3 2 1

const byte maxElements = 20;
byte elements;
char entryState; // 8 bit signed, -128 to 127
byte index[ maxElements ];
int data[ maxElements ];
int accumulate;
char sign;
char ch;

byte i, j, k, flag;

void usage()
{
  Serial.println( F( "Serial input sort example using indexes." ));
  Serial.println( F( "Enter up to 20 whitespace separated ints." ));
  Serial.println( F( "Make sure that your monitor adds newline.\n" ));
}

void show_indexes( byte count )
{
  Serial.print( F( "indexes so far: " ));
  for ( byte x = 0; x <= count; x++ )
  {
    Serial.print( index[ x ] );
    if ( x < count )
    {
      Serial.print( F( "," ));
    }
  }
  Serial.println( F( "\n" ));
}

byte a, b;
int xx, xy;
float ff;
long way;

void setup()
{
  Serial.begin( 250000 );
  usage();

  elements = entryState = 0;
}

void loop()
{
  if ( Serial.available() )
  {
    ch = Serial.read();

    //    Serial.print( F( "state " ));
    //    Serial.println( entryState, DEC );

    switch ( entryState )
    {
      case -1 :
        if (( ch >= '0' ) && ( ch <= '9' ))
        {
          accumulate = ch - '0'; // initial value set
          entryState = 1; // get the rest of the digits
        }
        else
        {
          entryState = 2; // garbage following '-', ignore line
        }
        break;

      case 0 :
        sign = 1;
        if (( ch >= '0' ) && ( ch <= '9' ))
        {
          accumulate = ch - '0'; // initial value set
          entryState = 1; // get the rest of the digits
        }
        else if ( ch == '\n' )
        {
          entryState = 3; // all entries finished
        }
        else if ( ch == '-' )
        {
          sign = -1;
          entryState = -1;
        }
        break;

      case 1 :
        if (( ch >= '0' ) && ( ch <= '9' ))
        {
          if ( accumulate < 3275 ) // max int is 32768
          {
            accumulate *= 10;
            accumulate += ch - '0';
          }
        }
        else
        {
          accumulate *= int( sign );
          data[ elements ] = accumulate;
          elements++;
          if ( ch == '\n' )
          {
            entryState = 10; // last number, all entries finished
          }
          else if ( elements == maxElements )
          {
            entryState = 9; // data array filled, ignore entered chars until newline.
          }
          else
          {
            entryState = 5; // this entry finished
          }
        }
        break;

      case 2 : // looking for newline
        if ( ch == '\n' )
        {
          entryState = 0; // finished, start over
          usage();
        }
        break;
    }

    //      Serial.print( F( "entryState = " ));
    //      Serial.print( entryState, DEC );
    //      Serial.print( F( "  entered = " ));
    //      Serial.print( accumulate );
    //      Serial.print( F( "  elements = " ));
    //      Serial.println( elements );

    if ( entryState > 4 ) // sort the new entry
    {
      //      Serial.print( F( "entryState = " ));
      //      Serial.print( entryState, DEC );
      //      Serial.print( F( "  entered = " ));
      //      Serial.print( accumulate );
      //      Serial.print( F( "  elements = " ));
      //      Serial.println( elements );

      i = elements - 1;
      if ( elements > 0 )
      {
        flag = 0;
        for ( j = 0; j < elements; j++ )
        {
          if ( data[ i ] < data[ index[ j ]] ) // new number is less than indexed value
          {
            // this should be a memmove()
            for ( k = i; k > j; k-- ) // move finished indexes up
            {
              index[ k ] = index[ k - 1 ];
            }
            index[ j ] = i;
            j = elements;
            flag = 1;
          }
          if ( flag == 0 )
          {
            index[ i ] = i;
          }
        }
        Serial.print( accumulate );
        Serial.print( F( " " ));
        show_indexes( i ); // see how the indexes get sorted as each data is added
      }
    }

    if ( entryState > 5 || entryState == 3 ) // report
    {
      Serial.println( F( "Data as entered." ));
      for ( i = 0; i < elements; i++ )
      {
        Serial.print( data[ i ] );
        if ( i < elements - 1 )
        {
          Serial.print( F( "," ));
        }
      }

      Serial.println( F( "\n\nData as sorted." ));
      for ( i = 0; i < elements; i++ )
      {
        Serial.print( data[ index[ i ] ] );
        if ( i < elements - 1 )
        {
          Serial.print( F( "," ));
        }
      }
      Serial.println( F( "\n\n" ));

      elements = 0;
      for ( i = 0; i < maxElements; i++ )
      {
        data[ i ] = 0;
        index[ i ] = 0;
      }
    }

    if ( entryState == 3 || entryState == 5 || entryState == 10 )
    {
      if ( entryState == 10 || entryState == 3 )
      {
        usage();
      }
      entryState = 0;
    }
    else if ( entryState == 9 )
    {
      entryState = 2;
    }

  } // end if ( Serial.available() )
}  // end loop()

// 223 -55 87 -126 9999 21000 -863 7512 444 11 -1260 -5078 768 -15 -2555 5 4 3 2 1

entered: 223 -55 87 -126 9999 21000 -863 7512 444 11 -1260 -5078 768 -15 -2555 5 4 3 2 1

Serial input sort example using indexes.
Enter up to 20 whitespace separated ints.
Make sure that your monitor adds newline.

223 indexes so far: 0

-55 indexes so far: 1,0

87 indexes so far: 1,2,0

-126 indexes so far: 3,1,2,0

9999 indexes so far: 3,1,2,0,4

21000 indexes so far: 3,1,2,0,4,5

-863 indexes so far: 6,3,1,2,0,4,5

7512 indexes so far: 6,3,1,2,0,7,4,5

444 indexes so far: 6,3,1,2,0,8,7,4,5

11 indexes so far: 6,3,1,9,2,0,8,7,4,5

-1260 indexes so far: 10,6,3,1,9,2,0,8,7,4,5

-5078 indexes so far: 11,10,6,3,1,9,2,0,8,7,4,5

768 indexes so far: 11,10,6,3,1,9,2,0,8,12,7,4,5

-15 indexes so far: 11,10,6,3,1,13,9,2,0,8,12,7,4,5

-2555 indexes so far: 11,14,10,6,3,1,13,9,2,0,8,12,7,4,5

5 indexes so far: 11,14,10,6,3,1,13,15,9,2,0,8,12,7,4,5

4 indexes so far: 11,14,10,6,3,1,13,16,15,9,2,0,8,12,7,4,5

3 indexes so far: 11,14,10,6,3,1,13,17,16,15,9,2,0,8,12,7,4,5

2 indexes so far: 11,14,10,6,3,1,13,18,17,16,15,9,2,0,8,12,7,4,5

1 indexes so far: 11,14,10,6,3,1,13,19,18,17,16,15,9,2,0,8,12,7,4,5

Data as entered.
223,-55,87,-126,9999,21000,-863,7512,444,11,-1260,-5078,768,-15,-2555,5,4,3,2,1

Data as sorted.
-5078,-2555,-1260,-863,-126,-55,-15,1,2,3,4,5,11,87,223,444,768,7512,9999,21000

Serial input sort example using indexes.
Enter up to 20 whitespace separated ints.
Make sure that your monitor adds newline.

entered: 223 -55 87 -126 9999 21000 -863 7512 444 11 -1260 -5078 768 -15 -2555 5 4 3 2 1

Please... some of us are sensitive "I have done a non-standard state machine."

...R

Some people learned Pascal. I learned Forth after Basic.

I prefer creative freedom over retentive structure whenever and however suits. Title fixed, hehe.

You might not like my code. It's not comment-rich and could be cleaner. It's amazing the things that get fixed before all but the 99 year bugs are eradicated. This is not worked-over code, may never be.

I just wanted to share something a bit unorthodox. Title cain't be without some curve.

GoForSmoke:
... I learned Forth after Basic. ...

Rock on!

Dunna much about 'lectronix.
Dunna know much pro-gram-ming.
But I do know Are Dween-o.
And I know you luv it too,
What a wonderful world it could be!

Four years ago I couldn't spell Injuneer.
Now I are one.

Tom... :slight_smile:

TomGeorge:
Four years ago I couldn't spell Injuneer.
Now I are one.

Tom... :slight_smile:

You shood lern to rite proper Inglish like wot eye does.

If you make a computer language that allows programmers to write in English,
you will find that computer programmers can't write English.
--- from the old Programmer's Joke Sheet.