void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  pinMode(A0, INPUT);
}

void loop() {
  static bool inLimitSetup = false; // last state memory
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);

  bool inLimit = (analogRead(A0) > 160 && (analogRead(A0) < 185));
  bool valueEnteredLimits = inLimit & inLimitSetup;
  inLimitSetup = !inLimit;

  if(valueEnteredLimits){
    digitalWrite(2, !digitalRead(2));
  }

  /*if (((analogRead(A0) > 160) && (analogRead(A0) < 185)) && digitalRead(2) == LOW) {
    digitalWrite(2, HIGH);
  }

  if ((analogRead(A0) > 160) && (analogRead(A0) < 185) && digitalRead(2) == HIGH) {
    digitalWrite(2, LOW);
  }*/
}