Sensor Tutorial

Tutorials on the individual components:
Code
The code that combines the servo and light sensor together.
The servo will try to move away from dark.
Copy and paste the code below:
/*
* Code for the July 26, 2014
*
*/
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
#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
void setup(void) {
  // We’ll send debugging information via the Serial monitor
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
void loop(void) {
  photocellReading = analogRead(photocellPin);
  Serial.print(“Analog reading = “);
  Serial.println(photocellReading);     // the raw analog reading
  Serial.print(“Servo posotopn= “);
  Serial.println(myservo.read());     // the raw analog reading
  myservo.write(2);
  //Move the servo based on the photocell reading
  if  (analogRead(photocellPin) <300) { //Threshold to see if touching the sensor
    int temp = myservo.read() + random(0, 100); //add a random angle between 0 and 100 to current servo position
    //Make sure the angle is not higher then maximum of 180 degrees
    if (temp >= 180) {
      temp = myservo.read() – random(0, 110); // if it is substract a random number
    }
    myservo.write(temp); //rotate the servo
    delay(100); //make sure the servo has time to rotate
  }//end if
  //Add a delay for readibility
   //delay(500);
}//end loop