[Solved]Controlling DC Motors with Photocell and Ultrasonic Sensors

![//Pin Declarations
 
const int motorOne_InputTwo = 4;
const int motorOne_InputOne = 5;
const int motorOne_Enable = 9;
const int motorTwo_Enable = 10;
const int motorTwo_InputOne = 6;
const int motorTwo_InputTwo = 7;
int sensorValue;
int trig = 12;
int echo = 13;
long duration, inches, cm;
  
void setup()
{
 pinMode(A0, INPUT); 
 Serial.begin(9600);
 //Assigns pinModes to the above pins
 for(int i = 4; i <= 10; i++)
  {
    if (i == 8)
    continue; //skips loop iteration if i is 8 as there is nothing connected to the pin number 8.
    pinMode(i, OUTPUT);
  }
 //Set enable pins to HIGH so that the motors would turn on
 digitalWrite(motorOne_Enable, HIGH);
 digitalWrite(motorTwo_Enable, HIGH);
}

void right()
{
 Serial.println("Turn Right.");
 digitalWrite(motorOne_InputOne, LOW);
 digitalWrite(motorOne_InputTwo, HIGH);
 digitalWrite(motorTwo_InputOne, HIGH);
 digitalWrite(motorTwo_InputTwo, LOW);
}

void foward()
{
 Serial.println("Forward.");
 digitalWrite(motorOne_InputOne, HIGH);
 digitalWrite(motorOne_InputTwo, LOW);
 digitalWrite(motorTwo_InputOne, HIGH);
 digitalWrite(motorTwo_InputTwo, LOW);
}

void stopMotor()
{
 Serial.println("Too Bright");
 digitalWrite(motorOne_InputOne, LOW);
 digitalWrite(motorOne_InputTwo, LOW);
 digitalWrite(motorTwo_InputOne, LOW);
 digitalWrite(motorTwo_InputTwo, LOW);
}

void ultraSensor()
{
// The output is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);
 
// The echo pin is used to read the signal: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
 
pinMode(echo,INPUT);
duration = pulseIn(echo, HIGH);
 
// convert the time into a distance
 
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
 
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
 
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PI...
return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}


void loop()
{
 ultraSensor();
 sensorValue = analogRead(A0);
 Serial.println(sensorValue);
 if ((sensorValue<500) && (15<inches<50))
 {
  foward();
 }
 else
 {
 if (sensorValue<500)
  {
   right();
  }
 else 
  {
   stopMotor();
  }   
 }
 delay (1000);
}

Hello. I'm trying to make a design so that two motors turn on when it is dark and when there isn't anything in front of it, the mechanism would rotate right. When the ultrasonic sensor detects something, then it would stop turning right and move foward. However, with the code and circuitry, the mechanism is only able to turn right. What am I missing / did wrong?

This:

(15<inches<50))

... isn't how to check if a value is between two limits, in spite of that being how we did it at school. You have to check both separately:

if ((sensorValue<500) && (inches>15) && (inches<50))

I tried that, but the motors still don't run like they're supposed to. The photocell works fine but I cant figure out the ultrasonic sensors

What do your serial prints tell you you're reading for inches?

I solved it, I mixed up the functions for forward and right for the motors