fabianmoronzirfas
12/19/2014 - 2:38 PM

a sketch for lennart b

a sketch for lennart b

// include the lib
#include <Servo.h>
int pos = 0; // the servo position
int servopin = 9; // the serrvo pin
Servo myservo; // the servo
long previousMillis = 0; // taking time
long interval = 20;// the time to wait
boolean setback = false;// if true we will set back the servo

void setup{

  myservo.attach(servopin); // attach the servo to the PWM pin


}

void loop(){
  unsigned long currentMillis = millis(); // take the current time
  int ldrvalue = analogRead(0); // read the LDR

  // now if the servo is not fully turned up
  // and the time interval has passed
  // and we are not setting the servo back
  // change the position of the servo and update the pos value
  if(pos < 180 && currentMillis - previousMillis > interval && setback == false){
     previousMillis = currentMillis;
     myservo.write(pos);
     pos++;
  }
  // check the value from the LDR
  // if it is high enough we setback the servo
  // also we do that only when the servo is fully turned up
  if(ldrvalue > 700 && pos == 180){
    setback = true;
  }
  // here is the setback mode
  // it is a simple loop that uses an delay
  // so we can be sure nothing interferes with our setback
  // // if it is done wer return to the beginning
  if(setback == true){
    for(int i = 180; i >= 0; i--){
      myservo.write(i);

      // set everything back to the beginning
      if(i == 0){
        pos = 0;
        setback = false;
      }
    }
  }

}