Total noob here, followed a tutorial but getting an error message?

Hey! I was following a tutorial for a HC-SR04 Ultrasonic sensor

This one to be specific: - YouTube

I'm pretty sure I copied the code from the tutorial exactly but here it is:

#define trigPin 3
#define trigPin 2
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.print(" cm");
delay(500);
}

When I try to verify it I get the following error message:

ultrasonic_distance_sensor_basic.ino: In function 'void setup()':
ultrasonic_distance_sensor_basic.ino:7:11: error: 'echoPin' was not declared in this scope
ultrasonic_distance_sensor_basic.ino: In function 'void loop()':
ultrasonic_distance_sensor_basic.ino:17:20: error: 'echoPin' was not declared in this scope
Error compiling.

In the video the code works perfectly can someone let me know what I did wrong?

Look at the first two lines. :slight_smile:

#define trigPin 3
#define trigPin 2

Those two lines? Whats wrong with them? Thats exactly what the guy put... I'm a total beginner so I don't understand what most of this code means. I just know some basic java

In Java, what happens if you assign two different values to a variable, one after the other?

The first value gets over written

if int a = 3; then I type a = 4; a is now 4, but where am I doing that? is pinMode a variable?

nevermind I finally noticed I typed trigPin twice instead of trigPin 2 echoPin 3.... feel like I should've noticed that 20 minutes ago lol

thanks

Lemme ask it a different way. In java, can you give two variables in the same scope the same name? Could they both be trigPin in java?

Is there anything defined anywhere in your sketch called echoPin?

no yea, I didn't notice that I typed:

#define trigPin 3
#define trigPin 2

The whole time I thought that it was:

#define trigPin 3
#define echoPin 2

I also didn't know that those were variables... I thought the hashtag sign was the equivalent of // in java.. Thought those were comments

thanks for the help guys!

Qwexi:
The first value gets over written

if int a = 3; then I type a = 4; a is now 4, but where am I doing that? is pinMode a variable?

nevermind I finally noticed I typed trigPin twice instead of trigPin 2 echoPin 3.... feel like I should've noticed that 20 minutes ago lol

thanks

Right answer, wrong troubleshooting methods.

The somewhat cryptic error message clearly stated the symbol is nowhere to be found ( in right place ) .

One suggestion given was explaining ( incorrectly) the results of the error, not the cause of the error. Why?
You already knew the result - it did not compile.
Than you run / calculated / or did whatever some test and even speculated about "pinMode".

I am not trying to be "a general after the battle" , but when a tool - compiler - gives you a error message it is better to react to the message instead of going on wild goose chase.

Just saying, nothing personal.

Qwexi:
I also didn't know that those were variables... I thought the hashtag sign was the equivalent of // in java.. Thought those were comments

thanks for the help guys!

Sine you're coming from java I'll explain this a little more. They're not actually variables. A variable would be:

int echoPin = 3;

In C++ we get a preprocessor. What the #define does is tell it to go through the code and replace every instance of "echoPin" with "2" before the code goes to compilation. It's not unlike using Find and Replace in a text editor.

Delta_G:
Sine you're coming from java I'll explain this a little more. They're not actually variables. A variable would be:

int echoPin = 3;

In C++ we get a preprocessor. What the #define does is tell it to go through the code and replace every instance of "echoPin" with "2" before the code goes to compilation. It's not unlike using Find and Replace in a text editor.

Ah, I had no idea that's really helpful thanks!

so going from that I'm assuming that

#define trigPin 3 and pinMode(trigPin, OUTPUT);

becomes pinMode(3, OUTPUT);

and basically that just sets that pin# to be an output?

Ah, I had no idea that's really helpful thanks!

I suspect that this sentence needs so more punctuation...

Qwexi:
Ah, I had no idea that's really helpful thanks!

so going from that I'm assuming that

#define trigPin 3 and pinMode(trigPin, OUTPUT);

becomes pinMode(3, OUTPUT);

and basically that just sets that pin# to be an output?

When in doubt about a line of code, never be afraid to read the doc. In this case the function name and parameter make it pretty obvious. That isn't always the case.

But you are correct about what that line turns into before the compiler sees it. The word trigPin gets replaced by a 3.

I think this is what the others were saying but based on what I think you are trying to do, the #define is not needed. However, trigPin and echoPin are variables; their state will change, so you need to put int before both of them, along with = 3 and = 2 otherwise it might think you are naming pin 3 trigPin3 without saying what pin it is. :slight_smile: :slight_smile:

EQOxx123:
I think this is what the others were saying but based on what I think you are trying to do, the #define is not needed. However, trigPin and echoPin are variables; their state will change, so you need to put int before both of them, along with = 3 and = 2 otherwise it might think you are naming pin 3 trigPin3 without saying what pin it is. :slight_smile: :slight_smile:

It's perfectly fine to use a define there. The pin numbers won't change. The states of the pins might change, but the state would be held in different variables if they were held in variables at all.

Qwexi:
#define trigPin 3
#define trigPin 2

Those two lines? Whats wrong with them? Thats exactly what the guy put... I'm a total beginner so I don't understand what most of this code means. I just know some basic java

Should be
#define trigPin 3
#define echoPin 2

(or the other way around)

EQOxx123:
I think this is what the others were saying but based on what I think you are trying to do, the #define is not needed. However, trigPin and echoPin are variables; their state will change, so you need to put int before both of them, along with = 3 and = 2 otherwise it might think you are naming pin 3 trigPin3 without saying what pin it is. :slight_smile: :slight_smile:

Well , from what I figure as typo error we learned #define basic - preprocessor replaces the symbol with assigned value.
Now is the time to find out WHY using #define in this simple form.
It does not add much to simple few pages of code, but...

Let's assume that you have few thousands lines of code , using several #include ( another useful preprocessor directive ) files, all making references to echoPin.
Than you want to change echoPin to 53 ( ARM /Due ).
So instead of doing "find / replace " "3" with "53" ( and in process replacing wrong 3 with 53 ) you just change single line of code - #define echoPin 3 with #define echoPin 53 - and you are done.

End of lesson. Homework assignment - write macro using #define with parameters. Class dismissed.

End of lesson. Homework assignment - write macro using #define with parameters. Class dismissed.

Response: Tell the instructor to piss it up a rope. Use functions, which as so much more obvious, and which the compiler will optimize far better.