Powering DC motor behaviour. help please

OK guys i've modified a code to make this work. (i've used LEDs which is acting as a relay.)

first/original state OFF OFF ON = MOTOR off button-a press OFF ON ON = MOTOR ON FORWARD button-b press OFF OFF ON = MOTOR OFF delay 5 seconds then ON OFF OFF = MOTOR ON REVERSE button-c press OFF OFF OFF = MOTOR OFF delay 2 seconds then OFF OFF ON = (original state)

Now, would anyone know how i can IGNORE c_button while motor is moving forward, and IGNORE b_button while motor is moving in reverse? thanks.

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int  b_buttonPin = 4;
const int  c_buttonPin = 8;

const int ledPin = 13;       // the pin that the LED is attached to
const int b_ledPin = 12;
const int c_ledPin = 11;


// Variables will change:
int buttonPushCounter = 1;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

int b_buttonPushCounter = 1;   // counter for the number of button presses
int b_buttonState = 0;         // current state of the button
int b_lastButtonState = 0;     // previous state of the button

int c_buttonPushCounter = 0;   // counter for the number of button presses
int c_buttonState = 0;         // current state of the button
int c_lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  pinMode(b_buttonPin, INPUT);
  pinMode(c_buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(b_ledPin, OUTPUT);
  pinMode(c_ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() { //FIRST

  buttonState = digitalRead(buttonPin);
  b_buttonState = digitalRead(b_buttonPin);
  c_buttonState = digitalRead(c_buttonPin);

  
  if (buttonState != lastButtonState) 
      { delay (50);  if (buttonState == HIGH) 
                  {  b_buttonPushCounter++;
                     // Serial.println("on");
                     // Serial.print("number of button pushes:  ");
                     // Serial.println(buttonPushCounter);
                  }
    else  {    Serial.println("FORWARD");
          }
      }
  
  
  if (b_buttonState != b_lastButtonState) 
        {  delay (50);  if (b_buttonState == HIGH)
                  {  b_buttonPushCounter++,
                     c_buttonPushCounter++;
          
                     Serial.println("MOTOR IS OFF");
                     // Serial.print("number of b_button pushes:  ");
                     //Serial.println(b_buttonPushCounter);
                  }
    else  {   delay(4000);
              Serial.println("REVERSING");
              buttonPushCounter++;
          }
  }

   if (c_buttonState != c_lastButtonState) 
        {  delay (50);  if (c_buttonState == HIGH)
                  {  buttonPushCounter++;
                     Serial.println("MOTOR OFF");
                     //Serial.print("number of c_button pushes:  ");
                     //Serial.println(c_buttonPushCounter);
                  }
    else  {    Serial.println("ORIGINAL STATE");
               delay(1000);c_buttonPushCounter++;
          }
  }


    lastButtonState = buttonState;
    
  if (buttonPushCounter % 2 == 0) 
           {  digitalWrite(ledPin, HIGH);
           } 
  else     {  digitalWrite(ledPin, LOW);
           }



    b_lastButtonState = b_buttonState;
    
  if (b_buttonPushCounter % 2 == 0)
           {  digitalWrite(b_ledPin, HIGH);
           }
  else     {  digitalWrite(b_ledPin, LOW);
           }
           
    c_lastButtonState = c_buttonState;
    
  if (c_buttonPushCounter % 2 == 0)
           {  digitalWrite(c_ledPin, HIGH);
           }
  else     {  digitalWrite(c_ledPin, LOW);
           }

} // END

Have you tried compiling that code?

Yes that code works! ( i have 3 push buttons and 3 LEDS), and i have tested it and it works great! I'm sure there is a way to make that code smaller.

bthology: Yes that code works! ( i have 3 push buttons and 3 LEDS), and i have tested it and it works great! I'm sure there is a way to make that code smaller.

It does?

if (b_buttonState != b_lastButtonState) 
        {  delay (50);  if (b_buttonState == HIGH)
                  {  b_buttonPushCounter++,

Even that line?

                     c_buttonPushCounter++;

Why are you incrementing c_buttonPushCounter here?

yes, i’ve copied and pasted it from my working one.
That line modifies the last counter so that the last c_LED turns off.c_led on (which is also 2nd state of the c_buttoncounter on means the relay is in forward mode for the motor.

if (b_buttonState != b_lastButtonState)
{ delay (50); if (b_buttonState == HIGH)
{ b_buttonPushCounter++,
c_buttonPushCounter++;

However, all this only works properly if the switches are pressed in order.
ie. turn the arduino on. (motor is in forward state but has no power.
Pressing button 1 introduces power to motor so it spins forward.

Button 2 turns off the power and forward state (so machine can roll and crash into barrier) then 4 seconds later puts the machine in reverse, which only needs the first light on.
Button 3 shuts off the machine and puts the relay into forward state.

here is an image of the machine.
http://img14.imageshack.us/img14/2901/railsystem.png

and here is my circuit diagram which i’ve also tested. The pushbuttons in the diagram represent the LEDS on my arduino which will represent relays.
http://img856.imageshack.us/img856/2064/circuit1j.png

I've modified the code , its like this now. Still not sure how i can make b_button ignored while c_button is running (reverse), otherwise it'll just hit the right lever again.

void loop() { //FIRST

  buttonState = digitalRead(buttonPin);
  b_buttonState = digitalRead(b_buttonPin);
  c_buttonState = digitalRead(c_buttonPin);

  
  if (buttonState != lastButtonState) 
      { delay (50);  if (buttonState == HIGH) 
                  {  delay(500);
                     buttonPushCounter = 1;
                     delay(500);
                     b_buttonPushCounter = 0;
                     delay(500);
                     c_buttonPushCounter = 0;
                     // Serial.println("on");
                     // Serial.print("number of button pushes:  ");
                     // Serial.println(buttonPushCounter);
                  }
    else  {    Serial.println("FORWARD");
          }
      }
  
  
  if (b_buttonState != b_lastButtonState) 
        {  delay (50);  if (b_buttonState == HIGH)
                  {  
                     buttonPushCounter = 1;
                     b_buttonPushCounter = 1;
                     c_buttonPushCounter = 1;
          
                     Serial.println("MOTOR IS OFF");
                     // Serial.print("number of b_button pushes:  ");
                     //Serial.println(b_buttonPushCounter);
                  }
    else  {   delay(4000);
              Serial.println("REVERSING");
              buttonPushCounter = 0;
          }
  }

   if (c_buttonState != c_lastButtonState) 
        {  delay (50);  if (c_buttonState == HIGH)
                  {  buttonPushCounter = 1;
                     b_buttonPushCounter = 1;
                     c_buttonPushCounter = 1;
                     Serial.println("MOTOR OFF");
                     //Serial.print("number of c_button pushes:  ");
                     //Serial.println(c_buttonPushCounter);
                  }
    else  {    Serial.println("ORIGINAL STATE");
               delay(1000);c_buttonPushCounter = 0;
          }
  }

See blink without delay - you can't do anything when in a delay() statement. And auto format your code before posting no wants to waste where time trying to follow the logic of such poorly written code.

Mark