2

I am creating a menu for adjusting system variables.

The menu is made up of pointers like so:

char* options4[] = {"hMin1", "hMax1", "refr1", "fSpeed1"};

I want to pass the selected string as the parameter for an 'adjust' function for use in some conditional logic and for display on a HCMAX7219 7 segment display. Here's what I've got:

int option = 0;
float variable = 82.0;

if(Serial.available() > 0) {
  key = Serial.read();
  if(key == back) {
    return;
  } else if(key == fwd) {
    float newVar = adjust(String(options4[option%4]), variable);
  } else if(key == down) {
    option++;
  } else if(key == up) {
    option--;
  }
}

float adjust(char* str, float var) {
  ...do some stuff...
  display.print7Seg(str, 8);
  EDIT: forgot this call..
  display.print7Seg(var, 1, 4);
  ...do stuff to var...
  return var;
}

The library function takes (char[] TextString, unsigned int Offset) as it's parameters.

EDIT: the library also has functions print7Seg(long number, byte decimalPlace, unsigned int Offset) and print7Seg(long number, unsigned int Offset)

Now I'm new to pointers, but I can see that I am passing a pointer to my adjust function, and expecting it to dig out a char array from memory. I can't see any problems with that, though I may be missing something. When I try to compile, the IDE just crashes with exit status 84; so no debug info.

Am I going about this the wrong way?

2 Answers 2

1

There are one or two things missing in your code. Where is setup() and loop()? Or is it just a snippet?

But answer to the question first. This is not right:

newVar = adjust(String(options4[option%4]), variable);

Should be:

newVar = adjust(options4[option%4], variable);

You want to index the vector of strings and pass one of the elements to the function.

Cheers!

7
  • It is just a snippet yeah. The full code is about 300 lines so thought I'd try to keep it brief. There are 12 variables to adjust so a generic function is what I'm aiming for. You're absolutely right about the typecasting on the function call, I didn't spot that. I've commented that line out for compilation, as I'm yet to think of a way of assigning the variable, so that's not the cause. Thanks for the spot though.
    – xeuari
    Feb 8, 2016 at 22:11
  • Do you mean the cause of the IDE crash? Feb 8, 2016 at 22:14
  • Yeah, the cause of the IDE crash could be linked to a problem in the code.
    – xeuari
    Feb 8, 2016 at 22:19
  • Then you will have to post all the code and tell us about the Arduino IDE version, board, etc. Have you turned on the "Show verbose output during:" in the IDE Preferences? Collect as much info as possible before the crash of the IDE. Feb 8, 2016 at 22:22
  • And I would also suggest changing the title of this post?? Feb 8, 2016 at 22:23
0

Ah .. ok I've just figured it out..

This is a legitimate way to pass char arrays to functions.

The original char* options4[] array is just an array of pointers to char arrays in memory, so passing one of these pointers to a function works fine.

The culprite was actually my float parameter, I was trying to make a library function call with a float to this function: print7Seg(long number, byte decimalPlace, unsigned int Offset).

This is what was causing the IDE to crash.

I got around it by having an int version of each of the variables I need to set. Then typecasting and dividing by a float to get variables that will be used in the main program. like so:

int hMin1Int = 820;
float hMin1 = (float)hMin1Int / 10.0;
int hMax1Int = 880;
float hMax1 = (float)hMax1Int / 10.0;
int refr1 = 30; //minutes
int fSpeed1 = 128;

I was then able to change my function to accept an int, the library function, so far, seems to be ok treating an int as a long, but if not I can just make all my ints longs:

int adjust(char* str, int var) {
  ...do some stuff...
  display.print7Seg(str, 8);
  display.print7Seg(var, 1, 4);
  ...do stuff to var...
  return var;
}

I was asking the wrong question!

Still I hope this will be useful to someone, so I will leave it up. Thanks for the response Mikael Patel.

Moral of the story, know your library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.