Radar Project ( Arduino mega + TFT screen + servo + hc-sr04)

As you see from the topic , i am trying to build a radar with these equipments.
(this just a test code , i am working on it)

my range will be between 5cm-30cm . otherwise it will accepted as zero.

i got several problems.
-my measured distances will keep in an array that is " mesafe[pos] "

  • normally if an distance out of my limits (5cm-30cm) it must be zero. But i have value of 4 at my arrays.

  • coordinates of my radar circle (x,y) = (200,120), radius = 15

  • i mapped my calculated coordinates calues to the radar circle
    (1-measure the first distance when an object has seen
    use the value of angle for first distance
    2- measure the second distance when an object has seen
    use the value of angle for first distance
    3- calculate the maps location from (1) and (2) then map to the radar screen. )

  • my results places only at horizontal exis (x axis) , (that is the main problem)

  • consider the first hal circle (degree: zero to 180)

// 15.09.2014
// gokhan mavi ekran
// UTFT_Demo_320x240 (C)
// 2014 Gokhan OZTURK
// web: http://ozturkgokhan.com/
// This was made for modules with a screen resolution 
// of 320x240 pixels.

#include <UTFT.h>
#include <stdio.h>
#include <Servo.h> //---------------------------------- servo setup
float x_coordinate=0,y_coordinate=0;
float x_coordinate2=0,y_coordinate2=0;
float objectx=0, objecty=0, objectc=0;
int xmin=0, xmax=0, ymin=0, ymax=0;
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
int pos = 0, pos_i1, pos_i2, pos_f1, pos_f2;    // variable to store the servo position 
int ultrasonic2TrigPin[2] = { A8, A10 };       // 
int utlrasonic2EchoPin[2] = { A9, A11 }; 
int m=0,f;
float mesafe[181] ={ 0 };
float mesafe2[181] ={ 0 };
extern uint8_t SmallFont[];

UTFT myGLCD(SSD1289,38,39,40,41);
int x=0;


void setup()
{
  pinMode(ultrasonic2TrigPin[0], OUTPUT);
  pinMode(utlrasonic2EchoPin[0], INPUT);
  pinMode(ultrasonic2TrigPin[1], OUTPUT);
  pinMode(utlrasonic2EchoPin[1], INPUT);
  myservo.attach(A7);
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);
  myGLCD.clrScr();
  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRect(0, 0, 319, 13);
  myGLCD.setColor(0, 0, 0);
  myGLCD.setBackColor(255,255,255);
  myGLCD.print("Gokhan OZTURK", 12, 18); // yaz?,xdeki ba?lang?ç, ydeki ba?lang?ç
  delay(500);
  myGLCD.print("21177112", 12, 31);
  delay(500);
  myGLCD.print("Hacettepe", 12, 44);
  delay(500);
  myGLCD.print("University", 12, 56);
  delay(500); 
  myGLCD.print("Electronics", 12, 69);
  delay(500); 
  myGLCD.print("Engineering", 12, 81);
  delay(500); 
  myGLCD.print("Basic Radar Project", 150, 18);
  myGLCD.setColor(160, 160, 160);
  myGLCD.drawRoundRect(3, 16, 120, 236); // sol kare
  myGLCD.setColor(160, 160, 160);
  myGLCD.drawRoundRect(124, 16, 318, 236); // sa? kare
  myGLCD.setColor(160, 160, 160);
  myGLCD.drawCircle(220,120,80); // radar daire
  myGLCD.drawLine(220,40,220,200); // dik çizgi
  myGLCD.drawLine(140,120,300,120); // yatay çizgi
  
  //myGLCD.setColor(255, 255, 0); // detected object
  //myGLCD.fillCircle(200,120,15); // radar daire
  
}

void loop()
{
  
   for(pos = 0; pos <= 180; pos += 1){ // goes from 0 degrees to 180 degrees                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
 
  if(mesafe_olc(m) >= 5 && mesafe_olc(m) <= 30){
     mesafe[pos] = mesafe_olc(m); // save distance to mesafe[pos] if distance is betweeen 5 and 30 
  }
  
  else{
    mesafe[pos] = 0; // save 0 to mesafe[pos] if distance is not betweeen 5 and 30 
  }
  }
  
  for(pos = 0; pos <= 180; pos += 1){
    if (mesafe[pos] != 0){
      pos_i1 = pos; // pos_i1 is equal to pos if mesafe[pos] is not equal to 0
      break;
    }
  }
  
  for(pos = pos_i1; pos <= 180; pos += 1){
    if (mesafe[pos] == 0){
      pos_f1 = pos; // pos_f1 is equal to pos if mesafe[pos] is equal to 0
      break;
    }     
  }
    
  radar (mesafe[pos_i1],pos_i1,&x_coordinate,&y_coordinate);
  radar (mesafe[pos_f1],pos_f1,&x_coordinate2,&y_coordinate2);
  radarm (x_coordinate,x_coordinate2,y_coordinate,y_coordinate2,&objectx,&objecty,&objectc);
  
  myGLCD.setColor(255, 255, 0); // detected object
  myGLCD.fillCircle(objectx,objecty,objectc); 

//  
//  else {
//  myGLCD.setColor(255, 255, 255); // ekrana siyah nokta basar, bo? yani
//  myGLCD.fillCircle(210,220,0); 
//  }


   for(pos = 180 ; pos >= 0 ; pos -= 1) // goes from 180 degrees to 0 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);     // waits 15ms for the servo to reach the position 
 
if(mesafe_olc(m) >= 5 && mesafe_olc(m) <= 30){
     mesafe2[pos] = mesafe_olc(m); // save distance to mesafe[pos] if distance is betweeen 5 and 30 
  }
  
  else{
    mesafe2[pos] = 0; // save 0 to mesafe[pos] if distance is not betweeen 5 and 30 
  }
  }
  
  for(pos = 180 ; pos >= 0 ; pos -= 1){
    if (mesafe2[pos] != 0){
      pos_i2 = pos; // pos_i2 is equal to pos if mesafe2[pos] is not equal to 0
      break;
    }
  }
  
  for(pos = pos_i2; pos >= 0 ; pos -= 1){
    if (mesafe2[pos] == 0){
      pos_f2 = pos; // pos_f2 is equal to pos if mesafe2[pos] is equal to 0
      break;
    }     
  }
    
  radar (mesafe2[pos_i2],pos_i2,&x_coordinate,&y_coordinate);
  radar (mesafe2[pos_f2],pos_f2,&x_coordinate2,&y_coordinate2);
  radarm (x_coordinate,x_coordinate2,y_coordinate,y_coordinate2,&objectx,&objecty,&objectc);
  
  myGLCD.setColor(255, 255, 0); // detected object
  myGLCD.fillCircle(objectx,objecty,objectc); 
  
}
//  else {
//  myGLCD.setColor(255, 255, 255); // ekrana siyah nokta basar, bo? yani
//  myGLCD.fillCircle(210,220,0); 
//  }


//radar alt progam?   
  void radar (float a, float b, float *c, float *d){ 
 *c=a*cos((b*PI)/180);
 *d=a*sin((b*PI)/180);
 *c = map(*c, -80, 80, 140,300);      
 *d = map(*d, -80, 80, 40, 200); 
}

//  void radarf (float a, float b, float *c2, float *d2){ 
// *c2=a*cos((b*PI)/180);
// *d2=a*sin((b*PI)/180);
// *c2 = map(*c2, -80, 80, 140 ,300);      
// *d2 = map(*d2, -80, 80, 40 , 200); 
//}
void radarm (float x_coordinate, float x_coordinate2, float y_coordinate, float y_coordinate2, float *objectx, float *objecty, float *objectc ){
  
  float xmin= 0 , xmax = 0, ymin = 0, ymax = 0;
  
  xmin = min(x_coordinate, x_coordinate2);
  xmax = max(x_coordinate, x_coordinate2);
  ymin = min(y_coordinate, y_coordinate2);
  ymax = max(y_coordinate, y_coordinate2);
  
  *objectx = (((xmax-xmin)/2)+xmin);
  *objecty = ((ymax-ymin)/2)+ymin;
  *objectc = min(((ymax-ymin)/2),((xmax-xmin)/2)) ;
}

  int mesafe_olc ( int m){ //m is the trig no 
    int ultrasonic2Duration = 0, ultrasonic2Distance = 0; 
    
    digitalWrite(ultrasonic2TrigPin[m], HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin[m], LOW); 
   
    ultrasonic2Duration = pulseIn(utlrasonic2EchoPin[m], HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
    return ultrasonic2Distance;
}
float mesafe[181] ={ 0 };
float mesafe2[181] ={ 0 };

On which Arduino? That's using 61% of the memory on a 328-based Arduino.

     mesafe2[pos] = mesafe_olc(m);

The function returns an int. Why are you storing that in a float array?

  • normally if an distance out of my limits (5cm-30cm) it must be zero. But i have value of 4 at my arrays.

Probably because you are using more memory than you have.

PaulS:

float mesafe[181] ={ 0 };

float mesafe2[181] ={ 0 };



On which Arduino? That's using 61% of the memory on a 328-based Arduino.



mesafe2[pos] = mesafe_olc(m);



The function returns an int. Why are you storing that in a float array?



> - normally if an distance out of my limits (5cm-30cm) it must be zero. But i have value of 4 at my arrays.


Probably because you are using more memory than you have.

1 - I am using mega 2560 R3
2- i changed "mesafe2[pos] = mesafe_olc" , now it returns a float
3- after i download my program to the arduino , it says: 15% used
"Binary sketch size: 39.310 bytes (of a 258.048 byte maximum) - 15% used
"

2- i changed "mesafe2[pos] = mesafe_olc" , now it returns a float

Why?

  if(mesafe_olc(m) >= 5 && mesafe_olc(m) <= 30){
     mesafe[pos] = mesafe_olc(m); // save distance to mesafe[pos] if distance is betweeen 5 and 30

Why do you need to call the function three times?

i got several problems.
-my measured distances will keep in an array that is " mesafe[pos] "

Why is this a problem?

  • normally if an distance out of my limits (5cm-30cm) it must be zero. But i have value of 4 at my arrays.

I don't see how you know this.

Once we know that you are reading the environment correctly, then we can concentrate on the other issues.

But, radar should be defined to take two reference arguments (the last to, that you write to) rather than confusing things using address of and pointer notation.