Echopin' was not declared in this scope

i am trying to make functions and variabel separate from the main loop . i created it all the main info what is needed just still i get an error about not declared in this scope.

int geefafstand(int trigpin, int echopin,long duration,int distance){
  //welke parameter pinnummer  
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delay(2);
   digitalWrite(pingPin, HIGH);
   delay(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);  
   distance = duration * 0.034 / 2;
}
void setup() {
  Serial.begin(9600);
  pinMode(greenLightPin, OUTPUT);
  pinMode(redLightPin, OUTPUT);
  pinMode(PinButton1, INPUT_PULLUP);
  pinMode(PinButton2, INPUT_PULLUP);
  pinMode(pingPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop(){ 
  int buttonState1 = digitalRead(PinButton1);
  int buttonState2 = digitalRead(PinButton2);
  long duration    = digitalRead (echopin);
  
  
  if (buttonState1 == LOW ){
    knipperled(greenLightPin,10,1000);
    geefafstand( trigpin,echopin,duration, distance);
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
     
    
  }

you are promising the compiler that this function will return an int but it does not return anything...
distance is a local variable to the function, different than the one you use for calling ➜ you want to pass this one by reference (same for duration if you want it back)

and for your error, what's the business of trigpin versus pingPin?

int geefafstand(int trigpin, int echopin,long duration,int distance)
pinMode(echoPin, INPUT);

Spot the different spellings ?

and that one too :slight_smile:

i changed pingping to trigping i changed the code but stil it says distance' was not declared in this scope

void loop(){ 
  int buttonState1 = digitalRead(PinButton1);
  int buttonState2 = digitalRead(PinButton2);
   digitalRead(distance);
  
  
  if (buttonState1 == LOW ){
    knipperled(greenLightPin,10,1000);
    geefafstand(distance);
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
     
    
  }

int geefafstand(int trigpin, int echopin,long duration,int distance){
//welke parameter pinnummer
digitalWrite(trigpin, LOW);
digitalWrite(trigpin, HIGH);

duration = pulseIn(echopin, HIGH);
distance = duration * 0.034 / 2;

return distance;
}

well is distance a PIN number ? are you sure you want to call digitalRead?

also you need a variable for this.

read about scope ➜ scope - Arduino Reference


please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)