/* FULLNEST_EMAIL_FLAG_IMAGE
This sketch controls one servo motor, to which a flag is attached to. It waits for a signal from 
Raspberry Pi and, upon getting it moves the servo to its maximum angle position, with that raising
the flag. Pressing a button, which is attached to the Arduino, allows to manually, move the servo to its
minimum position and the flag down. */

#include <Servo.h>

#define MIN_ANGLE 0            // depending on your application, you can define
#define MAX_ANGLE 90           // your own minimum and maximum angles the servo

#define SERVO_1_PIN 3          // attaching servo to pin 3

Servo servo1;

int servo1pos = MIN_ANGLE;      // servo is set to minimum angle position, as default


void setup()
{
  servo1.attach(SERVO_1_PIN);      
  pinMode(5, INPUT_PULLUP);        // button on pin 5
  
  Serial.begin(9600);              //initiating Serial communication with Raspberry Pi
}

void loop()
{
  if (Serial.available())
  {
    char ch = Serial.read();                 // Arduino 'waits' for commands from RasPi,
    if (ch == 'a')  servo1pos = MAX_ANGLE;  
    // if it receives the 'a' command, it moves servo to max.angle position = raising the flag
   
   if (digitalRead(5) == LOW){servo1pos = MIN_ANGLE;    
   //But Arduino is also 'controlled' by the button-switch. And here, pressing the button results in
   //servo moving to min.angle position = flag going down

  }
  servo1.write(servo1pos);
  delay(20);
  }}
