/*
#include <Servo.h>
Servo servo;
byte servoPin = 4;
byte buttonpin[] = { 2, 3, 5, 6, 7 };                         // declare button pins in an array
byte element;                                                 // indicates pressed button/pin
byte buttonArray = sizeof(buttonpin) / sizeof(buttonpin[0]);  // get number of elements in button array

unsigned long timer;          // timer keeps track of length of button press (or noise/ringing).
unsigned long timeout = 100;  // timeout (in milliseconds) is the minimum for "a button is pressed".

byte redPin = 11, grnPin = 9, bluPin = 8;  // pins for the common cathode RGBLED

void setup() {
  Serial.begin(115200);                   // start serial monitor
  for (int i = 0; i < buttonArray; i++)   // count to number of elements in button array
    pinMode(buttonpin[i], INPUT_PULLUP);  // configure button pins for reading (LOW when pressed)

  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT); 
  pinMode(bluPin, OUTPUT);

  servo.attach(servoPin);
}

void loop() {
  if (element > buttonArray)                  // count through array... do not use for() loops. they blocking program flow.
    element = 0;                              // if beyond the array size, start again from the first element
  if (!digitalRead(buttonpin[element])) {     // button may have been pressed, or just noise/ringing
    if (millis() - timer > timeout) {         // measure time from last "press" to this "press. replaces delay();
      if (digitalRead(buttonpin[element])) {  // valid button was released after debounce timeout
        timer = millis();                     // reset timer after valid button press
        switch (element) {
          case 0: red(); break;
          case 1: grn(); break;
          case 2: blk(); break;
          case 3: blu(); break;
          case 4: yel(); break;
          default: break;
        }
      }
    }
  }
  element++;  // next array element
}

void blk() {
  digitalWrite(redPin,  LOW);
  digitalWrite(grnPin,  LOW);
  digitalWrite(bluPin,  LOW);
  servo.write(90); // left slow
}

void blu() {
  digitalWrite(redPin,  LOW);
  digitalWrite(grnPin,  LOW);
  digitalWrite(bluPin, HIGH);
  servo.write(45); // left slow
}

void yel() {
  digitalWrite(redPin, HIGH);
  digitalWrite(grnPin, HIGH);
  digitalWrite(bluPin,  LOW);
  servo.write(0); // left fast
}

void grn() {
  digitalWrite(redPin,  LOW);
  digitalWrite(grnPin, HIGH);
  digitalWrite(bluPin,  LOW);
  servo.write(135); // right slow
}

void red() {
  digitalWrite(redPin, HIGH);
  digitalWrite(grnPin,  LOW);
  digitalWrite(bluPin,  LOW);
  servo.write(180); // right fast
}
//diagram.json for wokwi

*/

// https://forum.arduino.cc/t/servo-with-joystick-x-and-y-question/1322442
// https://wokwi.com/projects/414736427464105985

# include <Servo.h>

# define bRedFast    2
# define bGreenSlow  3
# define bBlueSlow   6
# define bYellowFast 7

# define bStop       5   // unused here?

# define xServoPin   4
# define yServoPin   12

byte redPin = 11, grnPin = 9, bluPin = 8;  // pins for the common cathode RGBLED

const byte ServoPin = 4;

Servo sx360;

void setup()
{
  sx360.attach(xServoPin);

  pinMode(bRedFast, INPUT_PULLUP);
  pinMode(bGreenSlow, INPUT_PULLUP);
  pinMode(bStop, INPUT_PULLUP);
  pinMode(bBlueSlow, INPUT_PULLUP);
  pinMode(bYellowFast, INPUT_PULLUP);

  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT); 
  pinMode(bluPin, OUTPUT);
}

void loop()
{
  int speed;

  bool any = false;
  speed = 90;  // unless we say otherwise, stopped
  if (digitalRead(bRedFast) == LOW) {
    speed = 180;
    any = true;
    red();
  }
  if (digitalRead(bGreenSlow) == LOW) {
    speed = 135;
    any = true;
    grn();
  }
  if (digitalRead(bBlueSlow) == LOW) {
    speed = 45;
    any = true;
    blu();
  }
  if (digitalRead(bYellowFast) == LOW) {
    speed = 0;
    any = true;
    yel();
  }

  if (!any) blk();

  sx360.write(speed); 
}


void blk() {
  digitalWrite(redPin,  LOW);
  digitalWrite(grnPin,  LOW);
  digitalWrite(bluPin,  LOW);
}

void blu() {
  digitalWrite(redPin,  LOW);
  digitalWrite(grnPin,  LOW);
  digitalWrite(bluPin, HIGH);
}

void yel() {
  digitalWrite(redPin, HIGH);
  digitalWrite(grnPin, HIGH);
  digitalWrite(bluPin,  LOW);
}

void grn() {
  digitalWrite(redPin,  LOW);
  digitalWrite(grnPin, HIGH);
  digitalWrite(bluPin,  LOW);
}

void red() {
  digitalWrite(redPin, HIGH);
  digitalWrite(grnPin,  LOW);
  digitalWrite(bluPin,  LOW);
}
L slow (45)
L fast (0)
R slow (135)
R fast (180)
Unused