Problems Using "While" Function

I am new to Arduino and want to use it to control a machine I am making, but I cannot to get a WHILE statement to work, altough it seems simple enough

The machine has a carriage and on startup I want to test that the carriage is in the home position, and if not to send it to the home position

The carriage has a microswitch that returns ground to digital input 4 if it is home
If it is not home I want to set digital output 8 to HIGH to operate the motor through a relay

The following is what I have written


void setup()
{
pinMode(4, INPUT);
pinMode(8, OUTPUT);

while (4 != LOW) // carriage not home
{
digitalWrite(8, HIGH); //operate motor to drive carriage home
if(digitalRead(4) == LOW) // Test to see if carriage is home
{
digitalWrite(8, LOW); // If carriage is home turn off motor
}
}
}
// setup complete & carriage home now start using the machine
void loop()
{
}


What I am getting is, regardless of whether the carriage microswitch is putting a LOW (gnd) or not to IP4, OP8 will always go HIGH and stay HIGH

Please help and if possible keep it simple so my old brain can comprehend
Many Thanks :slight_smile:

4 will never be the same as LOW.

I assume that you want to check the status of the pin in which case you need to use a digitalRead to read the pin.

Please read How to use this forum - please read, specifically point #7 about posting code using code tags.

I’ve deleted your other cross posts DenisHibbs.

Cross posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

Repeated cross posting will result in a suspension from the forum.

In the future, please take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky “How to use this forum - please read.” post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.