Using a reed switch to control a slide projector focus motor

Hi all,

I've been using an arduino to control the focus motor of a 35mm slide projector. The idea is to have the motor run in one direction for a certain number of seconds, then run the other way until a reed switch is triggered by a magnet. I would like to have the focus motor run UNTIL the reed switch goes HIGH, at which point, I'd like the motor to stop.

Here's what I've got so far. led and led2 control a couple of relays, which control the focus motor. The place I've run into trouble is down at the do-while command I was trying to use.

Thanks for any thoughts!

int led = 7; int led2 = 6;

const int pinSwitch = 12; //Reed Pin on Arduino

int StatoSwitch = 0;// reed switch

void setup() {

pinMode(led, OUTPUT); //controls relay 1 pinMode(led2, OUTPUT); // controls relay 2 pinMode(pinSwitch, INPUT); //reed switch }

void loop() {

digitalWrite(led, LOW); digitalWrite(led2, HIGH); delay(12000); // focus motor runs forward for 12 secs

digitalWrite(led2, LOW); delay(2000); //focus motor pauses 2 secs

// This is the part I'm confused about. The do-while command was just a shot in the dark...

StatoSwitch = digitalRead(pinSwitch);

do { digitalWrite(led, HIGH); digitalWrite(led2, HIGH);

} while (digitalRead(StatoSwitch == LOW));

// This code manages to run the focus motor while the reed switch is unconnected (LOW), but // when the reed makes contact w/ the magnet (HIGH), nothing changes and the focus // motor just keeps running.

digitalWrite(led2, LOW);// This line turns the motor off... delay(8000);

}

The place I've run into trouble is down at the do-while command I was trying to use.

What trouble? I've been writing C and C++ code for 25+ years. I can count one one hand the number of times I've used do/while.

This

digitalRead(StatoSwitch == LOW)

looks peculiar and probably doesn't do what you think it does.

do
    {      digitalWrite(led, HIGH);
           digitalWrite(led2, HIGH);

    } while (digitalRead(StatoSwitch == LOW));

And before someone else beats me to it - use "blink without delay" and post your code properly.

...and it the meantime change

while (digitalRead(StatoSwitch == LOW));

to while (!digitalRead(StatoSwitch));

If you get into habit to let the compiler evaluate the statement WITHOUT using "==" you will not have to chase these types of bugs.

If you get into habit to let the compiler evaluate the statement WITHOUT using "==" you will

be an idiot. There is no proof that the == LOW portion of that statement IS a problem, let alone THE problem.

Stop offering such stupid suggestions.

digitalRead(...) is documented to return either HIGH or LOW. As such, ==LOW or ==HIGH is required (unless you know something about what LOW and HIGH represent and are willing to take a chance that this will always be true).

HOWEVER, the statement

while (digitalRead(StatoSwitch == LOW));

probably has a misplaced right parenthesis and should be corrected.