What kind of variable is a definition?

I have 16 relays assigned to various arduino pins. I try to use them as an array, but I dont get far...

#define K1 21 // Brown
#define K2 22 // ORANGE
#define K3 23 // BLACK 
#define K4 24 // YELLOW
#define K5 25 // WHITE
#define K6 26 // GREEN
#define K7 27 // GRAY
#define K8 28 // Blue
#define K9 29 // YELLOW
#define K10 30 // purPLE

#define K11 31 // ORANGE
#define K12 32 // PURPLE
#define K13 33 // GREEN
#define K14 34 // BLACK
#define K15 35 // GRAY
#define K16 36 // PURPLE

long TimeBucket= 300000;  // 300 sec

String Relay[17] = {"00","K1","K2","K3","K4","K5","K6","K7","K8","K9","K10","K11","K12","K13","K14","K15","K16"};


void setup() 
{

   Serial.begin(115200);

  for (int i=21; i<=37; i++)
  {pinMode(i , OUTPUT);
    }

    /*
pinMode(K1 , OUTPUT);
pinMode(K2 , OUTPUT);
pinMode(K3 , OUTPUT);
pinMode(K4 , OUTPUT);
pinMode(K5 , OUTPUT);
pinMode(K6 , OUTPUT);
pinMode(K7 , OUTPUT);
pinMode(K8 , OUTPUT);
*/

  for (int i=1; i<=16; i++)
  {digitalWrite (Relay[i]), HIGH);
  delay (500);
    }

  
}

I get an error
cannot convert 'String' to 'uint8_t {aka unsigned char}' for argument '1' to 'void digitalWrite(uint8_t, uint8_t)'

It is kinda expected, but what to do. I need to actuate the relays by for loop a lot ....

Thanks

String Relay[17] = {"00","K1" Why String?
Pin numbers are typically integers.

because I need to call not the pin numbers, but the relay pin such as K1 K2....

The #define is a simple text substitution that occurs before the code is compiled. The substitution does not replace anything within quotes.

You can create an array of type int or byte, but do not use quotes. Also, array elements are numbered starting with 0, trying to circumvent that does not simplify the code.

because I need to call not the pin numbers, but the relay pin such as K1 K2..

No, didn't get any of that, sorry.

digitalWrite (Relay[i]), HIGH)

Ignoring the mismatched parentheses, what parameters does digitalWrite accept?

String?

Nope.

A fixed version .

#define K1 21 // Brown
#define K2 22 // ORANGE
#define K3 23 // BLACK
#define K4 24 // YELLOW
#define K5 25 // WHITE
#define K6 26 // GREEN
#define K7 27 // GRAY
#define K8 28 // Blue
#define K9 29 // YELLOW
#define K10 30 // purPLE

#define K11 31 // ORANGE
#define K12 32 // PURPLE
#define K13 33 // GREEN
#define K14 34 // BLACK
#define K15 35 // GRAY
#define K16 36 // PURPLE

long TimeBucket = 300000; // 300 sec
byte Relay[17] = {0, K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16};


void setup()
{
  Serial.begin(115200);
  for (byte i = 1; i <= 16; i++)
  {
    pinMode(Relay[i] , OUTPUT);
  }
  for (byte i = 1; i <= 16; i++)
  {
    digitalWrite (Relay[i], HIGH);
    delay (500);
  }
}

void loop()
{
  // both if these are the same
  digitalWrite(K1, HIGH);
  digitalWrite(Relay[1], HIGH);
}

Apart from the wasted first element.
Why?

Apart from the wasted first element.
Why?

I often find beginners forgetting arrays are 0 indexed, so 'forgetting' the first element helps to get a project up and running.
And it was edited from existing code & structure.

wow , that was simple, I feel silly.

Thanks a lot
mitch

What I find funny is some beginners not using element zero of an array even though they know it exists. Recently a poster in the forum said that they did not use element zero because they "did not like it" and they wrote code with a dummy value in position zero and for loops starting at 1 accessing the array specifically to avoid using it

Yip, Until they need to start saving bytes!!!

No problem. They will have often declared the array as int anyway when it holds only bytes or even better it only holds 0 or 1, so plenty of scope for saving memory

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.