I am working on Elegoo kit on sonic distance measuring program called SR04. I can't match up some of the names. Below is the attachment of the SR04.h and SR04.cpp. I put the question in RED as comment inside the file.
Thanks
I am working on Elegoo kit on sonic distance measuring program called SR04. I can't match up some of the names. Below is the attachment of the SR04.h and SR04.cpp. I put the question in RED as comment inside the file.
Thanks
Instead of the #define
you can indeed use int
. I think it's mostly historical.
Because it relates to timing and millis, it is better to use unsigned int
. And because it's constant you can add const
.
const unsigned int defaultDelay = 10;
I'm not sure whay you mean by "what is HIGH". The L in 15000L indicates that the value is a long.
pulseIn
is a function that you an fond in the Arduino reference.
Thanks for the reply
I cannot find "HIGH" is defined anywhere, that's my question.
You see the line:
_duration = pulseIn(_echoPin, HIGH, PULSE_TIMEOUT);
Nowhere I see any mention of pulseIn and HIGH anywhere.
the word pulseIn()
has parenthesis behind the word
pulseIn ()
these paranthesis inidicate it is a function.
HIGH is simply a predefined name for "1"
the meaning is logic level HIGH same as a single bit with value 1
opposite a single bit with value 0 is LOW
IO-pins are ussually checked this way
if (digitalRead(myButtonPin) == HIGH);
check if the IO-pin with name myButtonPin has logic level 1 / HIGH
if (digitalRead(myButtonPin) == 1);
best regards Stefan
Ah, pulseIn() is a function provided by Arduino, just like digitalRead().
Ha ha, HIGH is just "1". How can I not know that?!!!
Thanks
HIGH (as well as many other constants) is defined in Arduino.h line 40.
Thanks for all the replies. I still have a question in SR04.cpp, just a few lines from the top:
"long SR04::Distance() "
My understanding is SR04 is a CLASS, SR04::Distance() declare Distance() an INSTANCE of class SR04.
You can see SR04 has 2 integer parameters (int echoPin, int triggerPin), why there is a "long" in front of that?
Thanks
No. Distance() is a member function of the SR04 class. Think of the :: as meaning "is owned by".
Because the Distance() function returns a long data type.
HIGH and LOW are the values returned by digitalread() as per the documentation
they are described as part of the constants
HIGH
The meaning of
HIGH
(in reference to a pin) is somewhat different depending on whether a pin is set to anINPUT
orOUTPUT
. When a pin is configured as anINPUT
withpinMode()
, and read withdigitalRead()
, the Arduino (ATmega) will reportHIGH
if:** a voltage greater than 3.0V is present at the pin (5V boards)*
** a voltage greater than 2.0V is present at the pin (3.3V boards)*A pin may also be configured as an INPUT with
pinMode()
, and subsequently made HIGH withdigitalWrite()
. This will enable the internal 20K pullup resistors, which will pull up the input pin to aHIGH
reading unless it is pulledLOW
by external circuitry. This can be done alternatively by passingINPUT_PULLUP
as argument to thepinMode()
function, as explained in more detail in the section "Defining Digital Pins modes: INPUT, INPUT_PULLUP, and OUTPUT" further below.When a pin is configured to OUTPUT with
pinMode()
, and set toHIGH
withdigitalWrite()
, the pin is at:** 5 volts (5V boards)*
** 3.3 volts (3.3V boards)*In this state it can source current, e.g. light an LED that is connected through a series resistor to ground.
the doc does not mention the type nor the value, so you should not work under the assumption it's 1 and 0 and always will.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.