LDR mining rig switch with solar power wall

Hi all,
I’m new to this programming but learning quick,
I have a solar power wall (built out of nearly 2000 18650 batteries) running my house, I want my mining rig to switch on when the sun is up and strong,
I have made a demo using an led for a start, I’ve done most of the programming so the led comes on and off at the settings but my code is wrong,
This is my code:-

const int ledPin = 13;
const int ldrPin = A0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
static boolean overUpper=false;
static boolean underLower=false;

int ldrStatus = analogRead(ldrPin);
if (ldrStatus >=400 && ! overUpper) {
overUpper=true;
underLower=false;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
Serial.println(ldrStatus);
} else {

if (ldrStatus <=200 && ! underLower) {
overUpper=false;
underLower=true;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
Serial.println( ldrStatus);
}
if(ldrStatus<400 ) overUpper=false;
if(ldrStatus>200) underLower=false;
}
}

I want the led to come on once when it passes 400 and off when it passes 200
At the moment it comes on when it passes 400 but if it goes down to let’s say 300 then back up to 400 it will come on again, same when it passes 200 then goes up and down to 200 again,

I’m a little stuck now due to me only being a beginner,
any help in the right direction would be greatly appreciated,
Thanks in advance,
Phil.

Just figured it out,
This is how I did it:-

const int ledPin = 13;
const int ldrPin = A0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
static boolean overUpper=false;
static boolean underLower=false;
int ldrStatus = analogRead(ldrPin);
if (ldrStatus >=480 && ! overUpper){
overUpper=true;
underLower=false;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
Serial.println(ldrStatus);
} else {

if (ldrStatus <=80 && ! underLower) {
overUpper=false;
underLower=true;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
Serial.println( ldrStatus);
}
if (ldrStatus <=480 && ldrStatus >=80)
digitalWrite(ledPin, LOW);
}
}

Thanks,
Phil.
:slight_smile: