Trying to be smart, using an array... but did not work out

Im trying to write more efficient code, but I get
cannot convert 'String' to 'uint8_t {aka unsigned char}' for argument '1' to 'void digitalWrite(uint8_t, uint8_t)'

the code will tell it all,
Is there a way to do this right?
Thank you friends,
Mitch



// RELAY BUTTONS
#define MOTOR_ON_PIN 24  // WHITE- K1
#define MOTOR_DIR_PIN 26  // GRAY K2
#define CLUTCH_UP_PIN  28 // PUROPLE K3
#define CLUTCH_KEAD_PIN  46 //BLUE K4
#define CLUTCH_TAP_PIN 48 // GREEN K5
#define K6_PIN 52   // YELLOW K6
#define K7_PIN 50   // ORANGE K7
#define K8_PIN 22   // BROWN  K8

String Outputs [] =  {"0", "MOTOR_ON_PIN", "MOTOR_DIR_PIN", "CLUTCH_UP_PIN","CLUTCH_KEAD_PIN","CLUTCH_TAP_PIN","K6_PIN","K7_PIN","K8_PIN","","",  };

void setup() {
 Serial.begin (115200);

pinMode(MOTOR_ON_PIN, OUTPUT);
pinMode(MOTOR_DIR_PIN, OUTPUT);
pinMode(CLUTCH_UP_PIN, OUTPUT);
pinMode(CLUTCH_KEAD_PIN, OUTPUT);
pinMode(CLUTCH_TAP_PIN, OUTPUT);
pinMode(K6_PIN, OUTPUT);
pinMode(K7_PIN, OUTPUT);
pinMode(K8_PIN, OUTPUT);

for (int i=1; i<9; i++)
{
digitalWrite(Outputs[i], HIGH);
delay (1000);
}

 
}

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

}

don't use an array of Strings and loose the double quotes

const byte outputPins[] =  {MOTOR_ON_PIN, MOTOR_DIR_PIN, CLUTCH_UP_PIN,CLUTCH_KEAD_PIN,CLUTCH_TAP_PIN,K6_PIN,K7_PIN,K8_PIN };
const byte outputPinsCnt = sizeof outputPins / sizeof * outputPins;

...

for (byte i = 0; i< outputPinsCnt; i++) digitalWrite(outputPins[i], HIGH);

Great, thanks,
I took it a step farther,
is this correct?

/*
pinMode(MOTOR_ON_PIN, OUTPUT);
pinMode(MOTOR_DIR_PIN, OUTPUT);
pinMode(CLUTCH_UP_PIN, OUTPUT);
pinMode(CLUTCH_KEAD_PIN, OUTPUT);
pinMode(CLUTCH_TAP_PIN, OUTPUT);
pinMode(K6_PIN, OUTPUT);
pinMode(K7_PIN, OUTPUT);
pinMode(K8_PIN, OUTPUT);
*/
for (int i=0; i<8; i++)
{
pinMode(Outputs[i], OUTPUT);
}

look this over
array of structs

// see chapter 6 of The C Programming Language
// http://cslabcms.nju.edu.cn/problem_solving/images/c/cc/The_C_Programming_Language_%282nd_Edition_Ritchie_Kernighan%29.pdf

struct But {
    const byte  Pin;
    const char *desc;
    const char *label;
};

But but [] {
#if 0
    { 24, "White  K1",  "MOTOR_ON_PIN" },
    { 26, "Gray   K2",  "MOTOR_DIR_PIN" },
    { 28, "Purple K3",  "CLUTCH_UP_PIN" },
    { 46, "Blue   K4",  "CLUTCH_KEAD_PIN" },

    { 48, "Green  K5",  "CLUTCH_TAP_PIN" },
    { 52, "Yellow K6",  "K6_PIN" },
    { 50, "Orange K7",  "K7_PIN" },
    { 22, "Broem  K8",  "K8_PIN" },
#else
    {  6, "White  K1",  "MOTOR_ON_PIN" },
    {  7, "Gray   K2",  "MOTOR_DIR_PIN" },
    {  8, "Purple K3",  "CLUTCH_UP_PIN" },
    {  9, "Blue   K4",  "CLUTCH_KEAD_PIN" },

    { 10, "Green  K5",  "CLUTCH_TAP_PIN" },
    { 11, "Yellow K6",  "K6_PIN" },
    { 12, "Orange K7",  "K7_PIN" },
    { 13, "Broem  K8",  "K8_PIN" },
#endif
};

const int Nbut = sizeof(but) / sizeof(But);

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (115200);

    for (int n = 0; n < Nbut; n++)  {
#if 0
        pinMode        (but [n].Pin, OUTPUT);
        digitalWrite   (but [n].Pin, HIGH);
#endif
        Serial.println (but [n].label);
        delay (1000);
    }
}


void loop() {

}

Thanks everyone, that worked great

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