1

I would like to know how i can use my ultrasonic value in an if else statement, so i can use the value that the servo motors stop running and go in another direction when the ultrasonic sensor gets close to an object.

/*
  Digital pin 2 is connected to a switch
  Digital pin 3 is connected to a switch
  Digital pin 7 is connected to the echo port of the ultrasonic sensor
  Digital pin 8 is connected to the trig port of the ultrasonic sensor
  Digital pin 9 is connected to a servo
  Digital pin 10 is connected to a servo
  Digital pin 13 is connected to a LED

 */

#include <Servo.h> // include a special file that is needed for servo motors, do this only once

Servo motor1; // create a servo object for each motor
Servo motor2;

int echo = 7; //declare echo from ultrasonic sensor
unsigned long duration;
void setup() {
  // initialize the LED pin as an output:
  pinMode(13, OUTPUT);
    // initialize the pushbutton pin as an input:
    pinMode(2, INPUT);
    // switch 2 initialize
    pinMode(3, INPUT);
  // initialize the servo on pin 9
  motor1.attach(9);
  // motor 2 initialize
  motor2.attach(10);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
    //echo pin of ultrasonic sensor
    pinMode(echo, INPUT);
    //trig pin of ultrasonic sensor
    pinMode(8, OUTPUT);

}

void loop() {
      // the trigger pin sends a pulse
       digitalWrite(8, LOW); 
       delayMicroseconds(2); 
       digitalWrite(8, HIGH);
       delayMicroseconds(10);  
       digitalWrite(8, LOW);
       duration=pulseIn(echo, HIGH);
      // the echo pin measures how long it took for that pulse to come back
       Serial.println(duration); 
      // delay in between reads for stability
       delay(50); 
       duration=pulseIn(echo, HIGH);


  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH: 
  if(duration <=400) {
    // turn LED on:
    digitalWrite(13, HIGH);
    // turn the motors off for 2 seconds
    motor1.write(90);
    motor2.write(90);
    delay(2000);
    //turn motor 1 on for 2 seconds to make a turn
    motor1.write(180);
    delay(2000);
  } else {
    digitalWrite(13, LOW);
    //motors run when vehicle is not near objects
    motor1.write(0);
    motor2.write(180);
  }
}
  • So what is the problem so far? Does the code not work? Please be more specific. – Nick Solonko Sep 14 '16 at 15:55
  • The sensor shows me all the values in the console but i dont know how i can use those values in an if else statement so that my servo motors would stop and turn around when the sensor gets close to an object. The code works when i use a switch in the if else, but now i want to use the ultrasonic sensor to be the switch in stopping the servo motors. – S. Mateijsen Sep 14 '16 at 16:00
1

First, I would make sure I have continuous servo motors that can rotate 360 degrees. Your servos will just rotate to the degree you entered if they are not continuous. Additionally, it would be more helpful if you named your pins. For example:

int echo = 7;
void setup(){
    pinMode(echo, INPUT);
}

This will help us read the code better.

I also see that you have "pulseIn" in your if statement. You must save the value to a variable with the data type of unsigned long.

//before the setup 
unsigned long duration;

//keep your setup as it is


void loop(){
   digitalWrite(8, LOW); 
   delayMicroseconds(2); 
   digitalWrite(8, HIGH);
   delayMicroseconds(10);  
   digitalWrite(8, LOW); 

   duration=pulseIn(echo, HIGH);

   if(duration<=400){//check for the value
       // turn LED on:
        digitalWrite(13, HIGH);
        // turn the motors off for 2 seconds
        motor1.write(90);
        motor2.write(90);
        delay(2000);
        //turn motor 1 on for 2 seconds to make a turn
        motor1.write(180);
        delay(2000);
   }else{
        digitalWrite(13, LOW);
        //motors run when vehicle is not near objects
         motor1.write(0);
         motor2.write(180);
       }
   }

Overall: check your servos and save the value of pulseIn to a variable. You might also want to convert to centimeters by dividing duration by 58.2.

  • the led goes on now when i put my hand in front of the sensor and off when i get my hand away, but the motors do not continue running. When before they would just go on forever – S. Mateijsen Sep 14 '16 at 16:13
  • @S.Mateijsen What power source are you using? So the LED goes off, motor 1 stops and motor 2 is already stopped? And they just stay at rest? – Nick Solonko Sep 14 '16 at 16:17
  • im using 4 AA batteries and my laptop as power, the led stays on untill i remove my hand(thats also whats supposed to happen so the led works) the motors just rotate a bit around and then stop at there angles – S. Mateijsen Sep 14 '16 at 16:22
  • turned out my batteries where almost empty :< But now the motors both go on at the start. after a couple seconds the led goes on and he makes a turn. but he keeps making turns now and doesnt go right ahead anymore so he keeps looping the if duration <=100 statement and doesnt come to the else part. – S. Mateijsen Sep 14 '16 at 16:28
  • First check your batteries. Maybe they are dead. It happened to me yesterday. A servo was working and then in the middle it always shut off. Can you confirm that they are continuous servos? Post a picture of them or a link where you got them. The phrase "at their angles" confuses me. Do the servos stop at 0 and 180 degrees? If yes, then they are not continuous. – Nick Solonko Sep 14 '16 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.