Radar with Servo Motor and Hc-Sr04

Hello everyone i have got a problem. İ want make a Radar. İ must use millis in my code.Would you add millis in my code

#include<Servo.h>;
#include<LiquidCrystal.h>;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo sg90;
const int ledr = 6;
const int ledg = 13;
const int buzzer = 10;
const int trig = 7;
const int echo = 8;
int pos = 0;
int mesafe = 0;//in english it means distance
int sure = 0;//in english it means time
int led = 0;
void setup() {
  // put your setup code here, to run once:
  sg90.attach(9);
  pinMode(ledr, OUTPUT);
  pinMode(ledg, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trig ,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig ,LOW);
  sure = pulseIn(echo ,HIGH);
  mesafe = sure/2 /28,8;
  if(mesafe < 20)
  digitalWrite(ledr, HIGH);
  if(mesafe > 20)
  digitalWrite(ledg, HIGH);
  if(mesafe < 20)
  digitalWrite(ledg, LOW);
  if(mesafe > 20)
  digitalWrite(ledr, LOW);
  if(mesafe < 20)
  digitalWrite(buzzer, HIGH);
  if(mesafe > 20)
  digitalWrite(buzzer, LOW);
  if(mesafe < 20){
  lcd.home();  
  lcd.print("Cisim tespiti");//it is turkish
  delay(1000);
  lcd.clear();
  lcd.print("Uzaklık");//it is turkish
  lcd.print(mesafe);//it is turkish
  lcd.print("cm");
  delay(3000);
  lcd.clear();
  };
 {

  for (pos = 0; pos <= 180; pos += 1) {
  sg90.write(pos);
  delay(15);
  }
 for (pos = 180; pos >= 0; pos -= 1) {
  sg90.write(pos);
  delay(15);
 }

No, once you decide what you want to do with "millis", you can add it and decide if it does what you want it to do and if not, return with the results and how it is not what you want it to do.

Paul

I assume you want to replace the very long delay in loop()?

If you open the IDE, and have a look at the blink without delay tutorial, you should be able to see how this code could be incorporated into yours.

It isn't worth applying this technique to the trigger pulse of the SONAR unit.

Paul_KD7HB: No, once you decide what you want to do with "millis", you can add it and decide if it does what you want it to do and if not, return with the results and how it is not what you want it to do.

Paul

thank you for answer my questions. But i am not be able to use millis i mustn't use delay.

tetmc: thank you for answer my questions. But i am not be able to use millis i mustn't use delay.

You did not answer the question. What do you want to do with Millis?

Paul

tetmc:
But i am not be able to use millis i mustn’t use delay.

Then you need to learn how.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

…R

[/quote][quote author=Paul_KD7HB link=msg=3572579 date=1516467715]
No, once you decide what you want to do with "millis", you can add it and decide if it does what you want it to do and if not, return with the results and how it is not what you want it to do.

Paul
[/quote]

İ solved this problem without millis

#include<Servo.h>;
#include<LiquidCrystal.h>;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo sg90;
const int ledr = 6;
const int ledg = 13;
const int buzzer = 10;
const int trig = 7;
const int echo = 8;
int pos = 0;
int mesafe = 0;//in english it means distance
int sure = 0;//in english it means time
int led = 0;
int mesafe2 = 0;

unsigned long previousMillis = 0;
const long interval = 15;
void setup() {
// put your setup code here, to run once:
sg90.attach(9);
pinMode(ledr, OUTPUT);
pinMode(ledg, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:

for (pos = 0; pos <= 180; pos += 1) {
sg90.write(pos);
delay(15);
digitalWrite(trig ,HIGH);
delayMicroseconds(1000);
digitalWrite(trig ,LOW);
sure = pulseIn(echo ,HIGH);
mesafe = sure/2 /29,1;
if(mesafe < 20 && mesafe > 0)
digitalWrite(ledr, HIGH);
if(mesafe > 20 && mesafe > 0)
digitalWrite(ledg, HIGH);
if(mesafe < 20 && mesafe > 0)
digitalWrite(ledg, LOW);
if(mesafe > 20 && mesafe > 0)
digitalWrite(ledr, LOW);
if(mesafe < 20 && mesafe > 0)
digitalWrite(buzzer, HIGH);
if(mesafe > 20 && mesafe > 0)
digitalWrite(buzzer, LOW);
if(mesafe < 20 && mesafe > 0){
lcd.home();
lcd.print(“Cisim tespiti”);
delay(1000);
lcd.clear();
lcd.print(“Uzaklık”);
lcd.print(mesafe);
lcd.print(“cm”);
delay(3000);
lcd.clear();
Serial.print(“mesafe”);
Serial.print(mesafe);
};
}

for (pos = 180; pos >= 0; pos -= 1) {
sg90.write(pos);
delay(15);
digitalWrite(trig ,HIGH);
delayMicroseconds(1000);
digitalWrite(trig ,LOW);
sure = pulseIn(echo ,HIGH);
mesafe = sure/2 /29,1;
if(mesafe < 20 && mesafe > 0)
digitalWrite(ledr, HIGH);
if(mesafe > 20 && mesafe > 0)
digitalWrite(ledg, HIGH);
if(mesafe < 20 && mesafe > 0)
digitalWrite(ledg, LOW);
if(mesafe > 20 && mesafe > 0)
digitalWrite(ledr, LOW);
if(mesafe < 20 && mesafe > 0)
digitalWrite(buzzer, HIGH);
if(mesafe > 20 && mesafe > 0)
digitalWrite(buzzer, LOW);
if(mesafe < 20 && mesafe > 0){
lcd.home();
lcd.print(“Cisim tespiti”);
delay(1000);
lcd.clear();
lcd.print(“Uzaklık”);
lcd.print(mesafe);
lcd.print(“cm”);
delay(3000);
lcd.clear();
};
}

}

[/code]

Hi,

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Tom... :)

TomGeorge: Hi,

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Tom... :)

sorry i make a mistake but i know how can i write code

İ solved this problem without millis

I thought the problem was to eliminate delay().