Hi i am using ESP32 WROOM module, i have declare a global boolean variable, and i call the variable in the loop section and i got an error massage "J does not name a type "
boolean J = 1;
void setup() {}
void loop() {
J = digitalRead(tacSwitch);
}
Hi i am using ESP32 WROOM module, i have declare a global boolean variable, and i call the variable in the loop section and i got an error massage "J does not name a type "
boolean J = 1;
void setup() {}
void loop() {
J = digitalRead(tacSwitch);
}
As far as the integer values of true and false, some languages have rigid definitions, some languages have no definitions, and some languages it's implementation specific.
I believe the latter is the case with C++ and it changes to match the processor architecture.
For instance, on the propeller MCUs true is defined as 32 1s in binary. In 2's compliment, that number is -1. For an unsigned integer, that number is the maximum integer value.
Whereas false is 32 0s, which is just zero in either number system.
Which is a long-winded way of saying, just use true and false.
@opk you posted in the wrong section, so I moved it here.
You posted in the getting the IDE working section.
bool
is the actual official type to use and true and false the values that are to be used if you don’t want to depend on promotion
digitalRead()
does not return a bool so the proper way to code (but few do it this way) would be
bool tackSwitchIsOn = (digitalRead(tacSwitch) == LOW); // assuming INPUT PULL-UP
(J is a very bad global variable name by the way)
some do.
constexpr byte tacSwitch {4};
bool tackSwitchIsOn;
void setup() {
Serial.begin(115200);
pinMode(tacSwitch, INPUT_PULLUP);
}
void loop() {
tackSwitchIsOn = digitalRead(tacSwitch) == LOW ? true : false;
Serial.println(tackSwitchIsOn);
}
@opk digitalRead returns LOW or HIGH.
Don't make assumptions what LOW or HIGH is.
Check for a value based on the Reference of Arduino and set your variable to a value the variable supports.
Y U NO COMPILE?
i try your example it work fine when i select Arduino Mega board but it work when i select ESP32 WROOm board.
hum ... digitalRead(tacSwitch) == LOW
is already a boolean value (result of a comparison) so you don't need the ternary.
but if you want to use the conditional operator (colloquially referred to as ternary conditional or ternary operator), you could do
Serial.println(tackSwitchIsOn ? F("true") : F("false"));
ok i will try that
I got the error:
/Users/john/Documents/Arduino/sketch_jul22a/sketch_jul22a.ino: In function 'void loop()':
sketch_jul22a:7:19: error: 'tacSwitch' was not declared in this scope
J = digitalRead(tacSwitch);
^~~~~~~~~
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.