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;
}

Errors

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.

** smer = zmeratSonar28(); // <----here is my problem**

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); 

Possible rewrite

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
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.

very good advices!!

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

 

 

 

#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 &gt; max) {
	max = distance2;
	smer = 2;
}

/*--------------------3--------------------------*/
distance3 = zmeratSonar(TRIGsonar3, ECHOsonar3);
if (distance3 &gt; max) {
	max = distance3;
	smer = 3;
}

/*-------------------4---------------------------*/
distance4 = zmeratSonar(TRIGsonar4, ECHOsonar4);
if (distance4 &gt; max) {
	max = distance4;
	smer = 4;
}

/*-------------------5---------------------------*/
distance5 = zmeratSonar(TRIGsonar5, ECHOsonar5);
if (distance5 &gt; max) {
	max = distance5;
	smer = 5;
}

/*--------------------6--------------------------*/
distance6 = zmeratSonar(TRIGsonar6, ECHOsonar6);
if (distance6 &gt; max) {
	max = distance6;
	smer = 6;
}

/*--------------------7--------------------------*/
distance7 = zmeratSonar(TRIGsonar7, ECHOsonar7);
if (distance7 &gt; max) {
	max = distance7;
	smer = 7;
}

/*---------------------8-------------------------*/
distance8 = zmeratSonar(TRIGsonar8, ECHOsonar8);
if (distance8 &gt; 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 &lt; 100) {
	smer = zmeratSonar28();
}
else if(vzdialenost &gt; 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 &gt; 100 || vzdialenost8 &gt; 100) {
	if(vzdialenost2 &gt; 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;

}




code questions

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());
}
}

answer

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??? :slight_smile: @ggallant

My mistake

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.

hmm

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 ? :slight_smile: Because I dont have so good english…I’m from slovakia

Sonar max range

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.

yes

yes, I want to find the best way for the robot. For instance, robot will be turn on in a room, raspberry pi measure 8 sonar and choose the max direction - in the code you can see that, there is a IF, which  compare max from previos measure with distance from  current sonar. I dont have enough experience, but do you think that if there will be more than 1 measurements with more then 4 metres robot wont know which direction is right??..

 

So, how you suggest me to do that? :slight_smile:

Sonar

I think the HS04 sonar is useful for obstacle advoidance at less than 1m. The LIDAR modules are long range sensors. They also cost about $120US vs $2US for the HS04. What are you trying to accomplish? 

project to school

I think these sonars will be good for my project to school, I have 2 weeks to finish it, so I dont have time to buying new things. So, please, I only need to check if my code is good :slight_smile: :slight_smile:

I see no use for the

I see no use for the functions zmeratSonar1() and zmeratSonar28() as zmeratVsetky() gives you the index of the max distance. BTW, once you calcaulate and display the max distance is is lost. You might want to consider  saving is a global or returning the value via a function parameter.

Using micros()  in a task on multi-tasking environment like RPi is likely to give errors as the task may be suspended. You need to move the code inside an ISR or disable interrupts while waiting for the echo. 

code

Thanks, so if that functions isnt good, how they could look? 

Yes I know that if max is one calculate in zmeratVsetky() is lost because I want to use it only one time - first time, when robot is turn on, that time robot measure distance from every sonar and choose the best - longest way, then max is lost and robot is going to that chosen direction and always measure first sonar - zmeratSonar1(), if distance from that sonar is lower then 100 (some object is ahead of robot) zmeratSonar1() calls function zmeratSonar28() - these sonars are the nearest to sonar 1 - sonar2 (on the top left), sonar8 (on the top right). Function zmeratSonar28() return smer = direction to free way. If not, function send 0 = STOP.

 

Do you understand me? How I want to do that? :slight_smile:

zmeratSonar1()

If the distance > 100 you recursively call zmeratSonar1(). I think you might want to rewrite to use a while {} construct. Recursive calls can crash if they use all available stack.

 

code zmeratSonar1()

yes, but if I have code on another side of serial communication (arduino) which start with first sent number from RPI to arduino and sending logical 1 to motors of robot (car) if robot doesnt recieve any other number. Please check fast this code, especially code in the loop, code in switch - case is just for motors - what they have to do, when arduino recieve some of numbers :slight_smile: thanks

 

 

void Motors()

{

  digitalWrite(lmbrkpin,lmbrake>0);                     // if left brake>0 then engage electronic braking for left motor

  digitalWrite(lmdirpin,lmspeed>0);                     // if left speed>0 then left motor direction is forward else reverse

  analogWrite (lmpwmpin,abs(lmspeed));                  // set left PWM to absolute value of left speed - if brake is engaged then PWM controls braking

  if(lmbrake>0 && lmspeed==0) lmenc=0;                  // if left brake is enabled and left speed=0 then reset left encoder counter

 

  digitalWrite(rmbrkpin,rmbrake>0);                     // if right brake>0 then engage electronic braking for right motor

  digitalWrite(rmdirpin,rmspeed>0);                     // if right speed>0 then right motor direction is forward else reverse

  analogWrite (rmpwmpin,abs(rmspeed));                  // set right PWM to absolute value of right speed - if brake is engaged then PWM controls braking

  if(rmbrake>0 && rmspeed==0) rmenc=0;                  // if right brake is enabled and right speed=0 then reset right encoder counter

}

 

void setup() {

  Serial.begin(9600);

  pinMode(lmpwmpin,OUTPUT);                            // configure left  motor PWM        pin for output

  pinMode(lmdirpin,OUTPUT);                            // configure left  motor direction  pin for output

  pinMode(lmbrkpin,OUTPUT);                            // configure left  motor brake      pin for output

 

  pinMode(rmpwmpin,OUTPUT);                            // configure right motor PWM        pin for output

  pinMode(rmdirpin,OUTPUT);                            // configure right motor direction  pin for output

  pinMode(rmbrkpin,OUTPUT);                            // configure right motor brake      pin for output

}

 

void loop() { 

  if(Serial.available()){

    char smer = Serial.read();

      switch (smer) {

        case 0:

         lmbrake = 1;

         rmbrake = 1;

         Serial.println(“stop”);

         Motors();

         break; 

        case 1:

         lmspeed = 255;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println(“rovno”);

         Motors();

         break; 

        case 2:

         lmspeed = 50;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println(“otocit -> 2, lavohore”);

         Motors();

         delay(400);

         break; 

        case 3:

         lmspeed = -255;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println(“otocit -> 3, vlavo”);

         Motors();

         delay(280);

         break; 

        case 4:

         lmspeed = -255;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println(“otocit -> 4, lavodolu”);

         Motors();

         delay(430);

         break; 

        case 5:

         lmspeed = -255;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println(“otocit -> 5, dolu”);

         Motors();

         delay(550);

         break; 

        case 6:

         lmspeed = 255;

         rmspeed = -255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println(“otocit -> 6, pravodolu”);

         Motors();

         delay(430);

         break;

        case 7:

         lmspeed = 255;

         rmspeed = -255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println(“otocit -> 7, vpravo”);

         Motors();

         delay(280);

         break; 

        case 8:

         lmspeed = 255;

         rmspeed = 50;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println(“otocit -> 8, pravohore”);

         Motors();

         delay(400);

         break;

        default:

         lmspeed = 255;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Motors();

      }

     delay(1);

   }

}

**below this **

Hi, please can you check my code on arduino? this code is below this message :slight_smile: thx

while + if?

Hi, so you mean I have to use instead of IF - while, so it will look like this?

 

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

   vzdialenost = zmeratSonar(TRIGsonar1, ECHOsonar1);

   while(vzdialenost < 100)
   {
smer = zmeratSonar28();  
   }
   if (vzdialenost > 100)
   {
zmeratSonar1();
   }

   return(smer);
}