0
votes

I'm Having a little bit of trouble with this bit of code here for my arduino.

Basically what I'm trying to do is send in a series of characters, turn it into an char array, and use that to run the rest of the program. I'm close because I know everything is working perfectly internally, but when I boot from the serial port the message isn't getting in.

I assume that at this point it probably is how I am constructing the array... or some oddity. Probably just a simple error in how I put the code together but I'm completely struck. (I was previously using a string, but because of how the arduino works with them, it pretty much makes using them for memory purposes impossible).

I'm using a java program (ardulink) to send the information into the program with a customized version I've edited. So simply put, the input has to be a series of characters, and I need it stored in an array.

void serialEvent ()
{
  int arrayPostion = 0;
  int i;
  int maxArraySize = 20;
  char CharArrayInLocal[20];
  while (Serial.available() && !stringComplete) 
  {
    char inChar = (char)Serial.read();
    CharArrayInLocal[arrayPostion] = inChar;
    arrayPostion++;
    if (inChar == '\n') 
    {
      stringComplete = true;
    }
  }
  for (int i = 0; i<=19; i++)
  {
    CharArrayIn[i] = CharArrayInLocal[i];
  }
}
1
is this on its own thread?jbutler483
You are aware that your char CharArrayIn[20]; is local to the serialEvent() function and will not be available outside that very function?JimmyB
AT:jbutler483: I have no idea... but I assume so because the serial function of the arduino seems to be like setup and loop as far as I can tell. It threads it by itself. @HannoBinder: O.o You would be correct, that is one obvious thing I clearly overlooked, give me a second to re-up the code with a transfer for loop into another global array. If that's all the problem is I'll personally buy you a bear.Metric
@HannoBinder:I declared the variable in the local and the global, would this cause a conflict? I'll assume so and redefine the updated code.Metric
"I'll personally buy you a bear." - Well, thanks, but I generally don't accept wild animals ;-)JimmyB

1 Answers

0
votes

This worked for me,

String CharArrayInLocal[20];
String inputString = "";         
boolean stringComplete = false;  
int i=0;

void serialEvent() {
while (Serial.available()) {

char inChar = (char)Serial.read();

    if (inChar == '\n') {

      CharArrayInLocal[i]=inputString;

      i++;
      stringComplete = true;
      inputString="";
    }
    else{
      inputString += inChar;
    }
  }
}