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?
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
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.
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?
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.
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
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.
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.