assigning values from serial.read to an array

After getting away for a while with some very naive code to receive serial data, I am now doing it properly :slight_smile:

Nick Gammons very helpful explanation Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking
works fine, when I send from one arduino to the other.

The sketch as below prints out 10 numbers as predicted.

I dont understand the code in the switch statements, but what I need to do is to simply allocate each number to buf[10] .

I can then use the array for logging.

It looks simple but the code has confused me.

// Example of receiving numbers by Serial
// Author: Nick Gammon
// Date: 31 March 2012

const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter   = '>';

void setup ()
  { 
  Serial.begin (115200);
  Serial.println ("Starting ...");
  } // end of setup
  
void processNumber (const long n)
  {
  Serial.println (n);
  }  // end of processNumber
  
void processInput ()
  {
  static long receivedNumber = 0;
  static boolean negative = false;
  
  byte c = Serial.read ();
  
  switch (c)
    {
      
    case endOfNumberDelimiter:  
      if (negative) 
        processNumber (- receivedNumber); 
      else
        processNumber (receivedNumber); 

    // fall through to start a new number
    case startOfNumberDelimiter: 
      receivedNumber = 0; 
      negative = false;
      break;
      
    case '0' ... '9': 
      receivedNumber *= 10;
      receivedNumber += c - '0';
      break;
      
    case '-':
      negative = true;
      break;
      
    } // end of switch  
  }  // end of processInput
  
void loop ()
  {
  
  if (Serial.available ())
    processInput ();
    
  // do other stuff here
  } // end of loop

I dont understand the code in the switch statements

It's pretty simple, really. There is a case for the numbers 0 to 9, in which the current value is multiplied by 10 and the new character, converted to a number, is added to the current value. There are cases for the start and end markers, and for the minus sign.

but what I need to do is to simply allocate each number to buf[10] .

Allocate is the process of creating a memory location, or array of memory locations. Probably not what you really mean, here. It is unlikely that you want to assign any value to buf[10], if 10 is the size of the array.

You need to allocate that array, and an index into the array, initialized to 0. When you have a value to save, in the first case, save the value and increment the index.

Thanks Paul,
A nights sleep helped too.

The first number is the equipment ID number which I will always make > 99, to sync the message ( and the buffer now 11 locations ) so I got this working as below

I will not be using negative numbers so I ditched that bit

void processNumber (const long n)
{
  if ( n >99 ) { 
    buffcounter =0 ; 
  }
 
  buf [buffcounter] = n;

  Serial.print (" buff ");  
  Serial.print (buffcounter); 
  Serial.print ("     =  "); 
  Serial.println ( buf [buffcounter ]);


  buffcounter ++;  
  if ( buffcounter > 10 ) { 
    buffcounter = 0 ;
  }

If your array has ten elements, the last usable element will be element 9.

Your very last bit of code there is wrong. You increment buffcounter, and then
reset it to zero, IF it is bigger than 10.

Which means when it is 9, you will increment it to 10, and you won't reset it, and
on the next pass through the function you will access array element [10], which won't
be right.

You need to reset your counter to 0 as soon as you get to ten, not the cycle after that.

I put in an extra element as an ID and message start signal, so there are 11 now.