# IR sharp sensor with DC motor

Hi! So first of ill try to explain the project I am making. I am building a quadcopter with retractable legs. I am using a sharp IR proximity sensor and just a standard dc motor. As motor controller i am using an H bridge and the arduino Uno. The idea is when the sensor picks up something the legs of the quad go down again, and retracts when nothing is detected. I believe this sensor goes to 80cm so with nothing I mean everything after 80cm. The plan afterwards is to also use the gear switch on my remote to manually rectract the legs but thats for later, first I want to figure this out. Baby steps! After doing research below is the code I came up with. However it doesn’t work, could someone please point me in the right direction or what I did wrong? Thanks in advance!

``````//Motor one
int enA = 6;
int inA = 2;
int inB = 3;
// Sensor
int sensorPin = 0;

int sensorVal;

void setup() {

pinMode(enA, OUTPUT);
pinMode(inA, OUTPUT);
pinMode(inB, OUTPUT);
}

void loop() {

digitalWrite(inA, LOW);
digitalWrite(inB, HIGH);
if(sensorVal<500)

{
digitalWrite(inB, HIGH);

}

else {
digitalWrite(inA, LOW);

}

}
[code]
``````

You don't seem to be changing anything in the if/ else. You start with inA low and inB high. Then you either set inB high (no change) or inA low (still no change).

I suspect you want inA low and inB high as one option and inA high inB low as the other?

So move these....

``````digitalWrite(inA, LOW);
digitalWrite(inB, HIGH);
``````

....into the if and the else, with one pair of low/high reversed.

Thank you for that! I also figured out I forgot to tell enA to do anything. So it wasnt sending any signal to the motorcontroller. My new code is

``````//Motor one
int enA = 6;
int inA = 2;
int inB = 3;
// Sensor
int sensorPin = 0;

int sensorVal;

void setup() {

pinMode(enA, OUTPUT);
pinMode(inA, OUTPUT);
pinMode(inB, OUTPUT);
}

void loop() {

if(sensorVal>40)

{
digitalWrite(enA, HIGH);
digitalWrite(inB, HIGH);
digitalWrite(inA, LOW);

}

else {
digitalWrite(inA, HIGH);
digitalWrite(inB, LOW);
}

}
``````

Now i need tojust make it stop after x amount of seconds and ill be golden! Thanks again