Initialization of object values ​​out of function does not work

Hi. I have a problem with initializing values out of function. When I create an object in a function it works correctly, but I can not call it from other functions but if I create it out of function it does not pass the values to the object and then when I give the function “printIt ()” it will write zero for all values.

ASR.ino

#include "AdvancedSR.h"
#define dp 3 //DATAPIN
#define lp 10 //LATCHPIN
#define cp 6 //CLOCKPIN
int NOS =3; //NUM OF SHIFT REGISTERS
AdvancedSR btn(dp,cp,lp,NOS);
void setup() {
  Serial.begin(9600);
  btn.printIt();


}
void loop(){
 btn.printIt();
}

AdvancedSR.h

#ifndef advancedsh_h
#define advancedsh_h
#include "Arduino.h"
class AdvancedSR
{
  public:
  AdvancedSR(int DataPin,int ClockPin,int LatchPin,int NumOfSR);
  void printIt();
  void ShiftIn(int INPin);
  bool SInOn[];
  int DP;
  int CP;
  int LP;
  int NOSR;
};
#endif

AdvancedSR.cpp

#include "AdvancedSR.h"
AdvancedSR::AdvancedSR(int DataPin,int ClockPin,int LatchPin,int NumOfSR){
  
  DP = DataPin;
  CP = ClockPin;
  LP = LatchPin;
  pinMode(DP,OUTPUT);
  pinMode(LP,OUTPUT);
  pinMode(CP,OUTPUT);
  NOSR=NumOfSR;
  for( int x = 0;x<=(NOSR*8);x++){
    SInOn[x]= false;
  }
}

void AdvancedSR::printIt(){
  Serial.println(String(DP)+" "+String(CP)+" "+String(LP)+" "+String(NOSR));
}
void AdvancedSR::ShiftIn(int INPin){
  int x = NOSR;
  uint64_t BITS = 1;
  for(byte y = 0;y<(x*8);y++){
    digitalWrite(LP,LOW);
    for(byte nosW = NOSR;nosW>=1;nosW--){
      shiftOut(DP,CP,MSBFIRST,BITS>>8*(nosW-1));
    }
    
    digitalWrite(LP,HIGH);
    SInOn[y]=digitalRead(INPin);
    BITS*=2;
    delay(1);
    Serial.println("CIL");
  }
  digitalWrite(LP,LOW);
  for(int l = 1;l<=NOSR;l++){
    shiftOut(DP,CP,MSBFIRST,0);
  }
  digitalWrite(LP,HIGH);
}

you can't work with the hardware api before setup (). it is not intialized. add init or begin function to your class and call it in setup

I created the “begin” method but it does not work anyway.
ASR.ino

#include "AdvancedSR.h"
#define dp 3 //DATAPIN
#define lp 10 //LATCHPIN
#define cp 6 //CLOCKPIN
int NOS =3; //NUM OF SHIFT REGISTERS

AdvancedSR btn;

void setup() {
  
  Serial.begin(9600);
  btn.begin(dp,cp,lp,NOS);

}
void loop(){
 btn.printIt();
}

AdvancedSR.h

#ifndef advancedsh_h
#define advancedsh_h
#include "Arduino.h"
class AdvancedSR
{
  public:
  void begin(int DataPin,int ClockPin,int LatchPin,int NumOfSR);
  AdvancedSR();
  void printIt();
  void ShiftIn(int INPin);
  bool SInOn[];
  int DP;
  int CP;
  int LP;
  int NOSR;
};
#endif

AdvancedSR.cpp

#include "AdvancedSR.h"
void AdvancedSR::begin(int DataPin,int ClockPin,int LatchPin,int NumOfSR){
  
  DP = DataPin;
  CP = ClockPin;
  LP = LatchPin;
  pinMode(DP,OUTPUT);
  pinMode(LP,OUTPUT);
  pinMode(CP,OUTPUT);
  NOSR=NumOfSR;
  for( int x = 0;x<=(NOSR*8);x++){
    SInOn[x]= false;
  }
}
AdvancedSR::AdvancedSR(){
  init();
}

void AdvancedSR::printIt(){
  Serial.println(String(DP)+" "+String(CP)+" "+String(LP)+" "+String(NOSR));
}
void AdvancedSR::ShiftIn(int INPin){
  int x = NOSR;
  uint64_t BITS = 1;
  for(byte y = 0;y<(x*8);y++){
    digitalWrite(LP,LOW);
    for(byte nosW = NOSR;nosW>=1;nosW--){
      shiftOut(DP,CP,MSBFIRST,BITS>>8*(nosW-1));
    }
    
    digitalWrite(LP,HIGH);
    SInOn[y]=digitalRead(INPin);
    BITS*=2;
    delay(1);
    Serial.println("CIL");
  }
  digitalWrite(LP,LOW);
  for(int l = 1;l<=NOSR;l++){
    shiftOut(DP,CP,MSBFIRST,0);
  }
  digitalWrite(LP,HIGH);
}
  bool SInOn[];

  for( int x = 0;x<=(NOSR*8);x++){
    SInOn[x]= false;
  }

Writing to a zero element array seems wrong to me.

But how to use array in class

Your misconception is not related to classes at all.

You should not read or write outside array bounds, but C++ will not stop you from doing it.
You will probably get a warning if that was enabled.

In C++ arrays are just collections of elements of the same type sitting side by side in memory,
there is no builtin mechanism for automatic allocation (besides arrays on the stack) or size changes.

Study arrays and how to work with dynamic allocated arrays in a C++ tutorial.

MenTom_CZ:
But how to use array in class

same as in struct
bool SInOn[S_IN_COUNT];

WARNING: Your object doesn’t have a .init() function so what is being called in the constructor? It can’t be .begin() which has to be called in setup() so it is AFTER the Arduino runtime is initialized.

ERROR:  for( int x = 0;x<=(NOSR*8);x++){
That should be “x < (NOSR*8)” or you will go off the end of your array. Indexes start at 0 and end at size-1.

If you don’t know the size of the array until the object is constructed I think one way is to make the member variable a pointer and dynamically allocate the memory in .begin(). This requires a ‘destructor’ function to clean up.

ASR.ino

#include "AdvancedSR.h"
#define dp 3 //DATAPIN
#define lp 10 //LATCHPIN
#define cp 6 //CLOCKPIN
int NOS =3; //NUM OF SHIFT REGISTERS

AdvancedSR btn;

void setup() {
  
  Serial.begin(9600);
  btn.begin(dp,cp,lp,NOS);

}
void loop(){
 btn.printIt();
}

AdvancedSR.h

#ifndef advancedsh_h
#define advancedsh_h
#include "Arduino.h"
class AdvancedSR
{
  public:
  void begin(int DataPin,int ClockPin,int LatchPin,int NumOfSR);
  AdvancedSR();
  ~AdvancedSR();  // Destructor needed to free allocated memory
  void printIt();
  void ShiftIn(int INPin);
  bool *SInOn;  // Allocated memory for array of bool
  int DP;
  int CP;
  int LP;
  int NOSR;
};
#endif

AdvancedSR.cpp

#include "AdvancedSR.h"
void AdvancedSR::begin(int DataPin,int ClockPin,int LatchPin,int NumOfSR){
  
  DP = DataPin;
  CP = ClockPin;
  LP = LatchPin;
  pinMode(DP,OUTPUT);
  pinMode(LP,OUTPUT);
  pinMode(CP,OUTPUT);
  NOSR=NumOfSR;
  SInOn = new bool[NOSR * 8];
  for( int x = 0;x<=(NOSR*8);x++){
    SInOn[x]= false;
  }
}
AdvancedSR::AdvancedSR(){
  init();
}

AdvancedSR::~AdvancedSR(){
  delete [] SInOn;
}

void AdvancedSR::printIt(){
  Serial.println(String(DP)+" "+String(CP)+" "+String(LP)+" "+String(NOSR));
}
void AdvancedSR::ShiftIn(int INPin){
  int x = NOSR;
  uint64_t BITS = 1;
  for(byte y = 0;y<(x*8);y++){
    digitalWrite(LP,LOW);
    for(byte nosW = NOSR;nosW>=1;nosW--){
      shiftOut(DP,CP,MSBFIRST,BITS>>8*(nosW-1));
    }
    
    digitalWrite(LP,HIGH);
    SInOn[y]=digitalRead(INPin);
    BITS*=2;
    delay(1);
    Serial.println("CIL");
  }
  digitalWrite(LP,LOW);
  for(int l = 1;l<=NOSR;l++){
    shiftOut(DP,CP,MSBFIRST,0);
  }
  digitalWrite(LP,HIGH);
}