Programming a proximity sensor to run the program once it senses an obstacle

Hi guys so I'm really new with Arduino and Arduino programming, and I really need help with the programming aspect.

So for more context, my project is that Whenever the proximity sensor senses an object, the if statements on my light sensor will execute, which will then program servo motors. I have been able to program the Light sensor already, but I have no idea how to program the proximity sensor.

Do i make an If statement before the other if statements? Is there a way to create a new program then call out to another program file to execute?

I'm really lost and I would appreciate any insight someone can give me. Thank you all :slight_smile:

btw this is the code I have so far:

Servo myservo; 
Servo spinny;

int light;
int pos = 0;   
int lol = 0;

void setup() {
  myservo.attach(9); 
 spinny.attach(10);
  Serial.begin(9600);
  myservo.write(0);
  spinny.write(0);
}

void loop() {
light = analogRead(A0);
Serial.println(light);
if (light<11){
  for (pos = 0; pos <= 80; pos += 1) {    
    myservo.write(pos);              
    delay(15);                       
  }
  delay(1000);     
  for (pos = 80; pos >= 0; pos -= 1) { 
    myservo.write(pos);              
    delay(15);                       
  }
  
    myservo.detach();
    
   for (lol = 0; lol <= 80; lol += 1) { 
    // in steps of 1 degree
    spinny.write(lol);              
    delay(15);                       
  }
  delay(1000);     
  for (lol = 80; lol >= 0; lol -= 1) { 
    spinny.write(lol);              
    delay(15);                       
  }
  spinny.detach();
}
else if (light>13) {
  
     for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos);              
    delay(15);                       
  }
  delay(1000);     
  for (pos = 180; pos >= 0; pos -= 1) { 
    myservo.write(pos);              
    delay(15);                       
  }

  myservo.detach();
    
   for (lol = 0; lol <= 180; lol += 1) { 
    
    spinny.write(lol);              
    delay(15);                       
  }
  delay(1000);     
  for (lol = 180; lol >= 0; lol -= 1) { 
    spinny.write(lol);              
    delay(15);                       
  }
  spinny.detach();
}
    
else 
myservo.detach();
spinny.detach();
}

You read your proximity sensor about the same way as you read your light sensor. Then, you compare both sensors and if either one of them is triggered [use an if() statement with '||' or] you do your routine.

Note: you do not typically detach servo motors inside your loop. You attach them once inside setup() and than either call them to move or not.

Note 2: If you auto format your code, (Ctrl-T in IDE) you will see different indentation. Your lase 'else' clause does not contain brackets '{}' so only the first statement is connected to it. The other statement always gets executed.

Note 3: Bonus points for you as your first post and you used code tags!

Is your program intended to move the servos once only? I ask because you detach the servos in loop() but never attach them again. That will stop them doing anything next time round the loop().

And for information, an Arduino can only contain a single program. So you can arrange which parts of the program will be working at any time by using if statements, separating things out into functions and the like but you can't call a different program.

But for what you want it does seem like you just need a read of your "proximity sensor" at the top of loop with an if statement which runs all your existing code (or not depending on the result of the read). Give it a try and post your attempt if you need any help getting it to work.

Steve