Character array gets messed up when I "examine" it in my code

Hello Everyone,

What I am trying to do: I want my arduino to receive a message from my computer and output a PWM value on digital pin 3 according to the message received. the message sent from the computer always takes the form of where A will always be a 0 or a 1 and BBB is the three digit value of the PWM signal (0 <= BBB <= 255). The A value is whether the pin is on or off, and the BBB value is the PWM value.

I need to get the AAA into an integer to be sent to the pin ← my problem

What I have done so far: Here is my code in its entirety

/* Aug 21 2012
Joystick read and serial send
This program attempts to read a joystick value and send it over the serial port

This one is working
*/

#include <Bounce.h> // debouncing
#include <serLCD.h> // serial LCD from SparkFun
#include <SoftwareSerial.h> // Software Serial (for LCD)

// INPUTS
#define JoyPin 0 // Horizontal joystick ANALOG pin 0
#define PWMlitePin 3 // pin to write PWM on

// Variables

int JoyPot; // the joystick potentiometer value returned by the arduino
int JoyVal; // the value from the potentiometer mapped to a value (below I use 100 - 600)
int OldJoyVal = 0; // used to look for a change in value
int JoyDiff; // difference in readings
int LiteVal; // PWM value for light brightness
int LitePow; // led power
int LEDPower = 1;
boolean started = false;
boolean ended = false;
char inData[3]; // array to hold serial data sent from LabView
char LiteValArray[2]; 
byte index; // keep track of scribing the serial data

void setup() 
{
  pinMode(JoyPin, INPUT);
  pinMode(PWMlitePin, OUTPUT);  
  
  Serial.begin(38400);
  Serial.flush();
}

void loop()
{
  JoyPot = analogRead(JoyPin);    // read the input pin
  JoyVal = map(JoyPot, 0, 1023, 100, 600); 
  JoyDiff = abs(JoyVal - OldJoyVal);
  
  if(abs(JoyVal - OldJoyVal) > 0) 
  {  
    //Serial.print("*"); Serial.print(JoyVal); Serial.print("+"); //Serial.print("Hello");
  }
  
  OldJoyVal = JoyVal; // keep track of the last reading
  
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();


    if (inChar == '<')
    {
      index = 0;
      started = true;
      ended = false; 
    }

    else if (inChar == '>')
    {
      ended = true;
    }

    else 
    {    
      if (index <= 3)
      {
        inData[index] = inChar;
        index++;
      }
    }


     if (started && ended)
    {
      for(int i = 0; i < 3; i++)
      {
        LiteValArray[i] = inData[i+1];       
      }
      
      LiteVal = atoi(LiteValArray);
      
      if (inData[0] == '1')
      {
        analogWrite(PWMlitePin, LiteVal);
        Serial.print("+"); Serial.print(LiteValArray); Serial.print("-"); Serial.print(inData); Serial.print(":"); Serial.println(sizeof(inData));         
      }


      else if (inData[0] == '0')
      {
        analogWrite(PWMlitePin, 0);
      }
      
      started = false;
      ended = false;
      index = 0; 
    }
  }
}

There is some other stuff in there that is working and is independent of this problem. The area of the code that we are concerned with is here;

while (Serial.available() > 0)
  {
    char inChar = Serial.read();


    if (inChar == '<')
    {
      index = 0;
      started = true;
      ended = false; 
    }

    else if (inChar == '>')
    {
      ended = true;
    }

    else 
    {    
      if (index <= 3)
      {
        inData[index] = inChar;
        index++;
      }
    }


     if (started && ended)
    {
      for(int i = 0; i < 3; i++)
      {
        LiteValArray[i] = inData[i+1];       
      }
      
      LiteVal = atoi(LiteValArray);
      
      if (inData[0] == '1')
      {
        analogWrite(PWMlitePin, LiteVal);
        Serial.print("+"); Serial.print(LiteValArray); Serial.print("-"); Serial.print(inData); Serial.print(":"); Serial.println(sizeof(inData));         
      }


      else if (inData[0] == '0')
      {
        analogWrite(PWMlitePin, 0);
      }
      
      started = false;
      ended = false;
      index = 0; 
    }
  }

The problem is that the inData array gets jumbled / modified by the line

for(int i = 0; i < 3; i++)
      {
        LiteValArray[i] = inData[i+1];       
      }

If that for loop is commented out (eliminated), the inData array is returned exactly as I sent it (for example, If I send <1231>, the debugging line Serial.print("+"); Serial.print(LiteValArray); Serial.print("-"); Serial.print(inData); Serial.print(":"); Serial.println(sizeof(inData));

returns +1-1231: (1231 is the inData Array)

When I run that for loop and again send <1231>, the same debugging line returns +232-123232:3

What I don’t understand is I am not modifying inData, although it gets returned now as a six digit number, yet the size stays the same (3). Also, sending different values of BBB always returns a number in inData that only change in multiples of 10. For example, sending BBB as any number between 230 and 239 always returns +232-123232:3, and sending BBB as any number between 240 and 249 always returns +242-124242:3,

One of the high lords of this forum has a signature that says “measurement modifies behavior”, and this problem makes me think of that statement.

What am I doing wrong?

You're using atoi, but I can't see where you're terminating the string. inData is too small, I think.

AWOL: inData is too small, I think.

LiteValArray is also too small.

When you declare an array like inData[3], that means you get indeces 0, 1, and 2 to work with. You, however, have 4 values you are trying to put in it. Similiar problem with LiteValArray. The char arrays need two more spaces each to accommodate for the last digit and the null terminator.

You guys really are masters. That fixed it.

Does the null terminator automatically get added to the end of all arrays?

brian15co: Does the null terminator automatically get added to the end of all arrays?

No, not usually. There are certain circumstances like initilizing a string:

char myString[] = "Hello World!";

where the null-terminator gets added, but most of the time that is up to the programmer.

RTFM, from the Arduino reference site under array;

Finally you can both initialize and size your array, as in mySensVals. Note that when declaring an array of type char, one more element than your initialization is required, to hold the required null character.

Thanks for all the answers guys