Reversing dc motor with momentary press of microswitches

I would be grateful if I could get some advice on a problem operating a dc motor. The motor has a reduction gear and rotates a small camera platform through about 90 degrees until a bar on the platform hits a microswitch and reverses the direction.
I am struggling to get the prog to operate with two limit switches. I have got the system sort of working so the platform rotates clockwise until one limit switch is pressed then it rotates anticlockwise for 5 secs then back to clockwise.
I do need to get it working with both limit switches (removing the cheat with the 5 sec timer) and would be grateful for any advice. I am using the arduino motor shield and unfortunately need to stick with dc motors.
Thanks Kenneth

const int
PWM_A   = 3,
DIR_A   = 12,

limitcw  = 7,
limitanticw  = 2,

SNS_A   = A0;


void setup() {
  // Configure the A output

  pinMode(DIR_A, OUTPUT);    // Direction pin on channel A
    pinMode(limitcw, INPUT);    // Direction pin 7 on channel A
     pinMode(limitanticw, INPUT);    // Direction pin   2 on channel A
 Serial.begin(9600);
  Serial.println("camera panning test");
  delay (500);
}

void loop() {


if (digitalRead(7) == HIGH)  // if the limit switch is high

doanti();                //do the anticlockwise subroutine belo


else                          //or else
Serial.println("clockwise");
  digitalWrite(DIR_A, HIGH);    // now change the direction to backward setting HIGH the DIR_A pin
 analogWrite(PWM_A, 255);       //speed high
}



void doanti() 
{  
  Serial.println("anticlockwise");
  digitalWrite(DIR_A, LOW);   // setting direction to LOW the motor will spin forward

  analogWrite(PWM_A, 255);     // Set the speed of the motor, 255 is the maximum value

delay (5000);                  //set delay time  before returing to main loop
}

(deleted)

(deleted)

Thanks for your reply. I downloaded your solution but it only reverses direction while the microswitch is activated. The limit switches are only pressed momentarily but this needs to reverse the motor until the other limit switch is pressed momentarily. This will allow the rotating table to continue operating between the limitswitches
Any more ideas?
Thanks

i think this is a classic case for state machines.
have a look at the Example of StateChangeDetection in your Arduino IDE (i have IDE 1.0.5)
this toggles a LED based on momentary presses of a button.
i think you could incorporate that into your code

(deleted)

void setup() {
  // Configure the A output

  pinMode(DIR_A, OUTPUT);    // Direction pin on channel A
    pinMode(limitcw, INPUT);    // Direction pin 7 on channel A
     pinMode(limitanticw, INPUT);    // Direction pin   2 on channel A

Why does
your code
march across
the page like
that? Can't
you control it?

Tools + Auto Format can.

How have you got your limit switches connected? When closed do they pull the pin low or high?
It would be much clearer to us (and you in 6 months time) if you labeled your limit switch pins 'pinLimitLeft' and 'pinLimitRight' and your movement direction as left and right or Left and !Left (= NOT Left).
If the right limit switch is closed you want your motor to next go to the left and vice versa. It would be a good idea to stop the motor for a short while at the end stop to avoid putting a strain on the motor when it goes from full speed in one direction to full speed in the other.

Thanks for all your help- The prog is now working. And to annoy PaulS I will not be using autoformat. I am currently connecting both limit switches to pin7 and this works fine. I am going to try and simplify it a bit so that a momentary press just switches motor dir rather than the convoluted counting system that is currently in use. I also need to use a light activation system

const int
PWM_A   = 3,
DIR_A   = 12,
ledPin  = 13,
limitsw  = 7,
LDR_pin  = A0;  // choose the input pin 
int LDRreading = 0;  // variable to store the value coming from the sensor


// Variables will change:
int microswitchCounter = 0;   // counter for the number of button presses
int switchState = 0;         // current state of the button
int lastswitchState = 0;     // previous state of the button

void setup() {
  pinMode(DIR_A, OUTPUT);    // Direction pin on channel A
  pinMode(limitsw, INPUT);    // Direction pin 7 on channel A
  Serial.begin(9600);
  Serial.println("camera panning test");
  delay (500);
}

void loop() {
  {
    // read the pushbutton input pin:
    switchState = digitalRead(limitsw);

    // compare the buttonState to its previous state
    if (switchState != lastswitchState) {
      // if the state has changed, increment the counter
      if (switchState == HIGH) {

        microswitchCounter++;
        delay(100);                          

        Serial.print("number of microswitch pushes:  ");
        Serial.println(microswitchCounter);
      } 





    }
    // save the current state as the last state, 
    //for next time through the loop
    lastswitchState = switchState;
    // 
    if (microswitchCounter % 2 == 0) {
      digitalWrite(DIR_A, HIGH);    // now change the direction to backward setting HIGH the DIR_A pin
      analogWrite(PWM_A, 255);       //speed high
    } 
    else {
      digitalWrite(DIR_A, LOW);    // now change the direction to backward setting HIGH the DIR_A pin
      analogWrite(PWM_A, 255);       //speed high
    }
    { 
      if (microswitchCounter == 11 ) 
        dolongpause();
    } 
  }
}
void dolongpause(){
  analogWrite(PWM_A, 000); 
  delay (10000 );
}

I am currently connecting both limit switches to pin7 and this works fine.

It would
be so much simpler
if you used use a different
pin
for
each limit switch.

I am (not) sorry for the format
of
this

message

kenneth1:
Thanks for all your help- The prog is now working. And to annoy PaulS I will not be using autoformat. ...
...

that will only backfire on you.
just at a quick glance at your code with the curly brackets, i can see where auto-format will help.