/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe
 
 This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */

int sensorcenter = A4;// select the input pin for the potentiometer
int sensortop = A3;
int sensorbottom = A2;
int sensorswitch = A5;
int outbottom = 2;// select the pin for the LED
int outcenter = 3;
int outtop = 4;
int outswitch = 5;
int sensorValuec = 0; 
int sensorValueb = 0;
int sensorValues = 0;
int sensorValuet = 0;
int ledPin = 13;// variable to store the value coming from the sensor

int cent = 0;
int bot = 0;
int top = 0;
int swit = 0;

void setup() {
  // declare the pwm as an OUTPUT:
  pinMode(outbottom, OUTPUT);  
  pinMode(outcenter, OUTPUT);
  pinMode(outtop, OUTPUT);
  pinMode(outswitch, OUTPUT); 
  pinMode(ledPin, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(7, OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  
  // read the value from the sensor:
  digitalWrite(outcenter, HIGH);
  sensorValuec = analogRead(sensorcenter); 
  if( sensorValuec > 1000)
  {
  digitalWrite(outcenter, LOW);
  sensorValuec = analogRead(sensorcenter);
  if(sensorValuec < 100 ){
   cent = 1; 
  }
  else {
    cent = 0;
  }
  }  
  else
  {
   cent = 0; 
  }
  digitalWrite(outcenter, LOW);

  digitalWrite(outbottom, HIGH);
  sensorValueb = analogRead(sensorbottom); 
  //Serial.println(sensorValueb);
  if( sensorValueb > 1000)
  {
    digitalWrite(outbottom, LOW);
    sensorValueb = analogRead(sensorbottom);
    if (sensorValueb < 100){
   bot = 1; 
    }
    else {
      bot = 0;
    }
  }  
  else
  {
   bot = 0; 
  }
  digitalWrite(outbottom, LOW);


  digitalWrite(outswitch, HIGH);
  sensorValues = analogRead(sensorswitch); 
  //Serial.println(sensorValues);
  if( sensorValues > 1000)
  {
    digitalWrite(outswitch,LOW);
    sensorValues = analogRead(sensorswitch);
    if(sensorValues < 100){
      digitalWrite(outswitch, HIGH);
      sensorValues = analogRead(sensorswitch);
      if(sensorValues > 1000){
        swit = 1; 
        digitalWrite(12, HIGH);
    }
    }
    else {
      digitalWrite(12, LOW );
      swit = 0; 
    }
  }  
  else
  {
    digitalWrite(12, LOW );
    swit = 0; 
  }
  digitalWrite(outswitch, LOW);
 
  digitalWrite(outtop, HIGH);
  sensorValuet = analogRead(sensortop);   
  //Serial.println(sensorValuet);
  if( sensorValuet > 1000)
  {
    digitalWrite(outtop, LOW);
    sensorValuet = analogRead(sensortop);
    if(sensorValuet < 100){
   top = 1; 
  }  
  }
  else
  {
   top = 0; 
  }
  digitalWrite(outtop, LOW);
  digitalWrite(outbottom, LOW);
  digitalWrite(outcenter, LOW);
  //Serial.println(top);
  //Serial.println(bot);
  Serial.println(cent);
  if(top == 1 && bot ==1 && cent ==1)
  {
  digitalWrite(ledPin, HIGH); 
  delay(100);
  if(swit ==1)
  {
   digitalWrite(7, HIGH); 
  }
  else{
    digitalWrite(7, LOW);
    delay(1000);
  }
  }
 else
{
 //Serial.println(top);
 
  digitalWrite(ledPin, LOW);
  delay(100);
                 
}

}
