Char array to iterate through I/O pins

The project requires reading 35 Boolean yes/ no i/o pins
I want to iterate through them with an array. That would mean an array to specify the pin, an array to hold the status and an array to accept the appropriate value. I have been struggling and realize that I am hung up on the “”””char array_name [ ] = ( values ) “””” statement.
The standard approach: iterate through the fields with a variable in a ‘for’ statement.
I have the statements complete; I think. The ‘’int array_name etc ‘’’ works fine. The ‘’ char array_name” returns garbase. I am missing something.

I have reduced the code to the necessary parts. The first lines have an image of the project. The question is in the last lines. The char array; what am I missing?
And the Arduino project is

// I am unable to use an array with a char data type.I want to be able to reiterate throughthe 
// i/o pins to read a boolean yes/no without using a bujnch of if statements
//these first lines are mostly to communicate the project. but the issue is seen at the end.
//  my 'int arrays'are fine.  the 'char array' is finding and returning garbage
//  I have gone aroundand around on the details.  quotes, end '/0' thing square brackets etc
//  mostly no errors

#define ldrPin53 53
#define ldrPin52 52
#define ldrPin51 51
#define ldrPin50 50
#define ldrPin49 49

boolean ldrstatus53 = 0; 
boolean ldrstatus52 = 0; 
boolean ldrstatus51 = 0; 
boolean ldrstatus50 = 0; 
boolean ldrstatus49 = 0; 


float loc_53 = 181.12;   
float loc_52 = 179.04; 
float loc_51 = 176.96; 
float loc_50 = 174.88; 
float loc_49 = 172.8; 
int megaInputNumber = 1;
float primary_number = 16.7;
int i = 0;


/*
//pinMode (ldrPin53, INPUT); 
 pinMode (ldrPin52, INPUT);  
 pinMode (ldrPin51, INPUT);  
 pinMode (ldrPin50, INPUT);  
 pinMode (ldrPin49, INPUT); 
 
 */


//          hukmmm  it reads the i/o pin, not the status!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1                            INPUT STATUS ARRAY. HODER FOR BOOLEAN Y/N                                                                                                              THIS IS THE i/o ARRAY
char input_status[6] =  {    'ldrstatus49','ldrstatus50','ldrstatus51','ldrstatus52','ldrstatus53'  };
//                                                                                                                      THIS IS THE VALUE ARRAY
int value_array[6]  =  {     234,4556,1287,456     };  //   this one eworks
//                                                                                                PIN_ID_ARRAY
char pin_id_array[6] = {    ldrPin49,ldrPin50,ldrPin51,ldrPin52,ldrPin53     };

char char_array[7] = {    'ldrPin49','ldrPin50','ldrPin51','ldrPin52','ldrPin53' ,'\0'    };

const char  practicechar_array [5]=  {  '15','160','17','18','\0'};  // the const does not seem to help

////  the compier is ony seeing the ones place; one digit, on the right  hwat?
//  the \0 did not help


void setup()   {
  Serial.begin(9600);
  Serial.println("hello world");
}

void loop()    {

  primary_number = 767676;

 for ( int counter= 0; counter <5; counter++ ){ 
    Serial.print ( "   ok    ");
    Serial.println (value_array[counter]  );////////  this one works
    Serial.print ( "   what    ");
    Serial.println (value_array[2]) ;
 }


  for ( int counter= 0; counter <5; counter++ ){ 
    Serial.print ( "   ok    ");
    Serial.println (practicechar_array[counter]  );///////  this gives no error but garbage.. I see the last digit
    Serial.print ( "   what    ");
    Serial.println (practicechar_array[2]) ;
    delay (2000);

  }
}

Single quotes are for single ascii characters. This isn't Python.

Also a char variable holds one character, not a whole string of them.

It sounds like you could use an array of structures. The structure would contain the pin, the status, and the appropriate value. Then you'd have an array with 35 elements, where each array element is the structure.
I had a quick look at your code, but I couldn't work out what you expect it to do. I compiled it and got a lot of warnings. I think you might be using single quotes for strings when you should be using double quotes. It might do what you want if you fix all those warnings.
[Edit I wrote this before post #2 was posted, hence the same message about the quotes]

Something that may help: if you turn on all warnings during the compile, it will be eye opening.


/home/me/Documents/sketchbook/test/test.ino:42:30: warning: character constant too long for its type
 char input_status[6] =  {    'ldrstatus49','ldrstatus50','ldrstatus51','ldrstatus52','ldrstatus53'  };
                              ^~~~~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:42:44: warning: character constant too long for its type
 char input_status[6] =  {    'ldrstatus49','ldrstatus50','ldrstatus51','ldrstatus52','ldrstatus53'  };
                                            ^~~~~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:42:58: warning: character constant too long for its type
 char input_status[6] =  {    'ldrstatus49','ldrstatus50','ldrstatus51','ldrstatus52','ldrstatus53'  };
                                                          ^~~~~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:42:72: warning: character constant too long for its type
 char input_status[6] =  {    'ldrstatus49','ldrstatus50','ldrstatus51','ldrstatus52','ldrstatus53'  };
                                                                        ^~~~~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:42:86: warning: character constant too long for its type
 char input_status[6] =  {    'ldrstatus49','ldrstatus50','ldrstatus51','ldrstatus52','ldrstatus53'  };
                                                                                      ^~~~~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:48:27: warning: character constant too long for its type
 char char_array[7] = {    'ldrPin49','ldrPin50','ldrPin51','ldrPin52','ldrPin53' ,'\0'    };
                           ^~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:48:38: warning: character constant too long for its type
 char char_array[7] = {    'ldrPin49','ldrPin50','ldrPin51','ldrPin52','ldrPin53' ,'\0'    };
                                      ^~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:48:49: warning: character constant too long for its type
 char char_array[7] = {    'ldrPin49','ldrPin50','ldrPin51','ldrPin52','ldrPin53' ,'\0'    };
                                                 ^~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:48:60: warning: character constant too long for its type
 char char_array[7] = {    'ldrPin49','ldrPin50','ldrPin51','ldrPin52','ldrPin53' ,'\0'    };
                                                            ^~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:48:71: warning: character constant too long for its type
 char char_array[7] = {    'ldrPin49','ldrPin50','ldrPin51','ldrPin52','ldrPin53' ,'\0'    };
                                                                       ^~~~~~~~~~
/home/me/Documents/sketchbook/test/test.ino:50:41: warning: multi-character character constant [-Wmultichar]
 const char  practicechar_array [5]=  {  '15','160','17','18','\0'};  // the const does not seem to help
                                         ^~~~
/home/me/Documents/sketchbook/test/test.ino:50:46: warning: character constant too long for its type
 const char  practicechar_array [5]=  {  '15','160','17','18','\0'};  // the const does not seem to help
                                              ^~~~~
/home/me/Documents/sketchbook/test/test.ino:50:52: warning: multi-character character constant [-Wmultichar]
 const char  practicechar_array [5]=  {  '15','160','17','18','\0'};  // the const does not seem to help
                                                    ^~~~
/home/me/Documents/sketchbook/test/test.ino:50:57: warning: multi-character character constant [-Wmultichar]
 const char  practicechar_array [5]=  {  '15','160','17','18','\0'};  // the const does not seem to help
                                                         ^~~~
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: narrowing conversion of '13369' from 'int' to 'char' inside { } [-Wnarrowing]
 char input_status[6] =  {    'ldrstatus49','ldrstatus50','ldrstatus51','ldrstatus52','ldrstatus53'  };
                                                                                                     ^
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: narrowing conversion of '13616' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: narrowing conversion of '13617' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: narrowing conversion of '13618' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: narrowing conversion of '13619' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:42:101: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: narrowing conversion of '13369' from 'int' to 'char' inside { } [-Wnarrowing]
 char char_array[7] = {    'ldrPin49','ldrPin50','ldrPin51','ldrPin52','ldrPin53' ,'\0'    };
                                                                                           ^
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: narrowing conversion of '13616' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: narrowing conversion of '13617' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: narrowing conversion of '13618' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: narrowing conversion of '13619' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:48:91: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:50:66: warning: narrowing conversion of '12597' from 'int' to 'char' inside { } [-Wnarrowing]
 const char  practicechar_array [5]=  {  '15','160','17','18','\0'};  // the const does not seem to help
                                                                  ^
/home/me/Documents/sketchbook/test/test.ino:50:66: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:50:66: warning: narrowing conversion of '13872' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:50:66: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:50:66: warning: narrowing conversion of '12599' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:50:66: warning: overflow in implicit constant conversion [-Woverflow]
/home/me/Documents/sketchbook/test/test.ino:50:66: warning: narrowing conversion of '12600' from 'int' to 'char' inside { } [-Wnarrowing]
/home/me/Documents/sketchbook/test/test.ino:50:66: warning: overflow in implicit constant conversion [-Woverflow]

@mrphysh
The main question is - why do you think that the array of boolean variables should be type of char ?

[quote="mrphysh, post:1, topic:1296234"]

boolean ldrstatus53 = 0; 
boolean ldrstatus52 = 0; 
boolean ldrstatus51 = 0; 
boolean ldrstatus50 = 0; 
boolean ldrstatus49 = 0; 
char input_status[6] =  {    'ldrstatus49','ldrstatus50','ldrstatus51','ldrstatus52','ldrstatus53'  };

Why not to put the booleans to the boolean array?

boolean input_status[6] =  {    ldrstatus49, ldrstatus50, ldrstatus51, ldrstatus52, ldrstatus53  };

The same about this:

If you need an array of pins, why did you store its names in the array rather than storing the values???

Take a look at these arrays:

and the code in:

It should be adapable and extensible to 35 pins.

const byte FirstPin = 2;
const byte LastPin = 52;

uint64_t input_status = 0;
uint64_t old_status = 0;

void setup()   {
  Serial.begin(115200);
  Serial.println("Start");
}

void loop() {
  for ( int i = FirstPin; i <= LastPin; i++ ) {// of course this step can be omitted
    bitWrite(input_status, i, digitalRead(i));
  }

  for ( int i = FirstPin; i < ldrPinStart; i++ ) {
    bool b = bitRead(input_status, i);
    if (b != bitRead(old_status, i)) {
      Serial.print ("ldrPin");
      Serial.print (i);
      Serial.println (" changed") ;
      Serial.flush();
      bitWrite(old_status, i, b);
    }
  }
  delay (2000);
}
1 Like

The ON / OFF status of 35 pins could be stored in 7 bits of 1 byte. The lower 6 bits could hold a value of 0 ~ 63, one of the upper 2 bits could store the status of that pin (1 or 0).

The real theme is. if you have looked at the project, I want to evaluate the position of the laser by a careful look at the behavior of the laser as the magnetic needle is swinging. The software development pretty much requires cleaning up the 33 'if' statements. I will be quiet until I have processed this new information.

The data goes to Python and then into a sqlite3 database. I am having “no attribute ‘serial’ ” errors. This appears to be module , Python version, and OS issues. I have learned the “python -m pip install “ stuff and the “pip list” And go around and around with the install and uninstall for pyserial and serial and I need ‘sqlite3’ and ‘datetime’ , ‘sys’ and ‘random’ would be nice.
My computer had Python 3.7. I uninstalled that and loaded Python 3.12. I use IDLE. This project is on a dedicated HP notebook with a Win 8.1 OS.
I think I need a software download strategy. Like uninstall everything. Install this version of Python and then add these libraries.
The Arduino side looks fine. The serial monitor tells me that I am moving forward.
The array business is still under development, but moving forward.
This is working: much development ahead

/*
//there is an array for the pins boolboolean pin_status[40]    ==  {  etc
and another for the associated value
//   float location_values [40]  =  {   etc
*/
if (now_time > old_time) {
    digitalWrite (relayPin, LOW);    //  low is on ..  laser on 
    for ( int counter = 0; counter < 30  ; counter++ ) {
      bool_hold = digitalRead(pin_status[counter]);
      if (bool_hold == 1) {
        Serial.println (location_values [counter]);    //  if it picks up two values, it will send out two.  that might be cool..
        delay (30);
      }
      digitalWrite(relayPin, HIGH);    //turn off the laser

      now_time = millis();
      delay (100);

      old_time = (now_time + 300000);//  300000 would be 5 minutes  this establishes t

    }

Read deeply about the use of millis(), as this(forward addition) will run you into grief when millis() rolls over.
It appears you're running this on a Mega2560 or similar, so the default int is 16 bit. As a result, a constant of 300000 should be denoted as 300000UL, as it's greater than 32767, the maximum of the default signed int.
There may be other things to observe, but you'd have to post your full code for context, otherwise we are likely "fiddling while Rome burns".

2 Likes