Please use code tags when posting code. You can edit your post and
type
** **[code]** **
before your code
type
** **[/code]** **
after your code
OK, your setup() determines a low level. In loop you compare against that low level using '=='. Chances that the sensorValue is exactly that low level are not that big. You might want to use '<=' instead
void loop() {
sensorValue = analogRead(A0);
if (sensorValue <= sensorLow) {
...
...
}
else
{
...
...
}
You might also want to add a bit of play (hysteresis).
You can add some debugging to your code (use serial.println) and use serial monitor to display values that you have measured.
int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;
const int motorPin = 9;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
while (millis() < 5000) {
sensorValue = analogRead(A0);
if (sensorValue > sensorHigh) {
sensorHigh = sensorValue;
}
if (sensorValue < sensorLow) {
sensorLow = sensorValue;
}
}
Serial.print("sensorLow = "); Serial.println(sensorLow);
Serial.print("sensorHigh = "); Serial.println(sensorHigh);
digitalWrite(ledPin, LOW);
}
void loop() {
sensorValue = analogRead(A0);
Serial.print("sensorValue = "); Serial.println(sensorValue);
if (sensorValue <= sensorLow + 5) { <---------------------- added a bit of hysteresis
digitalWrite(motorPin, HIGH);
}
else {
digitalWrite(motorPin, LOW);
}
}
No idea about phototransistors so can't advise there; this is just the coding part.