control relay and voltage

/*what i would like to do is control two relays with output 12 and 11
11 would be 110 volts shore power
12 would be to control battery inverter to operate fridge
I have solar panels that charge the batteries and do make more power than i need so i want to use excess power to run fridge

so at this time i am using a potentiometer to control pin 0 control voltage

at this time when my sensorvalue is over 600 pin 12 HIGH, pin 11 LOW, this turns on inverter power and fridge runs on batteries.
but when sensor value under 600 pin 12 LOW and pin 11 HIGH, frige back on 110 volt shore power

what i would like it to do is keep pin 12 HIGH, pin 11 LOW until reading is 400 // under batteries are drained some
then when sensor value is bellow 400 switch back to pin 12 LOW, pin11 HIGH, // run on 110 volt shore power and let batteries charge on solar panels

When sensor value isat 600 start over again

( at this time the sensor value of 600 and 400 are just to write code and is not the true voltage )*/

( at this time the 600 and 400 are just to write code and is not the true voltage )*/

/*

The circuit:

  • potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
  • shore power relay connected to pin 11
    *battery relay connected to 12

*/

// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to potentiometer
const int ledPin = 11; // pin that the LED is attached to shore power
const int ledPing = 12; // pin that green led battery power
const int threshold = 600; // when over 600 switch to battery power in it reaches lower limit
const int lowerlimit =400; // when switch to shore power turn battery power off
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPing, OUTPUT);
digitalWrite(ledPin,HIGH);
digitalWrite(ledPing,LOW); // pin that green led battery power

// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);

// if the analog value is high enough, turn on the battery power:
if (analogValue > threshold)
digitalWrite(ledPin, LOW);
if (analogValue > threshold) {
digitalWrite(ledPing, HIGH); // pin that green led battery power
}
else {

digitalWrite(ledPin,HIGH);
digitalWrite(ledPing,LOW); // pin that green led battery power
}

// print the analog value:
Serial.println(analogValue);
delay(100); // delay in between reads for stability
}

You need to maintain state if you want hysteresis - a variable or variables are needed to
do this. This allows behaviour to be sensitive to history.

I think you should learn a little about state transition diagrams and coding a state
machine in software. Very useful to understand these.