Help creating alphanumeric string

Hello, I have a couple questions revolving creating a random alphanumeric string in arduino.

1st- Is this similar to creating it in C++?
2nd- How exactly would I go about it using an arduino leonardo because I want to be able to use a function to generate it multiple times instead of rewriting code everytime and or only being able to use it once.

Essentially with my arduino leonardo I have the ability to
Keyboard.printIn()
and I want to be able to run something like
Keyboard.printIn(randomString());
and then have it write that out.

If anyone can help I'd appreciate it a lot.

  1. Yes. Quite similar.
  2. Well that is exactly what functions are for, that shouldn't change your code nor increase the code complexity.

There was previously Generate random alphanumeric on this forum, I believe this was not the only post on it aswell.

In the end

Thank you, I just read through it and I am confused since when writing to my arduino leonardo I am only using the void setup part. And the code in that post uses the void loop and setup so how would I even implement that into my code?

Any reason why?

You can move the full content of loop() into setup().

You can also run loop() once if so inclined.

void loop()
{
  // do some stuff here
  ...
  ...

  // hang forever
  for(;;);
}

I just need help making the function to randomly generate an alphaNumerical string.
This is what I have so far but it's not working
void randStr(int lengt){
String randString;
int randN;
for(int x; x < lengt; x++){
randN = rand(97, 122);
randString = randString + to_string(randN);
randN = rand(0, 9);
randString = randString + to_string(randN);
}
}

Microcontrollers have little RAM.
You will do better using C char-array zero-terminated strings than C++ String objects.
Avoid dynamic allocation like the plague.

Use Serial to communicate with Serial Monitor.

Arduino Foundations -- one page of docs on the Main Arduino site.

@eh_asuna, please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

Please explain. And without context (the complete program) we have no idea what can be wrong.

@eh_asuna I suggest that you break your problem into sub-problems and work out each of one them separately. For instance: can you generate a random string by itself? Can you output a constant string by itself?

I agree with the advice to avoid String objects and use vanilla char arrays instead.

You assigned result to local variable, ok, and then what?

You can do something like this

#include <stdlib.h>

static const char alphanumericCharacters[] = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h,', 'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};

static void randomString(char *str, unsigned int count) {

    unsigned int i;
    for (i = 0; i < count; i++) {
        str[i] = alphanumericCharacters[rand() % sizeof(alphanumericCharacters)];
    }

    str[i] = '\0';
}

int main() {

    char str[129];
    randomString(str, 128);

    return 0;
}

I don't have an Arduino at hand right now, so this is written in plain C but should be easily adaptable.

This will not add numbers in ASCII to the string, but control characters,
including the "end of cstring" character '\0'.

  char chr;

// ............ snip

// ............ somewhere in void loop ---- How to filter data to handle by type

  if (( chr >= '0' ) && ( chr <= '9' )  { chr is a text digit, value = chr - '0';  do something; }

  else   if (( chr >= 'A' ) && ( chr <= 'Z' )  { chr is text uppercase alpha;  do something; }
  
  else   if (( chr >= 'a' ) && ( chr <= 'z' )  { chr is text lowercase alpha;  do something; }

  else  chr = 0;  // not an alpha-numeric

you could do

  for ( chr = '0'; chr < ( '9' + 1 ); chr++ )  { print( chr ); }
  print( "\n\n" );

  for ( chr = 'A'; chr < ( 'Z' + 1 ); chr++ )  { print( chr ); }
  print( "\n\n" );

  for ( chr = 'a'; chr < ( 'z' + 1 ); chr++ )  { print( chr ); }
  print( "\n\n" );

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.