/*crea un programa para Arduino que haga lo siguiente:
 si recibe por el puerto serie una "a" active el pin 2 de arduino
  durante 1 segundo, si recibe una "b" active el pin 3, 
  así sucesivamente hasta el pin 15. 
  Pero si recibe una "s" active el pin 16 durante 3 segundos 
  y si recibe una "r" active el pin 17 durante 30 segundos 
  a menos que vuelva a recibir una "r" , lo cual desactiva el pin 17.
*/
const int pinA = 2;
const int pinB = 3;
const int pinC = 4;
const int pinD = 5;
const int pinE = 6;
const int pinF = 7;
const int pinG = 8;
const int pinH = 9;
const int pinI = 10;
const int pinJ = 11;
const int pinK = 12;
const int pinL = 13;
const int pinM = 14;
const int pinN = 15;
const int pinO = 16;
const int pinP = 17;

int receivedValue;
bool rFlag = false;
unsigned long rTimer = 0;
bool pFlag = false;
unsigned long pTimer = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinH, OUTPUT);
  pinMode(pinI, OUTPUT);
  pinMode(pinJ, OUTPUT);
  pinMode(pinK, OUTPUT);
  pinMode(pinL, OUTPUT);
  pinMode(pinM, OUTPUT);
  pinMode(pinN, OUTPUT);
  pinMode(pinO, OUTPUT);
  pinMode(pinP, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    receivedValue = Serial.read();

    switch (receivedValue) {
      case 'a':
        digitalWrite(pinA, HIGH);
        delay(1000);
        digitalWrite(pinA, LOW);
        break;
      case 'b':
        digitalWrite(pinB, HIGH);
        delay(1000);
        digitalWrite(pinB, LOW);
        break;
      case 'c':
        digitalWrite(pinC, HIGH);
        delay(1000);
        digitalWrite(pinC, LOW);
        break;
      case 'd':
        digitalWrite(pinD, HIGH);
        delay(1000);
        digitalWrite(pinD, LOW);
        break;
      case 'e':
        digitalWrite(pinE, HIGH);
        delay(1000);
        digitalWrite(pinE, LOW);
        break;
      case 'f':
        digitalWrite(pinF, HIGH);
        delay(1000);
        digitalWrite(pinF, LOW);
        break;
      case 'g':
        digitalWrite(pinG, HIGH);
        delay(1000);
        digitalWrite(pinG, LOW);
        break;
      case 'h':
        digitalWrite(pinH, HIGH);
        delay(1000);
        digitalWrite(pinH, LOW);
        break;
      case 'i':
        digitalWrite(pinI, HIGH);
        delay(1000);
        digitalWrite(pinI, LOW);
        break;
      case 'j':
        digitalWrite(pinJ, HIGH);
        delay(1000);
        digitalWrite(pinJ, LOW);
        break;
      case 'k':
        digitalWrite(pinK, HIGH);
        delay(1000);
        digitalWrite(pinK, LOW);
        break;
      case 'l':
        digitalWrite(pinL, HIGH);
        delay(1000);
        digitalWrite(pinL, LOW);
        break;
      case 'm':
        digitalWrite(pinM, HIGH);
        delay(1000);
        digitalWrite(pinM, LOW);
        break;
      case 'n':
        digitalWrite(pinN, HIGH);
        delay(1000);
        digitalWrite(pinN, LOW);
        break;
      case 'o':
        digitalWrite(pinO, HIGH);
        delay(3000);
        digitalWrite(pinO, LOW);
        break;
            case 's':
        digitalWrite(pinO, HIGH);
        delay(3000);
        digitalWrite(pinO, LOW);
        break;
      case 'r':
        rFlag = !rFlag; // toggle
        if (rFlag) {
          pFlag = true;
          rTimer = millis();
          digitalWrite(pinP, HIGH);
        }
        else {
          pFlag = false;
          digitalWrite(pinP, LOW);
        }
        break;
    }

    // check if pinP should be turned off
    if (pFlag && (millis() - rTimer) >= 30000) {
      pFlag = false;
      digitalWrite(pinP, LOW);
    }
  }
}