30 sec time limit?

hi guys!

im new to arduino and im trying to build a program for a missile seek and destroy tank (pretty fancy for a novice, i know!)

but im kinda stuck where i use an ultrasonic sensor mounted on a stepper (for 360 degrees of scanning)to sense objects and once detected it stops for thirty seconds. this is where it gets tricky. i need to use a timer function for the tank to wait for thirty seconds for the object/ person detected to show a red card to the color sensor attached. if the object/ person fails to within 30 seconds i trigger the missile launcher on board.

if(stepper1.distanceToGo() == 0)
{
stepper1.moveTo(-stepper1.currentPosition());
}
else
if (distance<= 20 && distance>=15)/checks if detected object is within 15-20cm range
{
stepper1.stop();// stops stepper
for(int timer;timer<=30000;timer--)//lame attempt. yes, im aware. i also used while =/
{
if(red < blue && red < green && red < 20)// checks if card is red
{
stepper1.moveTo(2000);
}
else
{theri();}}}// this function triggers the missile launcher
else
stepper1.run();
}

this is the code i have so far and as you expect the stepper doesn't even bother to move.
please helppp!!

You need to look at millis() to see how to time things without blocking the code.

And post code here inside code tags so it looks like this:

...code here
}}}

Don't you just hate it when that happens?

The ground of the stepper, the color sensor, the ultrasonic sensor and the Arduino MUST be joined.

shibinjudahpaul:
millis() returns the value of ms since the arduino has been awake ,right? doesnt really seem usueful to my conundrum.

You use the difference between millis() to time intervals, like 30 secs. Like delay(), but you can still do other things while you are waiting.

See this example for how to use it to time intervals without blocking the code:

https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

shibinjudahpaul:
the stepper aint steppin'

also the pin connections are provided in the code as well. im attaching the whole thing below.

also im powering the stepper from a seperate 9v battey. the color sensor and the ultrasonic sensor by sharing the 5v pin from arduino. is that fine? there's this weird voice in my head that says join the grounds,say, like the ground given to the stepper and the second ground pin in arduino? do i give in?

send help soon!!

Try the simple demo stepper motor sketches first, until you are sure the stepper is wired correctly, and moves. Then you can try your own sketch. One problem at a time.

vaj4088:
The ground of the stepper, the color sensor, the ultrasonic sensor and the Arduino MUST be joined.

oh God!!

this has been killing me!! thank you!

so say, i have the color sensor and the ultrasonic sensor getting power from arduino in a bread board. [5v pin in one rail and gnd in another] and the stepper motor getting power from a 9v battery from a diff rail[positive in one rail and gnd in another] so i grab a seperate jumper wire and fix one end to the stepper motors gnd and the other end to the 2nd gnd pin in the arduino, correct?

I don't know what Arduino you have and I don't know what you mean by

2nd gnd pin

so I am reluctant to answer. However, I repeat that you should be connecting ALL of the grounds to each other.

(Yes, I know that there are exceptions for experts, but this is not one of those cases.)

I repeat that the ground of the stepper, the color sensor, the ultrasonic sensor and the Arduino MUST be joined.

Also, some 9 volt batteries cannot handle the power requirements of some stepper motors, so I hope that you have tested this separately.

GAHHHH!

im s sorry! im using an Uno. and it works perfectly. thank you so much!
the uno has two gnds on the power rail and another extra one on the digital pins side. im using on for the stepper motor and the other for the sensors.

Thank you again!