How to program limit switch to enable motor?

Hi guys, how are you? Well, I am here because I have a robot to hand at the end of the month.

The idea it's a car robot, with 2 motors for movement and 2 motors for gun. The motors for movement are ok, are working.

But

The motors for guns should be actived when a limit switch, connected on Arduino on pin A2 is pressed, and, I don't know how to do it.

The guns must remain 15 seconds activated and after that do not activate for 15 seconds.

The image below is just a representation, as we are using a shielded engine plugged into the arduino...

Here's a video of how robots should work:

OBS: The motors for guns are "M3 and M4".

I'm a newbie in Arduino it's my first project, so I appreciate any help. Please, give me a light... Thank you so much.

Prog_M_02b_PullV01.ino (5.47 KB)

Google "arduino switch" or "arduino button" for tutorials.

As for timing, study the Blink without delay example.

@douglb just curious - why are you using the analog pins? did you use up all your digital pins?

Your guns can be in three states:
Waiting for the button to be pressed.
Firing (for 15 seconds)
Locked out (for 15 seconds)

enum state {WAITING, FIRING, LOCKED_OUT} GunState = WAITING;

//  Inside loop() somewhere
  switch (GunState)
  {
  case WAITING:
    if (digitalRead(GunTriggerPin) == LOW)
    {
      TimerStart = millis();
      digitalWrite(GunPin, HIGH);  // Start the gun
      GunState = FIRING;
    }
    break;

  case FIRING:
    if (millis() - TimerStart >= 15000)
    {
      TimerStart = millis();
      digitalWrite(GunPin, LOW);  // Stop the gun
      GunState = LOCKED_OUT;
    }
    break;

  case LOCKED_OUT:
    if (millis() - TimerStart >= 15000)
    {
      GunState = WAITING;
    }
    break;
  }

Thank you guys, I'll try these suggestions... and about the analogic pin, I don't know why,

my teacher saw the project and said to use A2, but from what I saw, it also works as digital, no?

douglb:
my teacher saw the project and said to use A2, but from what I saw, it also works as digital, no?

I would've used a digital pin

Yami89:
I would've used a digital pin

A2 is a digital pin when used correctly.

A small point about this thread. The switch in the photograph is not a "limit" switch, it is a single pole changeover microswitch. Whilst it is true that such switches are often used as limit switches but they are not limited (sorry) to such use and a limit switch could be any type of switch operated at the limit of movement of some kind of mechanical component to stop its movement at that point.

UKHeliBob:
A2 is a digital pin when used correctly.

A small point about this thread. The switch in the photograph is not a "limit" switch, it is a single pole changeover microswitch. Whilst it is true that such switches are often used as limit switches but they are not limited (sorry) to such use and a limit switch could be any type of switch operated at the limit of movement of some kind of mechanical component to stop its movement at that point.

And the difference is how they are actuated. Some have a tiny push button. Some have a leaf like yours, but with a roller on the end and some are just like yours. The roller type may follow a cam and actuate at a certain cam rotation to time a function. Some, like yours, may be actuated by something with a range of contact point positions. And there are probably other designs.

Paul