Using global variables/definitions from .ino in another custom class

So in my main .ino file (titled ‘ABS3’), I create 4 global definitions:

#define PROCESS_V 0
#define BUTTON_V 1
#define PROCESS_S 2
#define BUTTON_S 3

I use these for a global 4-element boolean array (also defined in ‘ABS3’).

I pass this array into a function (void pressed(boolean act)) that’s part of this custom class called ‘LCD_Control’, however I do not want to have to redefine those 4 globals up top in the header file for ‘LCD_Control’.

How do I give LCD_Control class access to those 4 globals so I can use them in the ‘pressed(…)’ function?

Thanks

lennon-pledge:
How do I give LCD_Control class access to those 4 globals so I can use them in the 'pressed(..)' function?

are these constants?

why are you trying to do this with the preprocessor, why not just pass the four numbers in the class's constructor?

Yes they are constants, and what do you mean by "..with the preprocessor"? I don't know enough to pick that apart. Trying to do this with the preprocessor as opposed to what?

And I need the 4 numbers in my main file and the LCD_Control class. I did not want to have to make 8 constants to handle 4 numbers.

lennon-pledge:
Yes they are constants, and what do you mean by "..with the preprocessor"? I don't know enough to pick that apart. Trying to do this with the preprocessor as opposed to what?

And I need the 4 numbers in my main file and the LCD_Control class. I did not want to have to make 8 constants to handle 4 numbers.

I don't know what these are:

#define PROCESS_V 0
#define BUTTON_V   1
#define PROCESS_S  2
#define BUTTON_S   3

but I suppose they are four hardware pins, why not just pass them to your class in the constructor? e.g.

#define PROCESS_V  0
#define BUTTON_V   1
#define PROCESS_S  2
#define BUTTON_S   3

class MyClass{
  public:
    MyClass(byte pV, byte bV, byte pS, byte bS){
      processV = pV;
    }
  private:
    byte processV;
};

MyClass myObject(PROCESS_V, BUTTON_V, PROCESS_S, BUTTON_S);

They are constant representations of 4 scenarios for a project I'm working on:

Let's say, for an 8-vial carousel system, I change my visible vial 4 to vial 6 via two button clicks on my LCD. The carousel rotates to 6, and my 2nd element for the array boolean action[4] is set to true.

Let me reword the question a little bit.

Does it do any harm to define those 4 constants in the main ABS3 AND the LCD_Control class? So 2 definitions representing the same scenario and number will be compiled twice.

IN ABS3
#define BUTTON_VIAL 1

IN LCD_Control
#define BUTTON_VIAL 1

compiled twice.

No harm, no foul or is there a better coding practice to apply here? What you wrote out is not solving the duplication issue at hand (then again, that's probably my fault for not being clear enough.) If there is even a duplication issue to consider fixing.

lennon-pledge:
They are constant representations of 4 scenarios for a project I'm working on:

Let's say, for an 8-vial carousel system, I change my visible vial 4 to vial 6 via two button clicks on my LCD. The carousel rotates to 6, and my 2nd element for the array boolean action[4] is set to true.

what is a "constant representation?"

it seems you are speaking relative to the code you didn't post.

lennon-pledge:
Does it do any harm to define those 4 constants in the main ABS3 AND the LCD_Control class? So 2 definitions representing the same scenario and number will be compiled twice.

IN ABS3
#define BUTTON_VIAL 1

IN LCD_Control
#define BUTTON_VIAL 1

compiled twice.

No harm, no foul or is there a better coding practice to apply here?

This is certainly bad practice... see how it has already tripped you up?

How many libraries have you used where you need to go into the header file and make fundamental changes for it to work?

what are the four things you are #define-ing? what are they used for?

This is the first situation where I need the same constants in 2 libraries. It isn't tripping me up necessarily, I've just never done this before...I don't know the formalities of the solution.

Constant representations - numerical constants used to represent actions performed by a user on my machine.

So,

#define PROCESS_VIAL 0 ---------- Through some other software, a user changed the vial number
on the rotating carousel of our machine, so process the vial
(there's no need to explain any further, don't worry about what
these constants do, per say.)
#define BUTTON_VIAL 1 ---------- THROUGH ARDUINO, the user pressed a button on the LCD, so
the vial number changed and the carousel will now rotate.

I use an array to determine whether or not an action was committed by the user:

// Serial.print() log EXAMPLE
"array[PROCESS_VIAL] - false
array[BUTTON_VIAL] - true
array[PROCESS_SETP] - false
array[BUTTON_SETP] - false"

When the Arduino main loop() function runs, it finds that one of those array elements is set to TRUE, and thus the code to make the carousel rotate is run. Otherwise, the loop ignores any code that performs a physical action.

I made an array so that I can pass it into the function in LCD_Control and possibly make changes to it.

lennon-pledge:
I made an array so that I can pass it into the function in LCD_Control and possibly make changes to it.

so it is a four element array

someArray[4]

which you want to be able to modify from within the class or anywhere else in the program, even possibly from another class?

Precisely, and here's how I define it.

in ABS3.ino

boolean someArray[4];

#define PROCESS_VIAL 0
#define BUTTON_VIAL 1
#define PROCESS_SETP 2
#define BUTTON_SETP 3

// bunch of other code

void setup() {
someArray[PROCESS_VIAL] = false;
someArray[BUTTON_VIAL] = false;
someArray[PROCESS_SETP] = false;
someArray[BUTTON_SETP] = false;

// bunch of other code
}

void loop() {
pressed(action);

// bunch of other code
}

in LCD_Control.cpp

void LCD_Control::pressed(boolean act)
{
int lcd_key = readLCDButtons(); /**** don't worry about this...irrelevant /
switch (lcd_key)
{
case btnRIGHT: /
btnRIGHT is a constant represent the right button on LCD
{
act[0] = true;
act[1] = true;
break;
}
}
// bunch of other code
}

SO............you see how in the LCD_Control.cpp, I write act[0] and act[1]? Instead of 0, I want PROCESS_VIAL and instead of 1 I want BUTTON_VIAL.

They are just numbers.

try creating the global array and just passing a pointer to the array to the constructor…

here is an example:

byte someArray[4] = {3,2,1,0};

class MyClass{
  public:
    MyClass(byte* smArry){
      array = smArry;
    }
    void modifyArray(){
      for (int i = 0; i < 4; i++)
      {
        array[i] = i * 3;
      }
    }
    void printArray(){
      for (int i = 0; i < 4; i++)
      {
        Serial.println(array[i]);
      }
    }
  private:
    byte* array;
};

MyClass myObject(someArray); // edited for clearer syntax...

void setup() 
{
  Serial.begin(9600);
  Serial.println(F("Printing global array:"));
  for (int i = 0; i < 4; i++)
  {
    Serial.println(someArray[i]);
  }
  Serial.println(F("Printing array in class:"));
  myObject.printArray();
  myObject.modifyArray();
  Serial.println(F("Re-Printing array in class:"));
  myObject.printArray();
  Serial.println(F("Re-Printing global array:"));
  for (int i = 0; i < 4; i++)
  {
    Serial.println(someArray[i]);
  }
}

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

}

Listen I appreciate all your help so far, but first off, it's not a byte array.....and I'm not filling elements of the array with integers or bytes.....boolean values (which, yea, 1 and 0 are integers or bits, but that's not the point.)

0, 1, 2, 3 don't tell me anything about the physical operation that is taking place from the user interaction.

What does it mean to set the 3rd element of my array to 3?

Let me explain a little more, here is the critical code in my loop() function in main:

void loop()
{
    myLCD.pressed(someArray);    
    /**** if you recall, myLCD is the object created by LCD_Control class ****/

    if (someArray[BUTTON_VIAL] || someArray[BUTTON_SETP] || someArray[PROCESS_VIAL] || someArray[PROCESS_SETP]) {
    
        checkProcessed();
        /**** don't worry about what this does ****/
    }
}

So if any of those elements are true, checkProcessed() executes.

I'm not trying to create more arrays in my LCD_Control....that compounds the problem. Furthermore, there is no need to do so. I can explain why further if that helps.

I'm simply trying to use those #define constants in two separate files/classes. Because writing someArray[0] and someArray[1] is a shit way of discerning physical operations that will occur on my machine.

And since we're both aware that using #define twice for the same exact thing is poor coding practice, I'm trying to find an alternative.

lennon-pledge:
And since we’re both aware that using #define twice for the same exact thing is poor coding practice, I’m trying to find an alternative.

I see!

define an enum in your header:

enum Device {
  PROCESS_VIAL = 0,
  BUTTON_VIAL,
  PROCESS_SETP,
  BUTTON_SETP,
}device;

and you will be able to use those as if they are integers in global:

  someArray[PROCESS_VIAL] = false; // <<<<<<<<<<<<<<<<<<<<<<<<<<
  someArray[BUTTON_SETP] = false;

or in your class, the same way!

Ok, that's getting closer to what I'm looking for.

However, let's say I declare that enum in my header file of LCD_Control (and make it public), I get a "BUTTON_VIAL was not declared in this scope" when I try to use them in my main .ino file.

Same thing if I declare the enum as a global in my .ino file. I get a scope error in my LCD_Control class.

lennon-pledge:
Ok, that’s getting closer to what I’m looking for.

However, let’s say I declare that enum in my header file of LCD_Control (and make it public), I get a “BUTTON_VIAL was not declared in this scope” when I try to use them in my main .ino file.

Same thing if I declare the enum as a global in my .ino file. I get a scope error in my LCD_Control class.

it should be in the header…

you can’t use the enum anywhere higher in your .ino file than the #include for the header

example (compilable) header:

#ifndef TEST_H
#define TEST_H

#include "Arduino.h"

enum {
  PROCESS_VIAL = 0,
  BUTTON_VIAL,
  PROCESS_SETP,
  BUTTON_SETP,
};

class MyClass{
  public:
    MyClass(bool* smArry){
      array = smArry;
    }
    void modifyArray(){
      for (int i = 0; i < 4; i++)
      {
        array[PROCESS_VIAL] = true;
      }
    }
  private:
    bool* array;
};

#endif

and ino

#include "test.h"

bool someArray[4];

MyClass myObject(someArray);

void setup() 
{
  Serial.begin(9600);
  someArray[PROCESS_VIAL] = false; 
  someArray[BUTTON_SETP] = false;
}

void loop() 
{

}

AHHHHHH THERE IT IS!

I put the enum in the public section of the LCD_Control class. Didn't make it global.

Everything is good to go now, thank you very, very much for putting up with all this. Peace.