`const int TriggerPin = 8; //Trig pin
const int EchoPin = 9; //Echo pin
long Duration = 0;
long Distance_mm = Distance(Duration);
void setup(){
pinMode(TriggerPin,OUTPUT); // Trigger is an output pin
pinMode(EchoPin,INPUT); // Echo is an input pin
Serial.begin(9600); // Open a connection to the Serial monitor
pinMode(13, OUTPUT);
}
void loop(){
digitalWrite(TriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin, HIGH); // Trigger pin to HIGH
delayMicroseconds(10); // 10us high
digitalWrite(TriggerPin, LOW); // Trigger pin to HIGH
Duration = pulseIn(EchoPin,HIGH); // Waits for the echo pin to get high
// returns the Duration in microseconds
// Use function to calculate the distance
Serial.print("Distance = "); // Output to serial
Serial.print(Distance_mm);
Serial.println(" mm");
delay(1000); // Wait to do next measurement
}
if(long Distance<100){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
long Distance(long time)
}
{
// Calculates the Distance in mm
// ((time)*(Speed of sound))/ toward and backward of object) * 10
long DistanceCalc; // Calculation variable
DistanceCalc = ((time*0.343) / 2); // Actual calculation in mm
//DistanceCalc = time / 74 / 2; // Actual calculation in inches
return DistanceCalc; // return calculated value
}`
Hi guys, I'm new to coding, and I keep getting error messages which I can't understand, (I also can't really understand some of this code), I'm hoping for some guidance on how to resolve the errors. I'm trying to make it so that when the sensor detects something less than a 100mm to it, it turns the LED on.
// These lines are good
const int TriggerPin = 8; //Trig pin
const int EchoPin = 9; //Echo pin
long Duration = 0;
long Distance_mm;
// Remove this line. It should not be in this area.
// It is calling a function and expecting a return value.
// I have commented the line out.
// long Distance_mm = Distance(Duration);
// These lines are good.
void setup() {
pinMode(TriggerPin, OUTPUT); // Trigger is an output pin
pinMode(EchoPin, INPUT); // Echo is an input pin
Serial.begin(9600); // Open a connection to the Serial monitor
// Let the reader know this is an LED in the comment field
pinMode(13, OUTPUT); // LED_BUILTIN
}
// These lines are good.
void loop() {
digitalWrite(TriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin, HIGH); // Trigger pin to HIGH
delayMicroseconds(10); // 10us high
digitalWrite(TriggerPin, LOW); // Trigger pin to HIGH
Duration = pulseIn(EchoPin, HIGH); // Waits for the echo pin to get high returns the Duration in microseconds
// At this point your copy/paste went wrong.
// You pasted the external function "Distance" into the loop() function.
// This is how the distance function should have been called
// Use function to calculate the distance
Distance_mm = Distance(Duration);
// Calculates the Distance in mm
// ((time)*(Speed of sound))/ toward and backward of object) * 10
if (Distance < 100) {
digitalWrite(13, HIGH); // turn LED on
}
else {
digitalWrite(13, LOW); // turn LED off
}
Serial.print("Distance = "); // Output to serial
Serial.print(Distance_mm);
Serial.println(" mm");
delay(1000); // Wait to do next measurement
}
// This function was pasted into the loop() function...
// Moved here to be used as an external function
// This function will receive a 'long' and call it 'time' ...
// ... and return a 'long' locally called 'DistanceCalc'
long Distance(long time) {
// Calculation variable
long DistanceCalc = ((time * 0.343) / 2); // Actual calculation in mm
//DistanceCalc = time / 74 / 2; // Actual calculation in inches
return DistanceCalc; // return calculated value
}
You are stuck with printing, Hello, World!... but what if you wanted something different?
In the original program, the calculation was moved "externally" so you could make changes to the "external function" without disturbing the original program... so this example might look like this...
int count; // global variable
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Hello, ");
extfun(); // this calling an external function
delay(500); // slow the output down
}
void extfun() { // this is the function being call from inside 'loop()'
Serial.println("World of Warcraft!");
}