Storing Changing Variables in Array

This is a discussion on Storing Changing Variables in Array within the C Programming forums, part of the General Programming Boards category; I am trying to store constantly changing variables into a character array,while communicating over a serial port, and I am ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    10

    Storing Changing Variables in Array

    I am trying to store constantly changing variables into a character array,while communicating over a serial port, and I am not getting the consistency that I need.
    Here is the code:

    Code:
    Code:
    char str[4];
    
    if(Serial.available())
    {
    	for(int i=0;i<3;i++)
    	  {
    		  str[i] = Serial.read();
    	   }
    }


    Is this the best way to read in the variables that I need? I am reading multiple variables at a time,using multiple arrays, which could be two to three digits in length each and are sent next to each other as a whole number. Any help would be greatly appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    First of all, if you are reading binary data, you may want to declare your buffer as unsigned char.

    Even though your buffer is 4 chars big, you are only reading 3 values into it. Is this intentional?

    Is this the best way to read in the variables that I need?
    That depends. Does the Serial class have a function which can read more than 1 byte at a time?

    I am reading multiple variables at a time,using multiple arrays
    What do you mean by this? Are you using multiple threads?

    We may need a little more information about what you are attempting to do in order to help further.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    Quote Originally Posted by bithub View Post
    First of all, if you are reading binary data, you may want to declare your buffer as unsigned char.

    Even though your buffer is 4 chars big, you are only reading 3 values into it. Is this intentional?

    That depends. Does the Serial class have a function which can read more than 1 byte at a time?

    What do you mean by this? Are you using multiple threads?

    We may need a little more information about what you are attempting to do in order to help further.
    I am only reading up to 3 values into the buffer.
    The Serial class only reads 1 byte at a time.
    I am using more than one character array to store the different variables which are displayed in a row as one single number.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    So the values you are reading are numbers? How are the numbers sent across, is it binary or ascii? How are the numbers separated when they are sent across? In other words, how do you know you've reached the end of a number?

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    Quote Originally Posted by bithub View Post
    So the values you are reading are numbers? How are the numbers sent across, is it binary or ascii? How are the numbers separated when they are sent across? In other words, how do you know you've reached the end of a number?
    The values are numbers which are ascii. They are not separated and so I set a limit of three numbers per variable. After that, I was able to determine which numbers were two digits by using:
    Code:
    if(str[2]>0) // if the number is 3 digits or above 100
    I also used strcmp() in order to determine actions within certain points of a particular variable:
    Code:
    char small[] = "130";
    char large[] = "145";
    if(strcmp(str,small)>0 && strcmp(str,large)<0)  // Set the window between 130 and 145

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Ok, I'm going to assume the numbers are separated by a null byte. If that's not the case, then you have a problem.

    It sounds like you want to have a function that just gets you a number.
    Code:
    int get_number(void)
    {
        char buffer[16];
        int i;
        while(i < sizeof(buffer))
        {
            buffer[i] = Serial.read();
            if(buffer[i] == '\0')
                break; // We reached the end of the number
            i++;
        }
        if(i == sizeof(buffer))
            // Handle error case where we never encountered a null byte
        
        // Now convert the buffer and return it
        return atoi(buffer);
    }
    This will help to encapsulate the serial handling code from the rest of your application.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    That worked great, thanks. Now how would I move on to the next number if I was able to space each variable? Or should I read all variables into one array and then sort them?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If the goal is to sort the values, then you have a few options:

    1) Place the values in an array, and sort them as you insert them in.
    2) Place the values in an array, and sort them once you've gotten all the values.
    3) Place the values in a linked list, and sort them as you get them.

    Number 1 is the simplest to code.
    Number 2 is a little faster than number 1 since you can use a more complex sorting algorithm.
    Number 3 is best if you don't know how many values you are getting since lists are variable size whereas arrays are fixed size.

    All in all, it's all about which solution makes the most sense to you based on how you want your application to run.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    Thank you very much for your responses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing an array of structures?
    By Sparrowhawk in forum C Programming
    Replies: 4
    Last Post: 12-15-2008, 02:07 AM
  2. Changing size of array of pointers
    By carrotcake1029 in forum C Programming
    Replies: 1
    Last Post: 11-15-2008, 09:24 PM
  3. Transforming array variables
    By ben2000 in forum C Programming
    Replies: 28
    Last Post: 08-02-2007, 02:00 PM
  4. Trouble storing file input in array
    By difficult.name in forum C Programming
    Replies: 1
    Last Post: 10-10-2004, 11:54 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21