// Sweep // by BARRAGAN // This example code is in the public domain. #include 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 int startlight = 0 ; int startaccel = 0 ; int lightthreshold = 20 ; int accelthreshold = 3 ; int newlight = 0 ; int newaccel = 0 ; void setup() { delay(200) ; myservo.attach(9); // attaches the servo on pin 9 to the servo object startlight = analogRead(A0) ; startaccel = analogRead(A5) ; Serial.begin(9600) ; } void loop() { delay(5) ; newlight = analogRead(A0) ; newaccel = analogRead(A5) ; Serial.print(startlight) ; Serial.print(", ") ; Serial.print( newlight) ; Serial.print( ". ") ; Serial.print(startaccel) ; Serial.print(", ") ; Serial.println(newaccel) ; if ( ( abs (startlight - newlight) > lightthreshold) || (abs (startaccel - newaccel) > accelthreshold) ) { 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(5); // 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(5); // waits 15ms for the servo to reach the position } delay(200); } }