Hi,
How do I use Char Arrays with pinMode?
I did the following:
Assigned the output pins;
#define RELAY1 4
#define RELAY2 5
#define RELAY3 6
#define RELAY4 7
#define RELAY5 8
#define RELAY6 9
#define RELAY7 10
#define RELAY8 11
The array;
const char *relays[] = {"RELAY1","RELAY2","RELAY3","RELAY4","RELAY5","RELAY6","RELAY7","RELAY8"};
Set the output pins to LOW to check response;
for (int i=0; i = 7; i++){
pinMode(relays[i],OUTPUT);
digitalWrite(relays[i],LOW);
}
and I get no response... If I use;
pinMode(RELAY1,OUTPUT);
digitalWrite(RELAY1,LOW);
etc.....
The relays respond fine.
Thanks
Pete
system
April 6, 2014, 10:08am
2
does it make a difference if you change:
const char *relays[] = {"RELAY1","RELAY2","RELAY3","RELAY4","RELAY5","RELAY6","RELAY7","RELAY8"};
into:
const char* relays[] = {"RELAY1","RELAY2","RELAY3","RELAY4","RELAY5","RELAY6","RELAY7","RELAY8"};
(i have changed the "*" so it is after char, and not before the array name)
try
const char relays[] = { RELAY1, RELAY2, RELAY3, RELAY4, RELAY5, RELAY6, RELAY7, RELAY8 };
MarkT
April 6, 2014, 10:16am
4
#define names are visible to the compiler at compile-time, but don't exist at
runtime. And names are not the same thing as strings (which are runtime
objects).
You need an array of int or byte:
#define RELAY1 4
#define RELAY2 5
#define RELAY3 6
#define RELAY4 7
#define RELAY5 8
#define RELAY6 9
#define RELAY7 10
#define RELAY8 11
byte relays[] = {RELAY1, RELAY2, RELAY3, RELAY4, RELAY5, RELAY6, RELAY7, RELAY8};
void setup ()
{
for (int i=0; i = 7; i++){
pinMode (relays[i], OUTPUT);
digitalWrite (relays[i], LOW);
}
..
}
Whitespace serves only to separate names, it is ignored:
const char *relays[]
parses the same as
const char* relays[]
thanks MarkT
It works a treat....
Thanks everyone else for their input too
system
April 6, 2014, 1:11pm
6
parses the same as
To the compiler. Not to me.
char *a, b;
looks to the compiler, and to me, that a is a pointer to char and that b is a char.
char* a, b;
looks to the compiler like a is a pointer to char and b is a char, but looks to me like a and b are pointers to char.
I always put the * with the variable. I never put it with the type.
problem is solved by using one declaration per line. No mistakes like this
char a, *b, **c, d[], *e[];
(OK I am exaggerating)