Go Down

Topic: random integers to string arrays (Read 1 time) previous topic - next topic

AXNRXN

Hi All,

I'm working on a project where I have to send string commands to a stepper motor controller. These commands look typically like "10030XYG" or "-1500X45000YG" and tell the control board how many steps to move the X or Y motors. I have a separate program that can take commands that I type in, load these command strings into an array, and send them out to the control board as needed. But, what I really want to to is randomly generate the X and Y positions, convert those to strings, append the X/Y/G charachters, and then load them into the array.

I'm trying to get my head around getting everything in a string format that will load into the "char* stepArray[]" string array. So, I made another test program shown here in order to get this portion to work. I'm using String objects for now, but they don't seem to agree with the char* stepArray[].  I get a "String_Tests:51: error: cannot convert 'String' to 'char*' in initialization" error line. I understand string objects are not recommended due to memory issues. But, so far, trying to get one command string out of a character array is proving difficult. I've also fooled around with using "ltoa" to convert the random long numbers into strings, but then I have to append the letters and so forth.

I think that if I can get my stepArray[] array to load up, I should have this issue licked. Thanks for any help and recommendations,
--> K

Code: [Select]
/*
Test program to convert random integers into strings commands to send to Stepper Control Board.

*/

//******************Constants***********


//******************Variables***********
String stepCommand = "";


//****************Function Prototypes******


//***************Setup******************************
void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
 
 
}

//*************Main Program Loop************************
void loop() {

long randNumber_X1 = random(-10000, 10001);
long randNumber_Y1 = random(-10000, 10001);
long randNumber_X2 = random(-10000, 10001);
long randNumber_Y2 = random(-10000, 10001);

Serial.print(randNumber_X1);
Serial.print(" ");
Serial.println(randNumber_Y1);

Serial.print(randNumber_X2);
Serial.print(" ");
Serial.println(randNumber_Y2);

String firstPosition = stepCommand + randNumber_X1 + "X" + randNumber_Y1 + "Y" + "G";
String secondPosition = stepCommand + randNumber_X2 + "X" + randNumber_Y2 + "Y" + "G";
String homePosition = "0XYG";

Serial.println(firstPosition);    //This prints to Serial Monitor as expected
Serial.println(secondPosition);  //This prints to Serial Monitor as expected
Serial.println(homePosition);  //This prints to Serial Monitor as expected

char* stepArray[] = {    //Load up CNC Patterns into string array. HOW TO LOAD STRINGS???
  firstPosition,
  secondPosition,
  homePosition
  };

   
 
}
//*********************FUNCTIONS***************************

//**************************************************************************



guix

#1
Oct 09, 2012, 01:31 am Last Edit: Oct 09, 2012, 01:34 am by guix Reason: 1
Edit: sorry I was wrong :smiley-zipper:

holmes4

Don't use String (the object)! use the str (char array) functions or sprint() Or DIY it, lookup (it's handed out to students as an exercise) writing your own routine to convert an int to a string. Hint lots of dividing by ten to do.

Mark

holmes4


PeterH


Code: [Select]


char* stepArray[] = {    //Load up CNC Patterns into string array. HOW TO LOAD STRINGS???
 firstPosition,
 secondPosition,
 homePosition
 };



You can do it by declaring a character array long enough to hold the whole string (including the terminating null character, and then using sprintf() to write your string into that buffer.

Code: [Select]
const int LEN=20; // pick a number big enough to hold your longest command string + 1
char command[LEN];
sprintf(command, "%dX%dYG", x, y);


Although you seem to be trying to declare an array of strings, I suggest you only format and send one command string at a time - you haven't got much memory to play with.

If I were you I'd write a function which has function arguments providing the X and Y values as integer values, and sends the corresponding command string to your device.
I only provide help via the forum - please do not contact me for private consultancy.

AXNRXN

PeterH,

This method is close to working. I can get the X value to print in there, but the Y value seems to be chopped to either a 0 or -1.


Code: [Select]
/*
Test program to convert random integers into strings commands to send to Stepper Control Board.

*/

//******************Constants***********


//******************Variables***********



//****************Function Prototypes******


//***************Setup******************************
void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
 
 
}

//*************Main Program Loop************************
void loop() {

long randNumber_X1 = random(-10000, 10001);
long randNumber_Y1 = random(-10000, 10001);
long randNumber_X2 = random(-10000, 10001);
long randNumber_Y2 = random(-10000, 10001);

Serial.print(randNumber_X1);
Serial.print(" ");
Serial.println(randNumber_Y1);


const int LEN=20; // pick a number big enough to hold your longest command string + 1
char command[LEN];
sprintf(command, "%dX%dYG", randNumber_X1, randNumber_Y1);
Serial.println(command);

}
//*********************FUNCTIONS***************************

//**************************************************************************



And the Serial Monitor output is below. The first line just prints the random numbers, the second is the sprintf conversion. Any clue as to whats up?

8899 -3632
8899X0YG
4295 -5583
4295X0YG
-2445 -3267
-2445X-1YG
-5280 -9285
-5280X-1YG
342 -2815
342X0YG
-5991 -3474
-5991X-1YG



guix


AXNRXN

Ah, "l" for a long integer. That made it work.

Many props,
---> Karl

PaulS

Quote
Ah, "l" for a long integer. That made it work.

Of course, given the range of values that you are storing in the variables, it's hard to see why they are long.

Go Up