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!
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);
};
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]);
...
}
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...
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. 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
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.
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.