Hello, I have some code here that i have tried re-writing multiple times, and it still doesn't seem to be doing what I'd like. The point is to activate pin 3 after light hits a sensor for x seconds (5 in this case). I have a feeling the order of my if statements might be the culprit, but any input would be much appreciated.
I know that the wiring is okay because the part of the program that is controlled by the button works just fine.
// Label pins
int lightSensorPin = A0;
int buttonPin = 2;
int mosfetPin = 3;
int ledPin = 13;
// Initialize main variables
int light = 80;
int dark = 1;
unsigned long startMillis;
unsigned long currentMillis;
unsigned long otherMillis;
int power = 255;
int onTime = 2500;
void setup(){
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(mosfetPin, OUTPUT);
pinMode(lightSensorPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
// Loop breaks that I'm not yet sure about
int breaker = 0; //#1
int powerBreak = 0; //#2
void loop(){
// Start millis in loop
startMillis = millis();
// Convert button from positive pullup
int button = digitalRead(buttonPin);
button = !button;
// Read light value from light pin
int lightValue = analogRead(lightSensorPin);
// 5 Second light protection
//#1
if (breaker == 0 && lightValue > light){
otherMillis = millis();
breaker = 1;
}
if (lightValue < light){
breaker = 0;
}
//#2
if (powerBreak = 0 && startMillis - otherMillis > 5000){
analogWrite(mosfetPin, power);
powerBreak = 1;
}
if (lightValue < dark){
powerBreak = 0;
}
// Button activates mosfet
if (button){
analogWrite(mosfetPin, power);
currentMillis = millis();
}
// Turn off motor after onTime
if (startMillis - currentMillis > onTime){
digitalWrite(mosfetPin, 0);
}
// LED if mosfet is getting power
if (digitalRead(mosfetPin) == HIGH){
digitalWrite(LED_BUILTIN, HIGH);
}
if (digitalRead(mosfetPin) == LOW){
digitalWrite(LED_BUILTIN, LOW);
}
// Print things to test
Serial.println(lightValue);
}
Any help/ input would be appreciated.