HC SR04 QUESTION

#include <NewPing.h>
#include <Servo.h>

long dizi[180];
int aci;
long sure;
long uzaklik; 
Servo myservo;
void setup() {

pinMode(trigPin, OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(9600);
myservo.attach(9);
}

void loop() {
if(digitalRead(2) == HIGH){
  
  for(aci = 0 ; aci <= 180 ; aci++){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5); 
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);   
  sure = pulseIn(echoPin, HIGH); 
  uzaklik= sure /29.1/2;
  
  dizi[aci] = uzaklik - 10;
  myservo.write(aci);
  delay(15);
  }
}
else{
  
  for(aci = 0 ; aci <= 180 ; aci++){
      
     digitalWrite(trigPin, LOW);
     delayMicroseconds(5); 
     digitalWrite(trigPin, HIGH); 
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);   
     sure = pulseIn(echoPin, HIGH); 
     uzaklik= sure /29.1/2;
      
      myservo.write(aci);
      delay(15);
      if(dizi[aci]>uzaklik){
      digitalWrite(13,HIGH);
      }
  }
  
  for(aci = 180 ; aci <= 180 ; aci--){
      
     digitalWrite(trigPin, LOW);
     delayMicroseconds(5); 
     digitalWrite(trigPin, HIGH); 
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);   
     sure = pulseIn(echoPin, HIGH); 
     uzaklik= sure /29.1/2;
      
      myservo.write(aci);
      delay(15);
      if(dizi[aci]>uzaklik){
      digitalWrite(13,HIGH);
      }
  }  
}
}

It gives this error message. How can I solve it?

Arduino:1.8.13 (Windows 10), Kart:“Arduino Uno”

C:\Users\Hp\Documents\Arduino\hcsr04\hcsr04.ino: In function ‘void setup()’:

hcsr04:11:9: error: ‘trigPin’ was not declared in this scope

pinMode(trigPin, OUTPUT);

^~~~~~~

hcsr04:12:9: error: ‘echoPin’ was not declared in this scope

pinMode(echoPin,INPUT);

^~~~~~~

C:\Users\Hp\Documents\Arduino\hcsr04\hcsr04.ino:12:9: note: suggested alternative: ‘ServoPin_t’

pinMode(echoPin,INPUT);

^~~~~~~

ServoPin_t

C:\Users\Hp\Documents\Arduino\hcsr04\hcsr04.ino: In function ‘void loop()’:

hcsr04:21:16: error: ‘trigPin’ was not declared in this scope

digitalWrite(trigPin, LOW);

^~~~~~~

hcsr04:26:18: error: ‘echoPin’ was not declared in this scope

sure = pulseIn(echoPin, HIGH);

^~~~~~~

C:\Users\Hp\Documents\Arduino\hcsr04\hcsr04.ino:26:18: note: suggested alternative: ‘ServoPin_t’

sure = pulseIn(echoPin, HIGH);

^~~~~~~

ServoPin_t

hcsr04:38:19: error: ‘trigPin’ was not declared in this scope

digitalWrite(trigPin, LOW);

^~~~~~~

hcsr04:43:21: error: ‘echoPin’ was not declared in this scope

sure = pulseIn(echoPin, HIGH);

^~~~~~~

C:\Users\Hp\Documents\Arduino\hcsr04\hcsr04.ino:43:21: note: suggested alternative: ‘ServoPin_t’

sure = pulseIn(echoPin, HIGH);

^~~~~~~

ServoPin_t

hcsr04:55:19: error: ‘trigPin’ was not declared in this scope

digitalWrite(trigPin, LOW);

^~~~~~~

hcsr04:60:21: error: ‘echoPin’ was not declared in this scope

sure = pulseIn(echoPin, HIGH);

^~~~~~~

C:\Users\Hp\Documents\Arduino\hcsr04\hcsr04.ino:60:21: note: suggested alternative: ‘ServoPin_t’

sure = pulseIn(echoPin, HIGH);

^~~~~~~

ServoPin_t

exit status 1

‘trigPin’ was not declared in this scope

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Add this before void setup():

const int trigPin;
const int echoPin;

THANKS SO MUCH

…but make sure you give the constants values other than the default zero.

delay(15); But don’t expect consistent results with a ping rate of 66Hz.
Lower it to something closer to 25Hz

doruk6734:
THANKS SO MUCH

No problem :wink:

TheUNOGuy:
No problem :wink:

Oh! The irony.