Sharp 20120x proximity sensor

Hello all!
I’m having a little bit of trouble with my current project that I’m working on. I’ve written up the code for a simple bot that lights up in the dark and uses a proximity sensor to decide whether it goes forwards or turns. Unfortunately, I’m having difficulty getting my proximity sensor to even display readouts, never mind function as I had anticipated.
I’m a little new to Arduino, and programming in general, and I can’t seem to figure out if the problem is in my code or in my hardware. I was hoping I could get some help, maybe a second pair of eyes from people who are more experienced, in order to find out what exactly I’m doing wrong.

Thank you very much!

My code so far:

int photoPin = A2; //selecting the input pin for the photoresistor
int photoValue = 0; //storing the value coming from the sensor

int whiteled = 4; //setting the white led to pin 4

int sensorPin = A1; //selecting the input pin for the proximity sensor
int sensorValue = 0; //storing the values coming from the sensor

int motor1Pin1 = 5; // pin 2 on L293D
int motor1Pin2 = 6; // pin 7 on L293D
int enablePin = 3; // pin 1 on L293D
int motor2Pin2 = 10; // pin 10 on L293D
int motor2Pin1 = 11; // pin 15 on L293D

void setup() {

pinMode(whiteled, OUTPUT); //the pin for the white LED is an output pin
pinMode(photoPin, INPUT); //the sensor pin is an input
pinMode(sensorPin, INPUT); //the sensor pin is an input

pinMode(motor1Pin1, OUTPUT); //set motor pin as output
pinMode(motor1Pin2, OUTPUT); //set motor pin as output
pinMode(motor2Pin1, OUTPUT); //set motor pin as output
pinMode(motor2Pin2, OUTPUT); //set motor pin as output
pinMode(enablePin, OUTPUT); //set enable pin as output
digitalWrite(enablePin, HIGH);

}

void loop() {

sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);

if (sensorValue < 600){

digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high
digitalWrite(motor2Pin1, HIGH); // set pin 15 on L293D high
digitalWrite(motor2Pin2, LOW); // set pin 10 on L293D low
}
else {

digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high
digitalWrite(motor2Pin1, LOW); // set pin 15 on L293D low
digitalWrite(motor2Pin2, HIGH); // set pin 10 on L293D high

}

photoValue = analogRead(photoPin);
if (photoValue < 5)
{
float r = random(1000); //let the variable r be anything between 0-1000
digitalWrite(whiteled, HIGH); //turn the light on
delay(r); // for random amount of time
digitalWrite(whiteled, LOW); //turn the light off
delay (r); //for a random amount of time
}

}

please use code tags when posting code, they are for free by pressing the # button above the smileys :wink:

You should first try a sketch that just reads the proximity sensor and outputs it value to serial. Then you get a better impression of the values it generates.

Can you tell what proximity sensor you use? link to datasheet?
Also post a schematic how you connected it, we cannot see it :wink: