Let's Make Robots! | RobotShop

Many errors in my C code, communication with raspberry pi and arduino

I wrote this code and I don't know what to do to change that. This code I want to use in my Raspberry pi with sonars - hc sr 04 to measure a distance. Please do you know how to fix my code? :) before this I wrote a code for-example. This is my real code. So please check it again :) thanks!

int zmeratSonar1() {
int smer = 0;
printf("meram sonar1");
digitalWrite(TRIGsonar1, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar1, LOW);

while (digitalRead(ECHOsonar1)==LOW);
long zaciatok = micros();

while (digitalRead(ECHOsonar1)==HIGH);
long cas = micros() - zaciatok;

int vzdialenost = cas/58;

if(vzdialenost < 100) {
    smer = zmeratSonar28();  // <----here is my problem
}
else if(vzdialenost > 100) {
    zmeratSonar1();
}

return smer;
}

int zmeratSonar28(){
int smer = 0;
printf("meram sonare 2 a 8");
//------------SONAR 2---------------------
digitalWrite(TRIGsonar2, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar2, LOW);

while (digitalRead(ECHOsonar2)==LOW);
long startTime2 = micros();

while (digitalRead(ECHOsonar2)==HIGH);
long travelTime2 = micros() - startTime2;

int distance2 = travelTime2/58;

//------------SONAR 8----------------------
digitalWrite(TRIGsonar8, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar8, LOW);

while (digitalRead(ECHOsonar8)==LOW);
long startTime8 = micros();

while (digitalRead(ECHOsonar8)==HIGH);
long travelTime8 = micros() - startTime8;

int distance8 = travelTime8/58;

//porovnanie vzdialenosti
if(distance2 > 100 || distance8 > 100) {
    if(distance2 > distance8) {
        smer = 2;
    }

    else {
        smer = 8;
    }
}

else{
    smer = 0;
}

return smer;
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I am not familiar with RPi wiring but it looks to me like your error count would be very high. Start with a 5 line main program to display a simple message on the console. Then  make a routine to process a single sonar device. You could make a static array of I/O pins and just pass the index or you could pass the TRIG and ECHO numbers. Then I might consider talking to the Arduino.

You are using zmeratSonar28() prior to defining it. Add a forward declaration at the top of the file that looks like:

/*
 * Forward Declarations
 */

int  zmeratSonar28(void); 

The following is a possible rewrie using a single sonar function. I have NOT compiled this!!

 

/* * Forward Declarations */
int zmeratSonar28(void);

int zmeratSonar(int trigPin, int echoPin)
{
    int
	distance;
    long
	startTime, travelTime;

    digitalWrite(trigPin, HIGH);
    delayMicroseconds(20);
    digitalWrite(trigPin, LOW);

    while (digitalRead(echoPin) == LOW);
    startTime = micros();

    while (digitalRead(echoPin) == HIGH);
    travelTime = micros() - startTime;

    distance = travelTime / 58;
    return(distance);
}

int zmeratSonar1(void)
{
    int
	smer, vzdialenost;
    
    printf("meram sonar1\n");
    
    smer = 0;

    vzdialenost = zmeratSonar(TRIGsonar1, ECHOsonar1);

    if (vzdialenost < 100)
    {
	smer = zmeratSonar28();  // <----here is my problem
    }
    else if (vzdialenost > 100)
    {
	zmeratSonar1();
    }

    return(smer);
}

int zmeratSonar28(void)
{
    int
	distance2, distance8 smer;
    
    printf("meram sonare 2 a 8\n");
    
    smer = 0;

    //------------SONAR 2---------------------

    distance2 = zmeratSonar(TRIGsonar2, ECHOsonar2);

    //------------SONAR 8----------------------

    distance8 = zmeratSonar(TRIGsonar8, ECHOsonar8);

    // porovnanie vzdialenosti
    if (distance2 > 100 || distance8 > 100)
    {
	if (distance2 > distance8)
	{
	    smer = 2;
	}
	else
	{
	    smer = 8;
	}
    }
    else
    {
	smer = 0;
    }

    return(smer);
}

Why not just define zmeratSonar28(void) first?

And why are you using smer as a global variable when a local variable would make it more understandable? Just my opinion.

Thanks a lot, here is my real, whole code. please check it briefly :)

 

 

 

#include <string.h>
#include <errno.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>

#define TRIGsonar1 0
#define TRIGsonar2 2
#define TRIGsonar3 3
#define TRIGsonar4 12
#define TRIGsonar5 13
#define TRIGsonar6 14
#define TRIGsonar7 21
#define TRIGsonar8 22

#define ECHOsonar1 1
#define ECHOsonar2 4
#define ECHOsonar3 5
#define ECHOsonar4 6
#define ECHOsonar5 10
#define ECHOsonar6 27
#define ECHOsonar7 28
#define ECHOsonar8 29

void setup() {
	wiringPiSetup();
	pinMode(TRIGsonar1, OUTPUT);
	pinMode(TRIGsonar2, OUTPUT);
	pinMode(TRIGsonar3, OUTPUT);
	pinMode(TRIGsonar4, OUTPUT);
	pinMode(TRIGsonar5, OUTPUT);
	pinMode(TRIGsonar6, OUTPUT);
	pinMode(TRIGsonar7, OUTPUT);
	pinMode(TRIGsonar8, OUTPUT);

	pinMode(ECHOsonar1, INPUT);
	pinMode(ECHOsonar2, INPUT);
	pinMode(ECHOsonar3, INPUT);
	pinMode(ECHOsonar4, INPUT);
	pinMode(ECHOsonar5, INPUT);
	pinMode(ECHOsonar6, INPUT);
	pinMode(ECHOsonar7, INPUT);
	pinMode(ECHOsonar8, INPUT);
	
	digitalWrite(TRIGsonar1, LOW);
	digitalWrite(TRIGsonar2, LOW);
	digitalWrite(TRIGsonar3, LOW);
	digitalWrite(TRIGsonar4, LOW);
	digitalWrite(TRIGsonar5, LOW);
	digitalWrite(TRIGsonar6, LOW);
	digitalWrite(TRIGsonar7, LOW);
	digitalWrite(TRIGsonar8, LOW);
}

int zmeratSonar28(void);

int zmeratSonar(int TRIGpin, int ECHOpin) {
	int distance;
	long startTime, travelTime;
	
	digitalWrite(TRIGpin, HIGH);
	delayMicroseconds(20);
	digitalWrite(TRIGpin, LOW);
	
	while (digitalRead(ECHOpin) == LOW);
	startTime = micros();
	
	while (digitalRead(ECHOpin) == HIGH);
	travelTime = micros() - startTime;
	
	distance = travelTime / 58;
	return(distance);	
}

int zmeratVsetky() {
	int smer = 0;
	int max = 0;
	int distance1, distance2, distance3, distance4, distance5, distance6, distance7, distance8;
	/*--------------------1--------------------------*/
	distance1 = zmeratSonar(TRIGsonar1, ECHOsonar1);
	if (distance1 > max) {
		max = distance1;
		smer = 1;
	}
	
	/*--------------------2--------------------------*/
	distance2 = zmeratSonar(TRIGsonar2, ECHOsonar2);
	if (distance2 > max) {
		max = distance2;
		smer = 2;
	}
	
	/*--------------------3--------------------------*/
	distance3 = zmeratSonar(TRIGsonar3, ECHOsonar3);
	if (distance3 > max) {
		max = distance3;
		smer = 3;
	}
	
	/*-------------------4---------------------------*/
	distance4 = zmeratSonar(TRIGsonar4, ECHOsonar4);
	if (distance4 > max) {
		max = distance4;
		smer = 4;
	}
	
	/*-------------------5---------------------------*/
	distance5 = zmeratSonar(TRIGsonar5, ECHOsonar5);
	if (distance5 > max) {
		max = distance5;
		smer = 5;
	}
	
	/*--------------------6--------------------------*/
	distance6 = zmeratSonar(TRIGsonar6, ECHOsonar6);
	if (distance6 > max) {
		max = distance6;
		smer = 6;
	}
	
	/*--------------------7--------------------------*/
	distance7 = zmeratSonar(TRIGsonar7, ECHOsonar7);
	if (distance7 > max) {
		max = distance7;
		smer = 7;
	}
	
	/*---------------------8-------------------------*/
	distance8 = zmeratSonar(TRIGsonar8, ECHOsonar8);
	if (distance8 > max) {
		max = distance8;
		smer = 8;
	}	
	
	printf("max je: %dcm\n", max);
	printf("smer je: %dcm\n", smer);
	printf("vzdialenost1 je: %dcm\n", distance1);
	printf("vzdialenost2 je: %dcm\n", distance2);
	printf("vzdialenost3 je: %dcm\n", distance3);
	printf("vzdialenost4 je: %dcm\n", distance4);
	printf("vzdialenost5 je: %dcm\n", distance5);
	printf("vzdialenost6 je: %dcm\n", distance6);
	printf("vzdialenost7 je: %dcm\n", distance7);
	printf("vzdialenost8 je: %dcm\n", distance8);
	return smer;
}

int zmeratSonar1(void) {
	int smer, vzdialenost;
	printf("meram sonar1");
	smer = 0;
	vzdialenost = zmeratSonar(TRIGsonar1, ECHOsonar1);
	
	if(vzdialenost < 100) {
		smer = zmeratSonar28();
	}
	else if(vzdialenost > 100) {
		zmeratSonar1();
	}
	
	return smer;
}

int zmeratSonar28(){
	int smer, vzdialenost2, vzdialenost8;
	printf("meram sonare 2 a 8");
	//------------SONAR 2---------------------
	vzdialenost2 = zmeratSonar(TRIGsonar2, ECHOsonar2);
	
	//------------SONAR 8----------------------
	vzdialenost8 = zmeratSonar(TRIGsonar8, ECHOsonar8);
	
	//porovnanie vzdialenosti
	if(vzdialenost2 > 100 || vzdialenost8 > 100) {
		if(vzdialenost2 > vzdialenost8) {
			smer = 2;
		}
	
		else {
			smer = 8;
		}
	}
	
	else{
		smer = 0;
	}
	
	return smer;
}

int main(void)
{
	printf("zapol som program!");
	setup();
	int fd;
	if ((fd = serialOpen ("/dev/ttyACM0", 9600)) < 0) {
		fprintf (stderr, "Neda sa otvorit: %s\n", strerror (errno)) ;
		return -1;
    }

    if (wiringPiSetup () == -1) {
		fprintf (stdout, "Neda sa zapnut WiringPi: %s\n", strerror (errno)) ;
		return -1;
	}
	printf("meram vsetky");
	serialPutchar(fd, zmeratVsetky());
	//delayMicroseconds(1000000);
	for(;;){
		serialPutchar(fd,zmeratSonar1());
		if(zmeratSonar1() == 0) {
			serialPutchar(fd, zmeratVsetky());
		}
	}
	
	
	serialClose(fd);
	return 0;
}
	



I do not understand the following. serialPutchar does one byte, sonar range is a 32bit int.

serialPutchar(fd, zmeratVsetky());

//delayMicroseconds(1000000);
for(;;){
	serialPutchar(fd,zmeratSonar1());
	if(zmeratSonar1() == 0) {
		serialPutchar(fd, zmeratVsetky());
	}
}

pfuu, I dont really know :/....what did you mean? Because as you can see, I want with this code and 8 hc-sr04 look on my surroundings and find the best way to go with robot - car. In that code function - zmeratVsetky() - measure every sonar and function zmeratSonar1() - will measure only one - first sonar, to look everytime if some obstacle is there to avoid. Every function return integer value - smer. I know that serialPutchar(fd, char) send one byte. I send everytime number from 0 - 8. Is that problem???? :) @ggallant

It is my fault. I assumed you were writing distance, not the sonar index.

I am confused why you are finding the max distance instead of the min. Most sonar modules I have used just give up after a certain distance.

No, I want a distance from sonar, not an index. I want max distance because, sonars will be in circle and when my robot turn on, sonars has to measure max distance where robot can turn and then goes forward. Do you understand what I mean ? :) Because I dont have so good english...I'm from slovakia

The max range of an SH04 is 4m. I am assuming that the 8 sonar modules are mounted on the robot arranged in a radial pattern. What will the robot do if all echos return 4 meters? Are you looking for an opening in a confined space? 

As for English, I went to Moscow and survived. Perhaps drawing would help.