Arduino with sensor

hi frinds, i am ne bee in this world. i have some idea about programing in arduino. but i have encounter some problem in programing it. i have use sonic sensor it has 3 port. name of sensor GH-311RT. but i have found in discution it should not be use to distance measurement. but it should sense the value high withine 25cm and low beyound 25cm when i use control pin to analog input 'A0'. so i have plane to drive two motor with this sensor logic as explane above. so i have write program for it. but problem is that, it does not function according to sensor. the motors are move forward back left right without sensor value.

i have programe when any object is within 25cm both motor stop, get reverse take right/left turn. i have past my program if any mistak by me plz correct it.

data sheet is here any one find http://www.sure-electronics.net/download/DC-SS014_Ver1.0_EN.pdf

                                   
  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int enablePin = 9;    // H-bridge enable pin
  const int analogInPin = A0;// define analog pin                         
     int sensorValue; // Value for sensor output
  void setup() {
   
                                  // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
   
    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);

    pinMode(analogInPin,INPUT);
   
  }

  void loop() {
    sensorValue = analogRead(analogInPin);
    if (sensorValue==LOW)
    {
      driveforward();
    }
    else
    {
      stopmoto();
      delay(1000);
      reverse();
      delay(3000);
      left();
      delay(2000);
    }
  }
  void driveforward()
   {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      digitalWrite(motor3Pin, LOW);
      digitalWrite(motor4Pin, HIGH);
     
  }
    
    void stopmoto()
    {
      digitalWrite(motor1Pin, LOW);
      digitalWrite(motor2Pin, LOW);
      digitalWrite(motor3Pin, LOW);
      digitalWrite(motor4Pin, LOW);
    }
    void reverse()
    {
      digitalWrite(motor1Pin, HIGH);
      digitalWrite(motor2Pin, LOW);
      digitalWrite(motor3Pin, HIGH);
      digitalWrite(motor4Pin, LOW);
    }
 
  i think my" if " statment is confusing may be. but your suggetion is more importent.

thanx for feedback

this sensor is digital

The data sheet was not great but from what I could get, this is a digital output. The output does not go up or down as things get closer and further. Instead, you get “something there” or “something not there”. That’s it. High/ low or on/ off or yes/ no. That is all you will get from this sensor. 

sorry – how to fix it.

Try the same code but use a digital input instead of a ADC. I don’t know if the arduino does high/low with the ADC’s.

try new port

 i have try this sensor output port directly connect to digital pin-10 but it does not work. it move forward, backward, right without sensor value. here i used 5v supply from arduino for sensor. is more voltag is required for it or 5v is enough.

more suggestion

hi friends plz provide some idea what i do in it to make it working properly…? i am waiting for all ur suggetions.

your linked manual states

your linked manual states 6-12VDC as a power voltage.  I suggest you measure the output voltage with a multimeter before trying to use it in a program :slight_smile:

I think CtC has it correct.

I think CtC has it correct. You need to change your code to use the sensor’s digital output. Your current code is using an analogRead. You need to use a digitalRead.

Try that and let us know if it works.