//www.elegoo.com
//2018.10.25

/*
  Stepper Motor Control - one revolution

  This program drives a unipolar or bipolar stepper motor.
  The motor is attached to digital pins 8 - 11 of the Arduino.

  The motor should revolve one revolution in one direction, then
  one revolution in the other direction.

*/

#include <Stepper.h>


const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
const int rolePerMinute = 17;         // Adjustable range of 28BYJ-48 stepper is 0~17 rpm

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
  myStepper.setSpeed(rolePerMinute);
  // initialize the serial port:
  Serial.begin(9600);
}



void loop() {
  char inputVariable[4];
  Serial.print('\n');
  Serial.println("Enter first letter of each character in intiative no spaces");
  while(Serial.available() < 4) { }
  for(int i = 0; i < 4; i++){
    inputVariable[i] = Serial.read();
    Serial.println(inputVariable[i]);
  }
  serialFlush();
  bool nextTurn = true;
  int count = 0;
  int position = 0;
  int waiting = 1;
  while(nextTurn == true){
    count = 0;
    while(count < 4){
      if(inputVariable[count] == 'a'){
        Serial.println("Next Player is Arith" );
        if(position == 0){
        myStepper.step(896);
        position = 896;
        }
        else{
          myStepper.step(-position);
          myStepper.step(896);
          position = 896;
        }
      }
      if(inputVariable[count] == 'b'){
        Serial.println("Next Player is Bort" );
        if(position == 0){
        myStepper.step(-640);
        position = -640;
        }
        else{
          myStepper.step(-position);
          myStepper.step(-640);
          position = -640;
        }
      }
      if(inputVariable[count] == 'j'){
        Serial.println("Next Player is Jamie" );
        if(position == 0){
        myStepper.step(640);
        position = 640;
        }
        else{
          myStepper.step(-position);
          myStepper.step(640);
          position = 640;        
        }
      }
      if(inputVariable[count] == 'k'){
        Serial.println("Next Player is Knock" );
        if(position == 0){
        myStepper.step(-896);
        position = -896;
        }
        else{
          myStepper.step(-position);
          myStepper.step(-896);
          position = -896;
        }
      }
      count++;
      waiting = 0;
      if(count < 4){
        serialFlush();
        Serial.println("Enter 'g' for next player.");
        while(Serial.available() == 0) { }
        while(waiting == 0){
          if(Serial.read() == 'g'){
            Serial.println("LETS GOO");
            waiting = 1;
            serialFlush();
          }
        serialFlush();
        }
      }
    }
    if(position != 0){
      myStepper.step(-position);
      position = 0;
    }
    serialFlush();
    Serial.println("Another Round? (y/n)");
    while(Serial.available() == 0) { }
    if(Serial.read() == 'n'){
      nextTurn = false;
    }
  }
}

void serialFlush(){
  while(Serial.available() > 0) {
    char t = Serial.read();
  }
}