I am trying to make a ultrasonic sensor by making a function and calling it at another point as I plan on adding multiple sensors. (I want it to be concise so I want to avoid just repeating the code).
I keep getting the error " Ultrasonic_sensor_FINAL:19:3: error: 'cm' does not name a type
cm = microsecondsToCentimeters(duration);
^~
Ultrasonic_sensor_FINAL:20:3: error: 'Serial' does not name a type
Serial.print(cm);
^~~~~~
Ultrasonic_sensor_FINAL:21:3: error: 'Serial' does not name a type
Serial.print("cm");
^~~~~~
Ultrasonic_sensor_FINAL:22:3: error: 'Serial' does not name a type
Serial.println();
^~~~~~
Ultrasonic_sensor_FINAL:23:8: error: expected constructor, destructor, or type conversion before '(' token
delay(100);
^
Ultrasonic_sensor_FINAL:24:2: error: expected declaration before '}' token
}
^
exit status 1
'cm' does not name a type
"
This is my code:
#include <NewPing.h>
const int pingPin1 = 7;
long ping(int pinNumber)
{
long duration, cm;
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
delayMicroseconds(2);
digitalWrite(pinNumber, HIGH);
delayMicroseconds(5);
digitalWrite(pinNumber, LOW);
pinMode(pinNumber, INPUT);
duration = pulseIn(pinNumber, HIGH);}
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds){
return microseconds / 29 / 2;
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
long ping1;
ping1 = ping(pingPin1);
//long cm = microsecondsToCentimeters(duration);
}
Hi, @niro282
Thanks for using code tags.
Why is it that you load the NewPing library, but don't use it?
If you want to add multiple ultrasonic units use the library, there is an example in the IDE for three HC-SR04 ultrasonic units.
Oh I was testing different solutions and one was import a library, but it didn't work.
Sorry I'm pretty new to ardunio, what's the IDE?
I'm using the standard one on tinkercad, but the code does not even compile
I screwed up the brackets(thanks for the Ctrl+T tip) but now I've fixed what should be in the function, now it says:
"microsecondsToCentimeters' was not declared in this scope"
const int pingPin1 = 7;
long ping(int pinNumber)
{
long duration, cm;
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
delayMicroseconds(2);
digitalWrite(pinNumber, HIGH);
delayMicroseconds(5);
digitalWrite(pinNumber, LOW);
pinMode(pinNumber, INPUT);
duration = pulseIn(pinNumber, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
long ping1;
ping1 = ping(pingPin1);
//long cm = microsecondsToCentimeters(duration);
}
...as mentioned in post #2... and the OP fixed that, but in the process introduced another error (deleted a bracket they shouldn't have). Also didn't fix the 2nd error mentioned in post #2.
Hi,
The Arduino IDE, editor, has paths to example codes for the libraries.
Here is the NewPing multi sensor code for 3 units.
// ---------------------------------------------------------------------------
// Example NewPing library sketch that pings 3 sensors 20 times a second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define SONAR_NUM 3 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(6, 7, MAX_DISTANCE),
NewPing(8, 9, MAX_DISTANCE)
};
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print(i);
Serial.print("=");
Serial.print(sonar[i].ping_cm());
Serial.print("cm ");
}
Serial.println();
}