/* Servo_one_button.ino
*  --------------------
*  Programme élaboré en complétant l'exemple Debounce de l'IDE
*  Les lignes ajoutées ont un commentaire en français */

#include <Servo.h>

Servo Servo_1; /* Crée un objet de type servo appelé Servo_1 */

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int pinServo_1 = 7;   /* Broche commandant le servomoteur 'Servo_1' */
const int ledPin = 13;      // the number of the LED pin
const int direct = 0;       /* Butée du mouvement servomoteur */
const int deviee = 20;      /* Butée du mouvement servomoteur */

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int servoState = HIGH;       /* HIGH aiguille en direct et LOW en deviée */
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int posServo_1;              /* Position intermédiaire pour le servomoteur */
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Servo_1.attach(pinServo_1);  /* Servomoteur relié à pinServo_1 */
  // set initial LED state
  digitalWrite(ledPin, ledState);
  Servo_1.write(direct);       /* Initialise la position du servo en direct */
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
        servoState = !servoState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  /* Bouge le servomoteur avec un test sur variable servoState */
  if(servoState == LOW) {
      while(posServo_1 <= deviee) {
      Servo_1.write(posServo_1);
      delay(50);  // peut etre ajuste pour regler vitesse de deplacement
      posServo_1 = posServo_1 + 1;
    }
  }
  if(servoState == HIGH) {
      while(posServo_1 >= direct) {
      Servo_1.write(posServo_1);
      delay(50);  // Peut etre ajuste pour regler vitesse de deplacement
      posServo_1 = posServo_1 - 1;
    }
  }
  /* Fin de traitement du servomoteur */

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}