Go Down

Topic: Returning char arrays to a function (Read 117 times) previous topic - next topic

timozzzz

Aug 29, 2019, 10:24 am Last Edit: Aug 29, 2019, 10:31 am by timozzzz
Hello everyone,

I'm trying to make a Rubux cube solver (using the CFOP method to solve the cube) and I have a question about my code (it is not finished yet):

So far I have made multiple sketches of which one head sketch. This is the code of that sketch:
Code: [Select]
#include <Stepper.h>

#define W_DIR_PIN             1
#define W_STEP_PIN            2

#define G_DIR_PIN             3
#define G_STEP_PIN            4

#define R_DIR_PIN             5
#define R_STEP_PIN            6

#define B_DIR_PIN             7
#define B_STEP_PIN            8

#define O_DIR_PIN             9
#define O_STEP_PIN            10

#define Y_DIR_PIN             11
#define Y_STEP_PIN            12

void setup() {
  pinMode(W_DIR_PIN,        OUTPUT);
  pinMode(W_STEP_PIN,       OUTPUT);
  pinMode(G_DIR_PIN,        OUTPUT);
  pinMode(G_STEP_PIN,       OUTPUT);
  pinMode(R_DIR_PIN,        OUTPUT);
  pinMode(R_STEP_PIN,       OUTPUT);
  pinMode(B_DIR_PIN,        OUTPUT);
  pinMode(B_STEP_PIN,       OUTPUT);
  pinMode(O_DIR_PIN,        OUTPUT);
  pinMode(O_STEP_PIN,       OUTPUT);
  pinMode(Y_DIR_PIN,        OUTPUT);
  pinMode(Y_STEP_PIN,       OUTPUT);
  
  char White[9];
  char Blue[9];
  char Orange[9];
  char Green[9];
  char Red[9];
  char Yellow[9];

  Input(White, Blue, Orange, Green, Red, Yellow);

  OLL(Yellow, Blue, Orange, Green, Red);
  PLL(Blue, Orange, Green, Red);

  Turns(URotationsPLL, RotationsPLL);
}

void loop() {
  // put your main code here, to run repeatedly:
}


In another sketch, I programmed the code for the function PLL:
Code: [Select]
void PLL(char Blue[], char Orange[], char Green[], char Red[]) {
  for (int i = 0; i <= 3; i++){
    switch (i) {
      case 0: char URotationsPLL[1] = {}; break;
      case 1: URotationsPLL[2] = {'U'}; break;
      case 2: URotationsPLL[2] = {"U2"}; break;
      case 3: URotationsPLL[2] = {"Uc"}; break;
    }
    if ((Blue[6] == Blue[7] == Red[2]) && (Orange[6] == Red[0])) {
      char RotationsPLL[10][3] = {"Rc", "B", "Rc", "G2", "R", "Bc", "Rc", "G2", "R2"};
      i = 4;
    } //PLL Aa (Code of the PLL case)
    else if ((Blue[6] == Blue[7] == Green[4]) && (Orange[5] == Red[1] == Blue[0])) {
      char RotationsPLL[10][3] = {"R2", "G2", "R", "B", "Rc", "G2", "R", "Bc", "R"};
      i = 4;
    } //PLL Ab
    // 19 more cases (are pretty much the same as the ones above; just a slightly different if statement and array)
    else {
      Blue[0] = Extra[9];
      Blue[7] = Extra[10];
      Blue[6] = Extra[11];
      Orange[6] = Extra[0];
      Orange[5] = Extra[1];
      Orange[4] = Extra[2];
      Green[4] = Extra[3];
      Green[3] = Extra[4];
      Green[2] = Extra[5];
      Red[2] = Extra[6];
      Red[1] = Extra[7];
      Red[0] = Extra[8];
    }
  }
}


The letters between the braces (such as {'U'}) represents the turn the robot needs to do to solve that part of the Cube.

My first question is: I am allowed to compare those arrays (e.g. Blue[6] == Blue[7] == Red[2]) in this way?

Further on, my problem is I need to process those turns (the codes in URotationsPLL and RotationsPLL) in another sketch (the function Turns, which has not been programmed yet). And to do that, I need to be able to use those variables in that sketch.
I thought it might be possible to combine URotationsPLL and RotationsPLL to one array and return that combined array to the function or to return multiple arrays to one function, but I have no idea if either one of those ideas are possible.
So is it possible or is there a better, easier solution?

Thank you in advance,
timozzzz

This is my first big project with Arduino, so there probably will be mistakes in the code

6v6gt

Quote
My first question is: I am allowed to compare those arrays (e.g. Blue[6] == Blue[7] == Red[2]) in this way?
You are actually attempting to compare array elements, not entire arrays.
It's clear what you are trying to do, but I guess you have got to do it like this:

if ( Blue[6] == Blue[7] && Blue[7] == Red[2] ) {
   // action here
}

thomasvt

The == is a binary operator, which means it takes two operands.

So, I'm pretty certain that your a == b == c is interpreted as (a == b) == c. So, you are comparing c to the boolean result of a == b, which works in C++, but is not what you want.

The example given by 6v6gt is the correct way to do what you intend.

Whandall

In another sketch, I programmed the code for the function PLL:
Code: [Select]
void PLL(char Blue[], char Orange[], char Green[], char Red[]) {
  for (int i = 0; i <= 3; i++) {
    switch (i) {
      case 0: char URotationsPLL[1] = {}; break;
      case 1: URotationsPLL[2] = {'U'}; break;
      case 2: URotationsPLL[2] = {"U2"}; break;
      case 3: URotationsPLL[2] = {"Uc"}; break;
    }
  }
}
void setup() {}
void loop() {}

generates a lot of serious warnings.
Code: [Select]
Somewhere\PLL\PLL.ino: In function 'void PLL(char*, char*, char*, char*)':
Somewhere\PLL\PLL.ino:5:12: warning: jump to case label [-fpermissive]
       case 1: URotationsPLL[2] = {'U'}; break;
            ^
Somewhere\PLL\PLL.ino:4:20: note:   crosses initialization of 'char URotationsPLL [1]'
       case 0: char URotationsPLL[1] = {}; break;
                    ^
Somewhere\PLL\PLL.ino:6:12: warning: jump to case label [-fpermissive]
       case 2: URotationsPLL[2] = {"U2"}; break;
            ^
Somewhere\PLL\PLL.ino:4:20: note:   crosses initialization of 'char URotationsPLL [1]'
       case 0: char URotationsPLL[1] = {}; break;
                    ^
Somewhere\PLL\PLL.ino:6:32: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
       case 2: URotationsPLL[2] = {"U2"}; break;
                                ^
Somewhere\PLL\PLL.ino:7:12: warning: jump to case label [-fpermissive]
       case 3: URotationsPLL[2] = {"Uc"}; break;
            ^
Somewhere\PLL\PLL.ino:4:20: note:   crosses initialization of 'char URotationsPLL [1]'
       case 0: char URotationsPLL[1] = {}; break;
                    ^
Somewhere\PLL\PLL.ino:7:32: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
       case 3: URotationsPLL[2] = {"Uc"}; break;
                                ^


Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

timozzzz

#4
Aug 29, 2019, 01:10 pm Last Edit: Aug 29, 2019, 01:27 pm by timozzzz
First of all, thank you all for your responses

So far I changed the code like 6v6gt suggested. Also I replaced the switch statements in if statements. All of the warnings are gone right now.

Code: [Select]
if (i == 0) {
  char URotationsPLL[] = {};
}
else if (i == 1) {
  char URotationsPLL[] = {'U'};
}
else if (i == 2) {
  char URotationsPLL[] = {"U2"};
}
else if (i == 3) {
  char URotationsPLL[] = {"Uc"};
}


Does anyone know how I can solve the other problem?

Whandall

#5
Aug 29, 2019, 01:12 pm Last Edit: Aug 29, 2019, 01:15 pm by Whandall
First of all, thank you all for your responses

So far I changed the code like 6v6gt suggested. Also I replaced the switch statements in if statements. All of the warnings are gone right now.

Code: [Select]

if (i == 0) {
      char URotationsPLL[] = {};
    }
    else if (i == 1) {
      char URotationsPLL[] = {'U'};
    }
    else if (i == 2) {
      char URotationsPLL[] = {"U2"};
    }
    else if (i == 3) {
      char URotationsPLL[] = {"Uc"};
    }
}


Does anyone know how I can solve the other problem?
That code is nonsense, hast no effect and will probably be removed by the compiler.

And I get warnings for that code.
Code: [Select]
Somewhere\PLL\PLL.ino: In function 'void PLL(char*, char*, char*, char*)':
Somewhere\PLL\PLL.ino:4:12: warning: unused variable 'URotationsPLL' [-Wunused-variable]
       char URotationsPLL[] = {};
            ^
Somewhere\PLL\PLL.ino:7:12: warning: unused variable 'URotationsPLL' [-Wunused-variable]
       char URotationsPLL[] = {'U'};
            ^
Somewhere\PLL\PLL.ino:10:12: warning: unused variable 'URotationsPLL' [-Wunused-variable]
       char URotationsPLL[] = {"U2"};
            ^
Somewhere\PLL\PLL.ino:13:12: warning: unused variable 'URotationsPLL' [-Wunused-variable]
       char URotationsPLL[] = {"Uc"};
            ^
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

timozzzz

I am going to use those variables somewhere else (not in this sketch). I think that will remove those warnings, beceause they are used then

Whandall

#7
Aug 29, 2019, 01:28 pm Last Edit: Aug 29, 2019, 01:50 pm by Whandall
I am going to use those variables somewhere else (not in this sketch). I think that will remove those warnings, beceause they are used then
No.

You can not use variables outside the defining block.

Code: [Select]
char URotationsPLL[] = "unexpected";
void PLL(char Blue[], char Orange[], char Green[], char Red[]) {
  for (int i = 0; i <= 3; i++) {
    if (i == 0) {
      char URotationsPLL[] = {};
    }
    else if (i == 1) {
      char URotationsPLL[] = {'U'};
    }
    else if (i == 2) {
      char URotationsPLL[] = {"U2"};
    }
    else if (i == 3) {
      char URotationsPLL[] = {"Uc"};
    }
  }
  Serial.print(F("URotationsPLL[] = '"));
  Serial.print(URotationsPLL);
  Serial.println('\'');
}
char blue[] = "blue";
char orange[] = "orange";
char green[] = "green";
char red[] = "red";
void setup() {
  Serial.begin(250000);
  PLL(blue, orange, green, red);
}
void loop() {}

Code: [Select]
URotationsPLL[] = 'unexpected'
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Go Up