Working on a project and I have hit a wall. I created a RNG for a microcontroller where users can choose the upper limit of the RNG, so it would pick a number between 1 and upper limit. However, I am having to print this info into an LCD module. All the code works, except it will not print the actual generated number onto the LCD. I believe it is because due to the way the code works, the type for the variable is a String rather than an Integer (but I am not certain). Is there any way to convert a String type to an Integer type?
I have included some of the code below, this is not everything. This project is a combination of C language and Assembly.
Code:
int lower;
char upper;
int count;
char die;
char dieResult[4];
void printRandoms(int numFaces, int count)
{
int i;
int num = 1;
for (i = 0; i < count; i++) {
num = rand() % numFaces + 1;
ASCII = '0' + num;
}
memset(dieResult, 0, 4); //memset(result) is a char type
i = log10(num);
while (num != 0) {
dieResult[i] = '0' + (num % 10);
num /= 10;
i--;
}
}
void RNG()
{
UART_Puts(MS7);
UART_Puts(MS6);
UART_Get();
while (ASCII != '.') {
die = die * 10 + (ASCII - '0');
UART_Get();
}
upper = die;
char *result;
char buffer[20];
printRandoms(upper, 1);
sprintf(buffer, "Rolled d%d: %s", upper, dieResult);
//int value;
//value = atoi(dieResult); //this was an attempt at converting string to int
result = buffer;
UART_Puts(result);
DATA = 0x34;
LCD_Write_Command();
DATA = 0x08;
LCD_Write_Command();
DATA = 0x02;
LCD_Write_Command();
DATA = 0x06;
LCD_Write_Command();
DATA = 0x0f;
LCD_Write_Command();
char lcdOut[16];
sprintf(lcdOut,"d%d: %s", upper, dieResult); //can replace %s with %p to print in hexadecimal
//I think this doesnt print the dieResult because its a string and not an int
//sprintf(lcdOut,"d%d: %s", upper, dieResult);
LCD_Puts(lcdOut); //this doesn't work. Seems like dieResult has some memory error
//free(result); //cant have this free() as it isnt a pointer allocated via malloc()
//die = 0;
}