Is this "legal"? Will it work?

So I have a case where I want to loop on some digitalWrite statements instead of writing 6 blocks of digitalWrite statements. Each time I pass through the loop I want to be able to change whether the data is HIGH or LOW.

So, I have defined the following string variables:

String(STRING1) = "LOW";

String(STRING2) = "HIGH";

And the million dollar question is can I do this:

digitalWrite(BYTE1, STRING1);

digitalWrite(BYTE3, STRING2);

Before entering the loop I would set STRING1 and STRING2 to either HIGH or LOW so that when it's used in the Write statement it would vary depending on what I need those pins to do.

Hope this makes sense.

I didn't put this into a CODE paragraph because it's not really code, just an example of what I wanted to do. Please enlighten me and thanks!

Did you read the documentation for the digitalWrite() instruction? The first parameter is the pin number, remember?

maybe you can, but for sure it would not do, what you want it to do.

try something like this instead:

#define led13 13
#define led12 12

byte val1, val2;

val1 = LOW;
val2 = HIGH;

while (i<10) { // or other kind of loop, or loop() function or what :)
  digitalWrite(led13, val1);
  digitalWrite(led12, val2);
};

You will get compiler errors :smiley: See below demo; you did not specify what the type of BYTEx is so I used a hard coded pin number.

String(STRING2) = "HIGH";

void setup()
{
  digitalWrite(13, STRING2);
}

void loop()
{
  // put your main code here, to run repeatedly:
}
Compiling sketch...
"C:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.8\\cores\\arduino" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.8\\variants\\mega" "C:\\Users\\bugge\\AppData\\Local\\arduino\\sketches\\9718A765F4EF80B8E62D803A7A8A998E\\sketch\\sketch_jul1a.ino.cpp" -o "C:\\Users\\bugge\\AppData\\Local\\arduino\\sketches\\9718A765F4EF80B8E62D803A7A8A998E\\sketch\\sketch_jul1a.ino.cpp.o"
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved202661-6172-rln8jt.stfa\sketch_jul1a\sketch_jul1a.ino: In function 'void setup()':
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved202661-6172-rln8jt.stfa\sketch_jul1a\sketch_jul1a.ino:5:27: error: cannot convert 'String' to 'uint8_t {aka unsigned char}' for argument '2' to 'void digitalWrite(uint8_t, uint8_t)'
   digitalWrite(13, STRING2);
                           ^
exit status 1

Compilation error: cannot convert 'String' to 'uint8_t {aka unsigned char}' for argument '2' to 'void digitalWrite(uint8_t, uint8_t)'

This tells me (and now you as well) that digitalWrite expects an uint8_t (aka byte) for the second argument.

The documentation (reference) does not tell one what the type of the variables is :wink:

No.
The second argument has to be a value that evaluates to an integer (enum on some CPUs, but still an integer.)

What board are you using? If you turn up the Compiler warning level from Default to More or All, it might say

warning: unnecessary parentheses in declaration of 'STRING1' [-Wparentheses]
    1 | String(STRING1) = "LOW";
      |       ^~~~~~~~~

What are those parentheses supposed to be doing anyway?

Please explain why do you want to do that with strings?
The task that you have in your mind can be done easily if you define your HIGH and LOW variables as integers.

I'm sorry to say in my opinion you need to study programming languages before trying to do thing you don't know.
The symbols LOW and HIGH aren't strings, they just represent the status of a digital pin, and LOW equals to the integer value 0, HIGH to 1, i.e. any positive voltage level applied to a pin is HIGH, and if you want to output a digital signal, LOW is 0V and HIGH is Vcc (either 5V or 3.3V depending on the board you're working on).

Said that, if you have a bunch of 6 "almost equal" pins to control, please have a look a the "arrays" in C/Arduino. If you want to control 6 output pins you should use statements like (this is just a small example):

byte outPin[] = {3, 4, 5, 6, 7, 8};
byte outStatus[] = {LOW, LOW, HIGH, LOW, HIGH, LOW};
...
void setup() {
  for (int i=0; i<=6; i++)
    pinMode(outPin[i], OUTPUT);
...
}

void loop() {
...
  // Write the current status configuration
  for (int i=0; i<=6; i++)
    digitalWrite(outPin[i], outStatus[i]);
...
}

And that would probably become a struct, maybe even with bit fields.

@nstobert, you can ignore this if you're not ready for it.

Please don't clutter the thread.
This is a bad idea for promoting your project.
It's called spam and that can get you banned.

So, I have defined the following string variables:

String(STRING1) = "LOW";

String(STRING2) = "HIGH";

And the million dollar question is can I do this:

digitalWrite(BYTE1, STRING1);

digitalWrite(BYTE3, STRING2);

its not illegal, but also not working ... because string "HIGH" is not value 0 or 1 but character "H" "I" "G" "H"

so function expect value 1 and you write text!

so ... seem a big confusion from char and integer

  • digitalWrite() expects a numeric constant, not text

So the compiler cannot meaningfully convert "HIGH" → 1.

ok... scuse me!

Yes, I agree, but I always try to tailor my answer to the questioner's expertise: talking about "struct" to someone who isn't yet familiar with basic and common symbols like LOW and HIGH, in my opinion, is just a way to confuse them further... :wink: :smiling_face_with_sunglasses:

Hence my comment "you can ignore this if you're not ready for it." :wink:

I admire the creativity. There are languages where passing "LOW" and "HIGH" like that (or something very similar) is perfectly reasonable. C++ just isn't one of them. :expressionless: Rather than trying to make Strings stand in for LOW and HIGH, make two variabkes that store the actual values:

  auto V1 = LOW;
  auto V2 = HIGH;

Then update them as needed and pass them directly to digitalWrite(). auto deduces the correct type without depending on how the Arduino core happens to define LOW and HIGH. Which at this level strictly speaking is not something you can know. The API makes the rulez.

So set those variabkes wherever you would have done the Sstring variable

  V1 = HIGH;
  V2 = HIGH;

and use them in your digitalWrite() statements

digitalWrite(BYTE1, V1);

digitalWrite(BYTE3, V2);

a7

People

Before this goes wrong, please keep it friendly.

If you really need strings, you can translate their interpretation into something that digitalWrite() understands:

String(STRING1) = "LOW";
String(STRING2) = "HIGH";

int BYTE1 = 12;
int BYTE3 = 13;

void setup() {
  Serial.begin(115200);
  pinMode(BYTE1, OUTPUT);
  pinMode(BYTE3, OUTPUT);
}

void loop() {
  STRING1 = random(2) ? "HIGH" : "LOW";
  STRING2 = random(2) ? "HIGH" : "LOW";

  Serial.print(STRING1);
  Serial.println(STRING2);

  digitalWrite(BYTE1, STRING1 == "HIGH" ? HIGH : LOW);
  digitalWrite(BYTE3, STRING1 == "HIGH" ? HIGH : LOW);
  delay(1000);
}

But you'd be much better off avoiding strings and using a digitalWrite() compatible variables instead. Perhaps INT1=HIGH; INT2=LOW; in your scheme.

First obvious thing here is that you're not 100% on digitalWrite.

So you go to the Arduino Reference... bookmark it!

And you click on the function in question and get complete answers with example. Try it, refresh your memory easily! Open a tab to the Reference Page and click digitalWrite.

What is not covered there may be found on other pages that you want to bookmark.

AVR GCC standard libc page, docs links there.

The complete reference for the standard C libraries Arduino uses are in those doc pages though the project is on Github.

When you code, open a browser and keep tabs loaded with help pages for fast back and forth referencing. This will speed you up and let you ask better questions, fewer questions too! When I'm not sure-sure, it saves me time to check.. usually just getting the syntax right is all I need.

Arduino Main Site ( arduino.cc ) has a complete whole manual with tutorials online, this forum is in addition to that.

Standard C libraries like <string.h> keeps text in arrays as chars or bytes where the end of text is marked by a NULL (= 0) byte. C uses the text data kept in arrays.

C++ String keeps the same array locked inside of a String Object that controls that text through String functions and extra bytes not needed with C text-array strings. In a large RAM and code environment they are very good but in an AVR they are not.

On AVRs we can store byte arrays in flash (through PROGMEM) and EEPROM (eeprom.h library) but we can't store Strings, just what's in them. Flash and EEPROM extend AVR memory, store constants like labels and constant numbers there instead of RAM.

Sometimes I look on the web for answers on Arduino. Nick Gammon's site is a GOLD MINE of HowTo.

same

The forum used to have links across the top, one of which was LEARN. Now you click on ARDUINO.CC and navigate from there.

I miss those days only 10 years ago.