Theirchuck
Posts: 11
Joined: Sun Apr 18, 2021 1:17 pm

Code error in C

Sun Apr 18, 2021 1:24 pm

Hello guys, trying to run HC SR04 sensor in C language I used http://www.piprojects.xyz/ultrasonic-distance-sensor/ codes on this site but
ultrasonic.c: 50: 2: error: expected identifier or ‘('before‘ return'
return 0;
^ ~~~~~
ultrasonic.c: 51: 1: error: expected identifier or ‘('before‘)' token
}
^
I am getting error like this. Can you help me for solution?

and i'm using raspberry pi 3 B +

dsyleixa123
Posts: 1321
Joined: Mon Jun 11, 2018 11:22 am

Re: Code error in C

Mon Apr 19, 2021 11:37 am

perhaps the "}" in main() between delay(1000) and return 0 is redundant and has to be deleted...?

Code: Select all

        delay(1000);
	}   // <<<<<<<<<<<<<<<<
	return 0;

Heater
Posts: 18024
Joined: Tue Jul 17, 2012 3:02 pm

Re: Code error in C

Mon Apr 19, 2021 12:21 pm

More likely the "}" on the line between the printf and the delay should be removed. It makes little sense otherwise. Like so:

Code: Select all

int main(void) {
	setup();
	while (1)
	{
		int dist=getCM();
		printf("Distance: %dcm\n", dist);
		delay(1000);
	}
	return 0;
}
Memory in C++ is a leaky abstraction .

User avatar
DougieLawson
Posts: 41202
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Code error in C

Mon Apr 19, 2021 12:30 pm

This version works. Note: that I've changed from WiringPi pin numbers to BCM/Broadcom pin numbers (by using wiringPiSetupGpio).

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>

#define TRIG 22
#define ECHO 25

void setup() {
        wiringPiSetupGpio();
        pinMode(TRIG, OUTPUT);
        pinMode(ECHO, INPUT);

        //TRIG pin must start LOW
        digitalWrite(TRIG, LOW);
        delay(30);
}

int getCM() {
        //Send trig pulse
        digitalWrite(TRIG, HIGH);
        delayMicroseconds(20);
        digitalWrite(TRIG, LOW);

        //Wait for echo start
        while(digitalRead(ECHO) == LOW);

        //Wait for echo end
        long startTime = micros();
        while(digitalRead(ECHO) == HIGH);
        long travelTime = micros() - startTime;

        //Get distance in cm
        int distance = travelTime / 58;

        return distance;
}

int main(void) {
        setup();
        printf("Distance: %dcm\n", getCM());
        return 0;
}
I wired the HC-SR04 with Vcc on 5V, GND on GND, trig on pin#22 and echo (with a three 1K resistor voltage divider (cos I couldn't find a 1K & 2.2K resistor)) on pin#25.

https://pinout.xyz/pinout/pin15_gpio22
https://pinout.xyz/pinout/pin22_gpio25
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

dsyleixa123
Posts: 1321
Joined: Mon Jun 11, 2018 11:22 am

Re: Code error in C

Mon Apr 19, 2021 12:39 pm

I agree with Heater, it makes more sense to wait 1s after each reading than just once at the end of the program 8-)

Theirchuck
Posts: 11
Joined: Sun Apr 18, 2021 1:17 pm

Re: Code error in C

Wed Apr 21, 2021 10:14 pm

Hello, I tried the code hc sr04 Thanks. I have 2 questions. When I compiled it, I get these errors. Is there a solution to this?

/ usr / bin / ld: /tmp/ccMiXxtK.o: in the `setup 'function:

ultrasonic.c :(. text + 0x8): undefined reference to wiringPiSetupGpio

/ usr / bin / ld: ultrasonic.c :(. text + 0x14): `undefined reference to pinMode

/ usr / bin / ld: ultrasonic.c :(. text + 0x20): `undefined reference to pinMode

/ usr / bin / ld: ultrasonic.c :(. text + 0x2c): undefined reference for `` digitalWrite ''

/ usr / bin / ld: ultrasonic.c :(. text + 0x34): undefined reference to `` delay ''

/ usr / bin / ld: /tmp/ccMiXxtK.o: In function `getCM ':
ultrasonic.c :(. text + 0x54): undefined reference for `` digitalWrite ''

/ usr / bin / ld: ultrasonic.c :(. text + 0x5c): undefined reference for 'delayMicroseconds'

/ usr / bin / ld: ultrasonic.c :(. text + 0x68): undefined reference for `` digitalWrite ''

/ usr / bin / ld: ultrasonic.c :(. text + 0x74): undefined reference for `` digitalRead ''

/ usr / bin / ld: ultrasonic.c :(. text + 0x84): undefined reference to "micros"

/ usr / bin / ld: ultrasonic.c :(. text + 0x98): undefined reference for `` digitalRead ''

/ usr / bin / ld: ultrasonic.c :(. text + 0xa8): undefined reference for `` micros ''
Collect2: error: ld 1 returned exit status

and I need a schematic to build the circuit

User avatar
DougieLawson
Posts: 41202
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Code error in C

Thu Apr 22, 2021 12:52 am

Compile it with
gcc -o ultra -lwiringPi ultrasonic.c
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Theirchuck
Posts: 11
Joined: Sun Apr 18, 2021 1:17 pm

Re: Code error in C

Thu Apr 22, 2021 6:09 pm

hello thanks for your help, i am using the code compile command worked but when i compiled geany from brick i get the errors i wrote above i guess i need to fix them how can i fix them thanks.

Theirchuck
Posts: 11
Joined: Sun Apr 18, 2021 1:17 pm

Re: Code error in C

Thu Apr 22, 2021 6:21 pm

Hello. The codes worked. Thank you. But I have a question. What do I have to do to measure the sensor continuously? I am writing sudo ./ultrasonic for every measurement and it measures. What do I have to do to measure it continuously?

User avatar
B.Goode
Posts: 11867
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Code error in C

Thu Apr 22, 2021 6:28 pm

Theirchuck wrote:
Thu Apr 22, 2021 6:21 pm
Hello. The codes worked. Thank you. But I have a question. What do I have to do to measure the sensor continuously? I am writing sudo ./ultrasonic for every measurement and it measures. What do I have to do to measure it continuously?
I am no C expert, but it seems to me that you need to reinstate the while () { .. } loop that you removed?

Theirchuck
Posts: 11
Joined: Sun Apr 18, 2021 1:17 pm

Re: Code error in C

Thu Apr 22, 2021 6:38 pm

Hello, I deleted the first codes I threw. I am using the codes that Dougie Lawson threw now and I cannot measure constantly. I am not good in C language but I have homework I want help for this and by the way thanks for your opinion

User avatar
B.Goode
Posts: 11867
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Code error in C

Thu Apr 22, 2021 6:50 pm

Theirchuck wrote:
Thu Apr 22, 2021 6:38 pm
Hello, I deleted the first codes I threw. I am using the codes that Dougie Lawson threw now and I cannot measure constantly. I am not good in C language but I have homework I want help for this and by the way thanks for your opinion

Just to highlight that -
" I am not good in C language but I have homework "

Most teachers are not monsters. Homework is assigned to help you learn and improve. Or to check how much you might have learnt or improved.

Getting volunteer helpers here to do your homework is not beneficial to you.

User avatar
DougieLawson
Posts: 41202
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Code error in C

Thu Apr 22, 2021 6:50 pm

Theirchuck wrote:
Thu Apr 22, 2021 6:38 pm
Hello, I deleted the first codes I threw. I am using the codes that Dougie Lawson threw now and I cannot measure constantly. I am not good in C language but I have homework I want help for this and by the way thanks for your opinion
I don't like doing your homework for you. My rate is USD$750/day.

Stick

Code: Select all

while (1) {
after

Code: Select all

setup();
and

Code: Select all

delay (10);
}
before

Code: Select all

return 0;
You may need to tweak the time delay, so you get a reading when expected and don't burn the CPU @ 100% busy.
http://wiringpi.com/reference/timing/
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Theirchuck
Posts: 11
Joined: Sun Apr 18, 2021 1:17 pm

Re: Code error in C

Thu Apr 22, 2021 7:02 pm

Thanks for your help I apologize if I kept you busy

swampdog
Posts: 630
Joined: Fri Dec 04, 2015 11:22 am

Re: Code error in C

Thu Apr 22, 2021 7:54 pm

Here's the three "infinite" loops you can read up on..

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int
main()
{
 do {
	printf("%s\n","dw");
 }while(0);

 while(0)	{
	printf("%s\n","wl");
 }

 for (;;)	{
	printf("%s\n","fl");
	if ('q' == fgetc(stdin))
		break
	;
 }

 return EXIT_SUCCESS;
}

Heater
Posts: 18024
Joined: Tue Jul 17, 2012 3:02 pm

Re: Code error in C

Fri Apr 23, 2021 2:27 am

swampdog wrote:
Thu Apr 22, 2021 7:54 pm
Here's the three "infinite" loops you can read up on..

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int
main()
{
 do {
	printf("%s\n","dw");
 }while(0);

 while(0)	{
	printf("%s\n","wl");
 }

 for (;;)	{
	printf("%s\n","fl");
	if ('q' == fgetc(stdin))
		break
	;
 }

 return EXIT_SUCCESS;
}
That is sneaky of you. "infinite" in quotes because only the last example executes the printf repeatedly forever.
Memory in C++ is a leaky abstraction .

swampdog
Posts: 630
Joined: Fri Dec 04, 2015 11:22 am

Re: Code error in C

Sat Apr 24, 2021 2:53 am

Heater wrote:
Fri Apr 23, 2021 2:27 am
swampdog wrote:
Thu Apr 22, 2021 7:54 pm
Here's the three "infinite" loops you can read up on..

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int
main()
{
 do {
	printf("%s\n","dw");
 }while(0);

 while(0)	{
	printf("%s\n","wl");
 }

 for (;;)	{
	printf("%s\n","fl");
	if ('q' == fgetc(stdin))
		break
	;
 }

 return EXIT_SUCCESS;
}
That is sneaky of you. "infinite" in quotes because only the last example executes the printf repeatedly forever.
I'd like to take full credit but if I'm honest I was thinking about how to demonstrate three infinite loops concisely without slapping a break in everywhere. I am pleased with the result though! :-)

Heater
Posts: 18024
Joined: Tue Jul 17, 2012 3:02 pm

Re: Code error in C

Sat Apr 24, 2021 8:46 am

swampdog wrote:
Sat Apr 24, 2021 2:53 am
I'd like to take full credit but if I'm honest I was thinking about how to demonstrate three infinite loops concisely without slapping a break in everywhere.
I don't understand. Adding "break" to the two non-infinite loops you presented would not make them run forever.
swampdog wrote:
Sat Apr 24, 2021 2:53 am
I am pleased with the result though! :-)
You might also like to write "if" as:

Code: Select all

    for ( ; a == b; ) {
        printf("True\n");
        break;
    }
Memory in C++ is a leaky abstraction .

swampdog
Posts: 630
Joined: Fri Dec 04, 2015 11:22 am

Re: Code error in C

Sat Apr 24, 2021 6:52 pm

Heater wrote:
Sat Apr 24, 2021 8:46 am
swampdog wrote:
Sat Apr 24, 2021 2:53 am
I'd like to take full credit but if I'm honest I was thinking about how to demonstrate three infinite loops concisely without slapping a break in everywhere.
I don't understand. Adding "break" to the two non-infinite loops you presented would not make them run forever.
I know. It wouldn't have helped the OP, probably caused more confusion.
Heater wrote:
Sat Apr 24, 2021 8:46 am
swampdog wrote:
Sat Apr 24, 2021 2:53 am
I am pleased with the result though! :-)
You might also like to write "if" as:

Code: Select all

    for ( ; a == b; ) {
        printf("True\n");
        break;
    }
I like that as well. :-)

Heater
Posts: 18024
Joined: Tue Jul 17, 2012 3:02 pm

Re: Code error in C

Sat Apr 24, 2021 8:27 pm

swampdog wrote:
Sat Apr 24, 2021 6:52 pm
I like that as well. :-)
Sounds like you are ready for The International Obfuscated C Code Contest: https://www.ioccc.org/
Memory in C++ is a leaky abstraction .

swampdog
Posts: 630
Joined: Fri Dec 04, 2015 11:22 am

Re: Code error in C

Sat Apr 24, 2021 10:44 pm

Heater wrote:
Sat Apr 24, 2021 8:27 pm
swampdog wrote:
Sat Apr 24, 2021 6:52 pm
I like that as well. :-)
Sounds like you are ready for The International Obfuscated C Code Contest: https://www.ioccc.org/
That's what I like about our obfuscated code: it isn't. It's easy to read and logical.

Mind you, I was able to write..
"abc"[1]='D'
..in K&R and have that become "aDc". No memory protection!

Return to “C/C++”