// Circuit 3A Alternative 1

#include <Servo.h> // Load the basic Arduino servo library

Servo myservo; // Instance a servo object called myservo from the library

// Variables that remain constant
const byte pinSwitch1 = 6; // Digital input pin from momentary switch
const byte pinSwitch2 = 7;
const byte pinServo = 9; // Digital PWM-enabled () output pin for servo
const byte servoAngleMin = 15; // Min and max angle values for the servo
const byte servoAngleMax = 165;

// Variables that can change
byte lastSwitch1State = HIGH; // Tracks the last switch 1 state, open (= HIGH) at start
byte lastSwitch2State = HIGH;

boolean switch1 = false; // Stores a flag for "presssed", true or false; we start with false
boolean switch2 = false;

void setup()
{
  // Serial printing only necessary to understand the switch transitions~
  // when pressed and released; you will see that a button press causes
  // two events, not just one
  Serial.begin(9600);

  // Initialise momentary switch pins
  pinMode (pinSwitch1, INPUT_PULLUP);
  pinMode (pinSwitch2, INPUT_PULLUP);

  // Associate the servo object with the pin the servo is wired to
  myservo.attach(pinServo);
}

void loop()
{
  // A call to this function checks the momentary switches for presses
  checkSwitches();

  // If the momentary switch 1 was pressed, if the flag was set to true
  if (switch1)
  {
    // Then rotate the servo to an angle of 15°
    myservo.write(servoAngleMin);

    // And set the flag immediately to "not pressed", so we get only a single action!
    switch1 = false;
  }

  else if (switch2)
  {
    myservo.write(servoAngleMax);
    switch2 = false;
  }
}

void checkSwitches ()
{
  // The momentary switches are hardware debounced with a 0.1uF capacitor;
  // no debouncing code is necessary. See http://www.gammon.com.au/switches
  // Read the voltage from the momentary switch pins to see if something
  // has changed (was a button pressed or released?)
  byte switch1State = digitalRead (pinSwitch1);
  byte switch2State = digitalRead (pinSwitch2);

  // Has the momentary switch state changed since the last time it was
  // checked?
  if (switch1State != lastSwitch1State)
  {
    // Then, test if the switch was closed (button pressed)
    if (switch1State == LOW)
    {
      // Here, you can do something on pressing the button
      Serial.println ("Switch 1 button pressed");

      // So we toggle the matching flag variable to "pressed"
      switch1 = true;
    }

    else
    {
      // Here, you can do something on releasing the button. To see it
      // clearly, hold the button down for a while before releasing it
      Serial.println ("Switch 1 button released");
    }
    // Last, store the current switch state for the next time around
    lastSwitch1State =  switch1State;
  }

  if (switch2State != lastSwitch2State)
  {
    if (switch2State == LOW)
    {
      Serial.println ("Switch 2 button pressed");
    }

    else
    {
      Serial.println ("Switch 2 button released");
      switch2 = true;
    }

    lastSwitch2State =  switch2State;
  }
}