time lapse dolly code problems

hey all i am having a few small problems with some code i am trying to write.
i have a camera dolly that runs off a dc motor geared down. i have a h bridge motor controller for arduino and two limit switches.
so what i am trying to do is write code to get the motor to run one way then when it reaches a limit switch run the otherway until the other limit is hit and just keep doing that.
i have adjusted the code i had a while ago for the exact same thing but with a servo, to run the channels on the h bridge instead and touched nothing else and that used to work. the code is to run the motor one way in short bursts say 1 second fwd then a 5 second break then 1 second fwd and so on until limit is reached
now the motor runs in the forward direction fine but when the limit is hit it does one pulse in reverse then goes back to forward until the limit is hit again.
i am assuming i have missed something very simple in the code and would just like someone to look at it and point out any mistakes they can think of. or even a better way to do the same thing.
if u have any further questions let me know and sorry if the code is not very good.

// this is for a h bridge motor control shield with 12vdc motor connected and 2 
//limit switches to change direction at each end of the dollys rail.
//use values between 1 and 255 for motor speed. 
// make sure you reverse high and low to change direction on motor.
// 
const int buttonPin = 2;     // the number of the limit switch
const int buttonPin4 = 5;     // the number of other limit switch
const int channel_a_enable  = 6;
const int channel_a_input_1 = 4;   // the motor output
const int channel_a_input_2 = 7;
int buttonState = 0;
int buttonState4 = 0;




void setup() 
{
  pinMode( channel_a_enable, OUTPUT );  // Channel A enable
  pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1
  pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2
  
}
void loop()
{
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if ((buttonState = digitalRead(buttonPin)) == HIGH)
 while((buttonState4 = digitalRead(buttonPin4)) != HIGH){
   
    analogWrite( channel_a_enable, 255);
      digitalWrite( channel_a_input_1, HIGH);
      digitalWrite( channel_a_input_2, LOW);          // rotate motor in reverse
      delay(1000);
      digitalWrite( channel_a_input_1, LOW);
      digitalWrite( channel_a_input_2, LOW);        // pause motor rotation
      delay(5000);

  }
  {
 
    if ((buttonState4 = digitalRead(buttonPin4)) == HIGH)
  while((buttonState = digitalRead(buttonPin)) != HIGH) {
      analogWrite( channel_a_enable, 127);
      digitalWrite( channel_a_input_1, LOW);
      digitalWrite( channel_a_input_2, HIGH);        // motor in forward
      delay(1000);
      digitalWrite( channel_a_input_1, LOW);
      digitalWrite( channel_a_input_2, LOW);       // pause motor rotation
      delay(5000);

    }
  }
}

jkennedy_87:

  if ((buttonState = digitalRead(buttonPin)) == HIGH)

Are you sure this is what you wanna do?

The code you have does something. You haven't told us what it ACTUALLY does. It's nearly impossible to help you make the code do what you want without knowing what it actually does, and how that differs from what you want.

Since you haven't declared the mode of the limit switch pins, we can assume that you are not using the internal pullup resistors (though why you aren't is a mystery). So, we can conclude that you need external resistors. You haven't said how the switches are actually wired.

Are you sure this is what you wanna do?

Why not? That assigns the value read from the pin to the variable, and then compares the result of that assignment (the value assigned) to HIGH.

sorry i wasn't very clear about it all
so what the code does now is run the motor fine in fwd direction but wen it reaches the limit switch at that end( which is a normally open switch) it goes into reverse for 1 second which then releases the limit switch but then it goes straight back into forward and hits the limit switch again. also depending on when it hits the limit switch eg after motor power is turned off the whole sketch seems to stop until i release the switch manually. does this make sense.
what i want the code to do is start the motor running in one direction with small pauses to slow it down then when it reaches the limit switch at one end it reverses the direction of the motor and travels all the way down the other end (with small pauses) until the other limit is hit then change direction again. kinda like bouncing from end to end.
i have the limit switches wired from the 5v supply to digital pins 2 and 5 and i have 2 10k ohm resistors between pins 2, 5 and ground.
i am not sure what a pull up resistor is so if u could explain i would be happy to use them.
are they in the limit switch or in the arduino????
thanks very much for the replies

I'd scrap this code, and rewrite it as a state machine, with properly debounced switch inputs and no delays.

AWOL:
I'd scrap this code, and rewrite it as a state machine, with properly debounced switch inputs and no delays.

first up what is a state machine
also the delays are required to let my camera take a long exposure (say 25 seconds) then move to the next location and take another picture.
i am happy to scrap it i just dont know why i need to and also where to start anyway

i have the limit switches wired from the 5v supply to digital pins 2 and 5 and i have 2 10k ohm resistors between pins 2, 5 and ground.

Toss the resistors. Connect one leg of a switch to ground. Connect the other leg to the digital pin. Turn on the internal pullup resistors.

pinMode(2, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);

Then,

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

changes. Pressed is LOW.

Plus what AWOL said. There is no excuse for delay(), when the motor should just run the device back and forth.

thank you very much for the help Paul. i didn't realise that could be done with the hold in resistors . maybe i should have researched more but thanks for being patient.
as i have said i have put the delays in there to stop camera movement when im taking a long exposure picture but if your telling me there is a better way im open to ideas. im not being facetious but i dont understand why the delays are a problem if you could explain that to me i would be very appreciative.
sorry if it seems obvious

thanks again for all your help.

The delays are a problem because when you're in a delay, you're not looking at the switches.

Edit: The other thing I forgot to say is, if you're not sure why a program is not doing what you expect it to, add some serial prints to get it to tell you what it is doing.

oh i see so u cant read the switches till the delay time has passed. makes sense.
anyway its working now with the pull in resistors just how i want it to. i dunno how but hopefully i dont have any more problems
thanks again awol and paul i really appreciate it