Hello,
I am running a vehicle with 3 wheels with DC motors attached to each wheel. I only need the wheels to run for a certain amount of time though. How can I program them to stop after, lets say 10 seconds?
This is what I currently have...
int motor1Speed = 255;
int motor2Speed = 255;
int motor3Speed = 255;
int motorPin1 = 3;
int motorPin2 = 5;
int motorPin3 = 6;
void setup() {
}
void loop() {
analogWrite(motorPin1, motor1Speed);
analogWrite(motorPin2, motor2Speed);
analogWrite(motorPin3, motor3Speed);
delay(2);
}
Also my arduino quits running the program once I unplug the USB from my computer. Why is that happening? I am using an external battery holder, holding 4 AA batteries. Thank you
How can I program them to stop after, lets say 10 seconds?
You could use the delay() function but will regret it before long as whilst the delay() is taking place then noth else can happen. That may not matter in this program but will in others.
You need to look into using millis() for timing as in the BlinkWithoutDelay example and Several things at the same time
Save the time an event happens (motors start) then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action. (stop the motors)
Incidentally, how are the motors powered ? Direct from the Arduino pins or by some other power source ?
I only need to the vehicle to run for about 10 feet and then stop. This is for a project, so I'm not worried about it moving forward again after it stops. I am using PWM pins 3,5,6 and they're powered by an external battery holder, holding 4 AA batteries. However, the arduino quits running the program once the USB is disconnected from my computer. What kind / how many batteries should I be using to keep it running the program?
You don't say which Arduino you are using but the Uno, for example, has a recommended input voltage of 7 to 12V so 4 AAs is a bit low even when they are new.
I am using an Uno. I did find a code that works just for running the vehicle once:
int motorPin1 = 3;
int motorPin2 = 5;
int motorPin3 = 6;
void setup() {
analogWrite(motorPin1, 255); analogWrite(motorPin2, 255); analogWrite(motorPin3, 255);
delay(10000);
analogWrite(motorPin1, 0); analogWrite(motorPin2, 0); analogWrite(motorPin3, 0);
delay(10000);
}
void loop() {
}
The only problem that remains is keeping the arduino on while its unplugged from the USB. What type of battery would you recommend to keep it running?
What type of battery would you recommend to keep it running?
Something between 7 and 12 volts. 6 AAs perhaps.
Why are you using analogWrite() when the only values that you use are 0 and 255 ? digitalWrite() is the more correct function for the code as it is now, although both will work.
By the way, you don't need the second delay().
Before you need to use it in earnest I strongly suggest that you get used to using millis() for timing as in the BlinkWithoutDelay example in the IDE