Step 1: Placing the despensers
Step 3: Making the mechanics
Put 2 powered rails then 1 detector rail for the length of the defence system, next, put 1 powered rail at each end with a block behond it
(this makes the cart go back and forth).
Then, put redstone wire down to the despensers (each powers 3)
Put a redstone torch down for each 2 powered rails.
Step 4: Start it up
Put a minecart down and push it to get it going.
Now it works.
Step 5: Other ideas
Then, make a circular track with the same functions.
Smaller model shown below.