Homing sequence is breaking my code....!?

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
  if(sensorValue > 500)                             //setting a threshold value
  digitalWrite(doorClose,HIGH);                      //turn relay ON
  else digitalWrite(doorClose,LOW);                  //turn relay OFF

   if( digitalRead(limitSwitch) == HIGH){

Before anyone looks at your code, they need to see a diagram of how you have all this wired up and powered.


First thing I see is limit sw pin is set as input and you look for a HIGH, do you have a pulldown resistor (10k) from pin to GND so the pin is not floating when sw is open and generating false HIGHs? Are your relays energized by a HIGH or LOW signal?

My apologies....here is a drawing of the layout.

Tried to attach a screenshot from Fritzing but it was removed....please advise.

Thank you!

Please don't waste your and our time with Fritzing. Make a pencil drawing, scan or take a picture, and post that.

Updated photo....

Thanks for the advice jremington....I already had it drawn up there so here is the link....let me know if it is useful.

You are putting 12V on Nano pin 4. Connect one switch lead to Nano 5V, other lead to pin 4, pulldown resistor from pin 4 to Nano GND.

Thanks Outsider....now that you mention it I have 12v going to the ldr as well....I will fix those and upload another photo.

Just oversights from hastily throwing together a drawing a drawing and trying to get it up quickly.

Please use the "schematic view" in F**ing. It doesn't look as colorful and takes a little more effort to move things around to minimise line-crossings, but the result is much more readable.

In your code, make the "homing sequence" a function. Then you can call that function from setup() or anywhere else you need to use it.

Wiring the switch with one leg to ground, and one leg to the digital pin, and using INPUT_PULLUP as the mode makes for MUCH easier wiring and fewer components.

Much thanks to Morgan and Paul! Good constructive info!