Go Down

Topic: Can't Seem To Figure Out the Problem? (Read 186 times) previous topic - next topic

DovahBear

Hey! I've been working with Arduino for a few weeks but I've stumbled across a problem I can't seem to solve.

What I'm trying to do is input values into an array through the Serial Monitor.

The problem is that the index values of the array are radically inaccurate. In short; I've set up a FOR loop to go through the array and fill it but the value of i is jumping to odd numbers.

Here's the code:

Code: [Select]

int BTArray[] = {}; //This will store the FULL array of commands

void setup() {
  Serial.begin(9600);
  Serial.print("-- Started --");
}

boolean Break = false; //Ignore this for now

unsigned int integerValue = 0; // Max value is 65535
char incomingByte;

void loop() {

  for (int i = 0 ; Break == false ; i++)
  {
    while (Serial.available() && Break == false)
    {

      integerValue = 0; // throw away previous integerValue
      while (1) //CREDIT FOR PART OF THIS CODE: JAMES LEWIS @ https://www.baldengineer.com/arduino-multi-digit-integers.html
      {
          // force into a loop until '\n' is received
          incomingByte = Serial.read();
          if (incomingByte == '\n') break;   // exit the while(1), we're done receiving
          if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
          integerValue *= 10;  // shift left 1 decimal place
          // convert ASCII to integer, add, and shift left 1 decimal place
          integerValue = ((incomingByte - 48) + integerValue);
      }

      if (integerValue == 404) //If 404 is entered, break.
      {
        Break = true;
        break;
      }

      else
      {
        BTArray[i] = integerValue;
        Serial.print("\nIndex: ");
        Serial.print(i);
        Serial.print(" value: ");
        Serial.print(integerValue);
        delay(100);
      }

    }
  }
}


And some sample inputs:
(The user inputs are the values. I've simply inputted 0, 1, 2, 3, 4 and 5.

Code: [Select]

-- Started --
Index: 27072 value: 0
Index: 18212 value: 1
Index: -22173 value: 2
Index: -17462 value: 3
Index: 14389 value: 4
Index: -21792 value: 5


As can be seen, instead of the array index starting at 0 then increment slowly by 1, every iteration, the index actually starts at 27072 (in this case) and the random numbers go on.

Any help??

KeithRB

I have no idea bout the loop indexes, but you have created a zero sized array and are clobbering memory.

noweare

#2
May 02, 2017, 03:50 am Last Edit: May 02, 2017, 04:05 am by noweare
While nothing is available to read your loop is running &  incrementing i like crazy but not
really doing anything.

Start with simpler code and just get the reading of the input into the array working. Forget everything else.  when you get that working add the next steps.

You can test a lot of this on any windows based compiler without having to burn it to your aduino to make troubleshooting go faster.


Delta_G

Code: [Select]
int BTArray[] = {}; //This will store the FULL array of commands

No, it is a zero size array.  It won't store anything without clobbering some other memory location.  You have to give it a size.
If at first you don't succeed, up - home - sudo - enter.

Robin2

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The examples store the incoming data into a char array.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

DovahBear

#5
May 02, 2017, 09:02 am Last Edit: May 02, 2017, 09:55 am by DovahBear
While nothing is available to read your loop is running &  incrementing i like crazy but not
really doing anything.

Start with simpler code and just get the reading of the input into the array working. Forget everything else.  when you get that working add the next steps.

You can test a lot of this on any windows based compiler without having to burn it to your aduino to make troubleshooting go faster.


Currently trying to re-write the code but with no luck.

Code: [Select]
int BTArray[] = {}; //This will store the FULL array of commands

No, it is a zero size array.  It won't store anything without clobbering some other memory location.  You have to give it a size.
Defining a size for the filled array didn't affect how the value of i is being perceived.

DovahBear

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The examples store the incoming data into a char array.

...R
I've used the code to receive multiple characters using start and end markings:

When the users enters 'X', they will stop the loop.

Code: [Select]

const byte numChars = 128;
char receivedChars[numChars];

int BTArray[] = {}; //This will store the FULL array of commands
int IndexNumber = 0;
boolean Break = false;

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    if (Break == false)
    {
      recvWithStartEndMarkers();
      showNewData();
    }

    if (Break == true)
    {     
        Serial.print("DONE");
    }
   
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false)
    {
        rc = Serial.read();

        if (recvInProgress == true)
        {
            if (rc != endMarker)
            {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars)
                {
                    ndx = numChars - 1;
                }
            }
            else
            {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker)
        {
            recvInProgress = true;
        }

        else if (rc == 'X')
        {
          Break == true; loop();
        }
    }
}

void showNewData() {
    if (newData == true) {

        if (Break == false)
        {
            Serial.print("\nIndex: ");
            Serial.println(IndexNumber);
            Serial.print(" with value: ");
            Serial.println(receivedChars);
   
            BTArray[IndexNumber] = receivedChars;
           
            IndexNumber++;
            newData = false;
        }
    }
}


Problem is that the array is only being filled up to 5 times before the Arduino resets:

Code: [Select]

<Arduino is ready>

Index: 0
 with value: 5

Index: 1
 with value: 4

Index: 2
 with value: 6

Index: 3
 with value: 2

Index: 4
 with value: 1
<Arduino is ready>


I put in the values 5, 4, 6, 2, 1, and 7.
The <7> wasn't recognized and the arduino reset. So the array will restart from 0. Any help with this?

Whandall

Code: [Select]
int BTArray[] = {}; //This will store the FULL array of commands

This will still not hold any value.

Code: [Select]
            BTArray[IndexNumber] = receivedChars;
Overwriting some variables by storing the address of the array is a strange idea.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

DovahBear

Upon re-writing the code (as advised by noweare) and actually defining a size for my array (i.e. int BTArray[5] = {};), the code seems to hold the right values.

Thank you all for your help and advise! I do appreciate it!

Go Up