"IF" statement for servo control using phototransistor input

Hello,

I am trying to write a code in order to have a phototransistor control a servo. I have tried to cut and paste from a few different code examples to make one that does this. The code is attached for reference.

-I have successfully used the “sweep” example from the arduino program’s built-in examples to control a servo
-I have successful used an example code from online to get the phototransistor to work and display corresponding voltage levels in the serial monitor
-Now I am trying to get these two codes to work together using an “if” statement so that the light levels control the turning of the servo

I can compile and upload the code without any errors, however, it is not yet doing what I want it to. The servo runs, but the phototransistor had no effect on the result. When I open the serial monitor, I can see that the phototransistor is indeed working and sending out different voltage values related to light levels. I am not sure what I am doing wrong, but since I know the hardware works, I feel that I must somehow not be using the “if” statement correctly, despite having looked at examples. Any insight would be appreciated.

input_phototransistor_controlling_servo.ino (2.72 KB)

Why are you writing to servoPin ?

The attached code:

/*
  input_phototransistor_controlling_servo
  measeures light levels
  Kelly Zona, 2015

// board: select the appropriate Arduino or Fabduino option
// programmer: Arduino as ISP
// serial port: choose the appropriate one depending on which USB port your device is connected to
// use the appropriate driver board and be sure to ground it to the Arduino!
// open Arduino's Serial Montor to read the voltage output from the phototransistor board connected to A3

 */
 
 #include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 


// These constants won't change:
const int analogPin = A3;    // pin that the sensor (phototransistor) is attached to
const int servoPin = 9;       // pin that the servo is attached to
const int threshold = 3;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the servo pin as an output:
  pinMode(servoPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  
}



void loop() {
  
  Serial.print("A3 = ");                     // Display values for input A3 
  Serial.print(volts(A3));                   // Display measured voltage
  Serial.println(" volts");                  // Display voltage and newline
  delay(1000);                               // Delay for one second
  
  
  
  // read the value of the phototransistor:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the servo:
  if (analogValue > threshold) {
    digitalWrite(servoPin, HIGH);
  } 
  else {
    digitalWrite(servoPin,LOW); 
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability
  
  
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  
  }
  
  
  }
                                             
float volts(int adPin)                       // Measures volts at adPin
{                                            // Returns voltage
 return float(analogRead(adPin)) * 5.0 / 1024.0;
   

  
  
}
  if (analogValue > threshold) {
    digitalWrite(servoPin, HIGH);
  } 
  else {
    digitalWrite(servoPin,LOW); 
  }

This is the only part that bothers with the reading from the analog pin, and it is writing digital to the servo pin, that isn’t going to help the servo move in any normal way.

You also have threshold set to 3, so barely anything on the phototransistor trips this if.

This is the only part that moves the servo:

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  
  }

And nowhere in it is the value from that analogRead used at all. It just sweeps the servo to and fro.

Can you tell from this code that the servo is moving in response to the argument passed to the write method? Perhaps you should use the value you read from the analog pin to calculate a position to write the servo to.

You aren’t really clear on how you want the phototransistor to affect the movement of the servo, but perhaps something like this is more what you need?

void loop() {

  Serial.print("A3 = ");                     // Display values for input A3 
  Serial.print(volts(A3));                   // Display measured voltage
  Serial.println(" volts");                  // Display voltage and newline
  delay(1000);                               // Delay for one second



  // read the value of the phototransistor:
  int analogValue = analogRead(analogPin);

  int servoPos = map(analogValue, 0, 1023, 0, 90);  // maps the analog reading to a value between 0 and 90
  myservo.write(servoPos);  // write that value to the servo


}

Delta_G thank you- it works! Your comments were very helpful and I now understand what the problems were.