//Define the pins for the Stepper motor
#define pin1 8
#define pin2 9
#define pin3 10
#define pin4 11
//Defines space between the frequency waves of the piezo
#define delaytime 8
//Sets the photocell value to the minimum of the room and the 
// maximum to the maximum resistance
int sensorValue;
int sensorHigh = 1023;
int sensorLow = 0;
const int ledPin = 13;

const int buzzerPin = 5;
int duration = 500; //change the duration of the note and break in between
int threshold = 500; // NEEDS TO BE MODIFIED
int photoResistor[6];
int photoResistPins[6] = {A0, A1, A2, A3, A4, A5}; // NEEDS TO BE MODIFIED
int onValue[6] = {1, 2, 4, 8, 16, 32};
int currentValue;
int frequency;

void setup(){
  pinMode(pin1, OUTPUT); //set all stepper pins to output
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);

       Serial.begin(9600);
       pinMode(buzzerPin, OUTPUT);
       Serial.println("Setup Complete");
    }

  
  
void loop(){
  forward();//constant forward loop for stepper

currentValue = 0;
  Serial.println("Main Loop Begin");
  
  // analog read photoresistors 0-5
  for (int current = 0; current < 6; current++) {
    Serial.println("For loop #" + current);
    
    photoResistor[current] = analogRead(photoResistPins[current]);
    Serial.print("photoResistor[");
    Serial.print(current);
    Serial.print("] = ");
    Serial.println(photoResistor[current]);
    
    // check if current photo resistor is above threshold
    if (photoResistor[current] > threshold) {
      Serial.println("Above threshold");
      // if it is, add the respective 'onValue'
      currentValue += onValue[current];
      Serial.print("Current Value =");
      Serial.println(currentValue);
    }
    else {
      Serial.println("Not above threshold");
    }
   
  }
  Serial.println("Current Value: " + currentValue);
  Serial.println("********************");
  switch(currentValue) {
    case 1:
      Serial.println("Case 1");
      frequency = 175;
      break;
      
    case 2:
      Serial.println("Case 2");
      frequency = 185;
      break;
      
    case 4:
    Serial.println("Case 4");
      frequency = 233;
      break;
      
    case 8:
    Serial.println("Case 8");
      frequency = 247;
      break;
  
    case 16:
    Serial.println("Case 16");
      frequency = 258;
      break;
  
    case 32:
    Serial.println("Case 32");
      frequency = 262;
      break;
  
    case 3:
    Serial.println("Case 3");
      frequency = 277;
      break;
  
    case 5:
    Serial.println("Case 5");
      frequency = 294;
      break;
  
    case 6:
    Serial.println("Case 6");
      frequency = 311;
      break;
  
    case 7:
    Serial.println("Case 7");
      frequency = 330;
      break;
  
    case 9:
    Serial.println("Case 9");
      frequency = 349;
      break;
  
    default:
    Serial.println("Case default");
      frequency = 0;
      break;
  }
  
  Serial.println("********************");
  
  // frequency() required
  tone(buzzerPin, frequency, duration);
  delay(duration);

}

void Step_OFF(){//resets the stepper
  digitalWrite(pin1, LOW);
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
}

void Step_A(){//starts stepper
  digitalWrite(pin1, HIGH);
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
}
void Step_B(){
  digitalWrite(pin1, LOW);
  digitalWrite(pin2, HIGH);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
}
void Step_C(){
  digitalWrite(pin1, LOW);
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, LOW);
}
void Step_D(){
  digitalWrite(pin1, LOW);
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, HIGH);
}//ends and restarts the loop

void forward(){
  Step_A();
  delay(delaytime);
  Step_B();
  delay(delaytime);
  Step_C();
  delay(delaytime);
  Step_D();
  delay(delaytime);
}

void backward(){
  Step_D();
  delay(delaytime);
  Step_C();
  delay(delaytime);
  Step_B();
  delay(delaytime);
  Step_A();
  delay(delaytime);
}