r/beginners_cpp • u/FosItek • Jul 23 '24
Pulseln only works in TestProgram, not in main program
Hi fellow programmers!
Im currently trying to interface a RCWL-1670 ultrasound distance sensor to my esp32, using VS Code and PlatformIO.
I made a program that is working (based on this guide: https://randomnerdtutorials.com/esp32-hc-sr04-ultrasonic-arduino/)
My test program read distance just as expected. (Ref. TestProgram)
However, my main program only output Pulseln = '0 (Ref. MainProgram)
Any reason why this make sense? Im reading around about other people with similar issues, is there a workaround?
1
u/FosItek Jul 23 '24
MainProgram --> Main.cpp:
Not able to paste whole file, but here is the function that I expect to read the distance sensor.
float MeasureWaterLevel(float WaterLevel){
for (int n=0; n < NumberOfMeasurements ; n++) {
digitalWrite(trigPin, LOW); // Clears the trigPin
delayMicroseconds(5);
delay(250);
digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 20 micro seconds
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
currWaterLevel = duration * SOUND_SPEED/2; // Calculate the distance
cout << "- Reading variable 'pulseIn = '" << pulseIn(echoPin, HIGH) << endl;
cout << "- Reading variable 'duration = '" << duration << endl;
cout << "- Sound speed =" << SOUND_SPEED << endl;
cout << "- Reading water level: n" << n << " = " << currWaterLevel << " cm";
WaterLevel = WaterLevel + currWaterLevel;
cout << " Adding to: " << WaterLevel << " cm";
cout << " /Averages: " << WaterLevel/(n+1) << " cm" << endl;
}
WaterLevel = WaterLevel/NumberOfMeasurements;
cout << endl << "Water level sensor summary: ";
cout << endl << " - Averages to: " << WaterLevel;
cout << endl << " - Sensor has a maximum allowed value = " << ErrLimSensor_High;
cout << endl << " - Sensor has a lowest allowed value = " << ErrLimSensor_Low << endl << endl << endl;
return WaterLevel;
}
void loop() { And so on....
1
u/jedwardsol Jul 23 '24
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, currWaterLevel = duration * SOUND_SPEED/2; cout << "- Reading variable 'pulseIn = '" << pulseIn(echoPin, HIGH) << endl;Does reading the pin reset it? Because this is calling
pulseIntwice. Perhaps this should be printingdurationinstead1
u/FosItek Jul 24 '24
Nope. I added only duration value output to serial first, then added another pulseIn reading after...
1
u/FosItek Jul 23 '24
Here is the serial output:
---- Opened the serial port COM3 ----
Reading variable 'pulseIn = '0
Reading variable 'duration = '0
Sound speed =0.343
Reading water level: n18 = 0 cm Adding to: 0 cm /Averages: 0 cm
Reading variable 'pulseIn = '0
Reading variable 'duration = '0
Sound speed =0.343
Reading water level: n19 = 0 cm Adding to: 0 cm /Averages: 0 cm
Reading variable 'pulseIn = '0
Reading variable 'duration = '0
Sound speed =0.343
Reading water level: n20 = 0 cm Adding to: 0 cm /Averages: 0 cm
---- Closed the serial port COM3 ----
1
u/FosItek Jul 25 '24
I found the issue.
I forgot to set the pins as INPUT and OUTPUT...
10hrs wasted, haha!
1
u/jedwardsol Jul 23 '24
You should post the source code of the program that doesn't work
1
u/FosItek Jul 23 '24
How?
Im only getting a red line in Reddit saying "Unable to create comment" after pasting my main.cpp file...
1
1
u/FosItek Jul 23 '24
MainProgram --> Configuration.h