I am trying to put together a little sketch to run a dc motor based on LDR readings....but to give it some general idea of where it is in the world I am making a rookie attempt at implementing a homing sequence every time the system is powered up.
I have read on the intertube that this code needs to be implemented in the setup loop so that it only runs once. But no matter where i seem to put it it kills the serial print of my LDR readings which determine the thresholds of my motor commands.
Can any of you gentleman(or ladies) help me isolate what is wrong....the code that seems to be
causing the problem is the "while statements" commented out in the setup loop.
Link to drawing:----Updated--->
// Control loop to start and stop a motor based on LDR readings
int sensorPin = A0; // select the input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor
int doorOpen = 2; //
int doorClose = 3; //
const int limitSwitch = 4; //limit switch
void setup() {
pinMode(2, OUTPUT); //pin connected to the l298n (open door)
pinMode(3, OUTPUT); //pin connected to the l298n (close door)
pinMode(limitSwitch,INPUT); //defines limit switch as input
Serial.begin(9600); //sets serial port for communication
/* while(digitalRead(limitSwitch) == LOW) // Door is not fully open at start
digitalWrite(doorOpen, HIGH); // To raise door
while(digitalRead(limitSwitch) == HIGH) //Door is fully open at start
digitalWrite(doorOpen, LOW); // Motor does nothing
*/
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
if(sensorValue < 500) //setting a threshold value
digitalWrite(doorOpen,HIGH); //turn relay ON
else digitalWrite(doorOpen,LOW); //turn relay OFF
delay(100);
if(sensorValue > 500) //setting a threshold value
digitalWrite(doorClose,HIGH); //turn relay ON
else digitalWrite(doorClose,LOW); //turn relay OFF
delay(100);
if( digitalRead(limitSwitch) == HIGH){
digitalWrite(doorOpen,LOW);
delay(500);}
}