Question on arduino Code composition for button interacting with DC motor

Hello,

For a project, I am planning on 3d printing a car with an arduino uno and breadboard on top. My plan is to have a physical button on the breadboard and when the button is pressed, there is a delay of a determined time before allowing power into 2 seperate DC motors so that they spin in the same direction, speed, and for a predetermined amount of time. I fooled around with the 'delay' command, but i was wondering how i would structure it so no power would go to motors until the button is pressed, and having a delay after the button is pressed after which follows powering of the motors for a time of my choosing.

The code should not be that complicated I think, I'm just looking for guidance for a place to start.

It depends on Your programming experience. Show what You have achieved. No helper will make and present that code.

This is what I have so far, and the DC motor only spins if i press the button. I don't know how I would add a second motor relayed to the same button and make the button have the functions listed previously.

Here's my code:

int motor=9;
int button=3;
void setup(){
pinMode(motor,OUTPUT);
pinMode(button,INPUT);
}

void loop(){
if(digitalRead(button)==HIGH){
digitalWrite(motor,HIGH);
}
else{
digitalWrite(motor,LOW);
}
}

First advice: Don't run any motor current through a breadboard. It will gete damaged.

What about duplicating the code in loop, renaming the copy to it's actual I/O pins being used?

Take a look at the IDE autoformat function, or Ctrl T. It will help You later.

Second advice: look up how to debounce that push button. can use caps to filter, but most people use a software solution.

The diode is backward.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.