Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

raspberry pi geany rasp hc-sr04 sonar arduino

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 ROny9

ROny9

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 25 December 2015 - 12:17 PM

Hi, please can you check my code? I dont know where I have a errors :(
 
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
 
 
#define TRIGsonar1 = 0;
#define TRIGsonar2 = 2;
#define TRIGsonar3 = 3;
#define TRIGsonar4 = 12;
#define TRIGsonar5 = 13;
#define TRIGsonar6 = 14;
#define TRIGsonar7 = 17;
#define TRIGsonar8 = 19;
 
#define ECHOsonar1 = 1;
#define ECHOsonar2 = 4;
#define ECHOsonar3 = 5;
#define ECHOsonar4 = 6;
#define ECHOsonar5 = 10;
#define ECHOsonar6 = 11;
#define ECHOsonar7 = 18;
#define ECHOsonar8 = 20;
 
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 zmeratVsetky()
int max = 0;
int smer = 0;
/*--------------------1--------------------------*/
digitalWrite(TRIGsonar1, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar1, LOW);
 
while (digitalRead(ECHOsonar1)==LOW);
long startTime1 = micros();
 
while (digitalRead(ECHOsonar1)==HIGH);
long travelTime1 = micros() - startTime1;
 
int distance1 = travelTime1/58;
 
if (distance1 > max) {
max = distance1;
smer = 1;
}
 
/*--------------------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;
 
if (distance2 > max) {
max = distance2;
smer = 2;
}
 
/*--------------------3--------------------------*/
 
digitalWrite(TRIGsonar3, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar3, LOW);
 
while (digitalRead(ECHOsonar3)==LOW);
long startTime3 = micros();
 
while (digitalRead(ECHOsonar3)==HIGH);
long travelTime3 = micros() - startTime3;
 
int distance3 = travelTime3/58;
 
if (distance3 > max) {
max = distance3;
smer = 3;
}
 
/*-------------------4---------------------------*/
 
digitalWrite(TRIGsonar4, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar4, LOW);
 
while (digitalRead(ECHOsonar4)==LOW);
long startTime4 = micros();
 
while (digitalRead(ECHOsonar4)==HIGH);
long travelTime4 = micros() - startTime4;
 
int distance4 = travelTime4/58;
 
if (distance4 > max) {
max = distance4;
smer = 4;
}
 
/*-------------------5---------------------------*/
 
digitalWrite(TRIGsonar5, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar5, LOW);
 
while (digitalRead(ECHOsonar5)==LOW);
long startTime5 = micros();
 
while (digitalRead(ECHOsonar5)==HIGH);
long travelTime5 = micros() - startTime5;
 
int distance5 = travelTime5/58;
 
if (distance5 > max) {
max = distance5;
smer = 5;
}
 
/*--------------------6--------------------------*/
 
digitalWrite(TRIGsonar6, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar6, LOW);
 
while (digitalRead(ECHOsonar6)==LOW);
long startTime6 = micros();
 
while (digitalRead(ECHOsonar6)==HIGH);
long travelTime6 = micros() - startTime6;
 
int distance6 = travelTime6/58;
 
if (distance6 > max) {
max = distance6;
smer = 6;
}
 
/*--------------------7--------------------------*/
 
digitalWrite(TRIGsonar7, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar7, LOW);
 
while (digitalRead(ECHOsonar7)==LOW);
long startTime7 = micros();
 
while (digitalRead(ECHOsonar7)==HIGH);
long travelTime7 = micros() - startTime7;
 
int distance7 = travelTime7/58;
 
if (distance7 > max) {
max = distance7;
smer = 7;
}
 
/*---------------------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;
 
if (distance8 > max) {
max = distance8;
smer = 8;
}
 
return smer;
}
 
 
int zmeratSonar1() {
fprintf("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();
}
else if(vzdialenost > 100) {
zmeratSonar1();
}
 
return smer;
}
 
int zmeratSonar28(){
fprintf("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;
}
 
int main(int argc, char **argv)
{
fprintf("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;
}
fprintf("meram vsetky");
serialPutchar(fd, zmeratVsetky());
fprintf("max je: ", max);
fprintf("smer je: ", smer);
fprintf("vzdialenost1 je: ", distance1);
fprintf("vzdialenost2 je: ", distance2);
fprintf("vzdialenost3 je: ", distance3);
fprintf("vzdialenost4 je: ", distance4);
fprintf("vzdialenost5 je: ", distance5);
fprintf("vzdialenost6 je: ", distance6);
fprintf("vzdialenost7 je: ", distance7);
fprintf("vzdialenost8 je: ", distance8);
delayMicroseconds(1000000);
for(;;){
serialPutchar(fd,zmeratSonar1());
if(zmeratSonar1() == 0) {
serialPutchar(fd, zmeratVsetky());
}
}
 
 
serialClose(fd) ;
return 0;
}
 
 
 
 


#2 Gikoskos

Gikoskos

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 26 December 2015 - 01:33 AM

What are the errors about? What command do you use to compile?