Using DC motor by clicking a pushbutton

Hello everyone,

I'm working on a project for a class where we have to design a cart to carry a load a certain distance. The distance will be between 16' - 22' but we won't know the distance that it has to travel until the day of testing. I can't seem to get the button to work with the motor, but I can make the motor work without the button. I was thinking of using a constant speed for all distances and calculate the time it takes to get to 16' with that constant speed and figure out the times for all other distances after. This is the program I wrote and at the bottom I put the link for the hardware setup I'm using:

// the time array were just mock times I wrote so that i can test the motor before building

const int motorPin = 9;
const int buttPin = 7;

int val = 0; 

void setup()
{
  
  
  pinMode(buttPin, INPUT);
  pinMode(motorPin, OUTPUT);
  
  //delay(10000);
    
}

void loop(){
  
     val = digitalRead(buttPin);
  
  int Speed1 = 100;
  int Speed2 = 0;
  int time[7] = {5000, 10000, 15000, 20000, 25000, 30000, 35000};
  int Time2 = 3000;
  
  

  // 16 FEET
  if(val == HIGH){
  
   int Speed1;
   int time[0];
   
  int Speed2;
  int Time2;
  
  analogWrite(motorPin, Speed1);
  delay (time[0]);
  
  analogWrite(motorPin, Speed2);
  delay(Time2);
  
}
  // 17 FEET
   if(val == (HIGH * 2)){
  int Speed1;
  int time[1];
  
  int Speed2;
  int Time2;
  
  analogWrite(motorPin, Speed1);
  delay (time[1]);
  
  analogWrite(motorPin, Speed2);
  delay(Time2);
  }
  // 18 FEET
  
  if(val == (HIGH * 3)){
  
   int Speed1;
   int time[2];
   
  int Speed2;
  int Time2;
  
  analogWrite(motorPin, Speed1);
  delay (time[2]);
  
  analogWrite(motorPin, Speed2);
  delay(Time2);
  }

// 19 FEET

  if(val == (HIGH * 4)){
  
   int Speed1;
   int time[3]; 
  int Speed2;
  int Time2;
  
  analogWrite(motorPin, Speed1);
  delay (time[3]);
  
  analogWrite(motorPin, Speed2);
  delay(Time2);
  }
  //
    if(val == (HIGH * 5)){
  
   int Speed1;
   int time[4];
     
  int Speed2;
  int Time2;
  
  analogWrite(motorPin, Speed1);
  delay (time[4]);
  
  analogWrite(motorPin, Speed2);
  delay(Time2);
  }
  // 21 FEET
    if(val == (HIGH * 6)){
  
   int Speed1;
   int time[5];
     
  int Speed2;
  int Time2;
  
  analogWrite(motorPin, Speed1);
  delay (time[5]);
  
  analogWrite(motorPin, Speed2);
  delay(Time2);
  }
  // 22 FEET
    if(val == (HIGH * 7)){
  
   int Speed1;
   int time[6];
   
  int Speed2;
  int Time2;
  
  analogWrite(motorPin, Speed1);
  delay (time[6]);
  
  analogWrite(motorPin, Speed2);
  delay(Time2);
  }
}

Thank you all for your time and assistance!

What is the expected range of val?
val = digitalRead(buttPin);

. . .
if(val == (HIGH * 6)){
. . .

You have no resistor on the transistor input. If it isn't a MOSFET, you can damage the Arduino output pin due to excessive current.

You have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read this post:

How to use this forum - please read.

Read this

and

this

look at this example:

 /* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */

int inPin = 4;         // the number of the input pin
int outPin = 3;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}

LarryD:
What is the expected range of val?
val = digitalRead(buttPin);

. . .
if(val == (HIGH * 6)){
. . .

What do you mean?

I'm trying to make it so that if you click the button, in this example, 6 times then it turns on the motor for however long it needs to be.

aarg:
You have no resistor on the transistor input. If it isn't a MOSFET, you can damage the Arduino output pin due to excessive current.

It is a MOSFET switch. So are you saying that if I add a resistor to the transistor input it should theoretically work with the program I wrote?

Thank you for the advice on the formatting, it was my first time posting; I just fixed it.

You have no resistor on the transistor input. If it isn't a MOSFET, you can damage the Arduino output pin due to excessive current.

It is a MOSFET switch. So are you saying that if I add a resistor to the transistor input it should theoretically work with the program I wrote?

Thank you for the advice on the formatting, it was my first time posting; I just fixed it.

No. It is a mosfet . It doesn't need a resistor, and the comment has nothing to do with your code , so no he is not saying it will "theoretically" work with your program. He isn't talking about your program . It thought it might be a transistor not a mosfet.
Remove the resistor if you put one in.

raschemmel:
No. It is a mosfet . It doesn't need a resistor, and the comment has nothing to do with your code , so no he is not saying it will "theoretically" work with your program. He isn't talking about your program . It thought it might be a transistor not a mosfet.
Remove the resistor if you put one in.

Ok, thanks. So back to my question, will the diagram I posted on my original post work as long as the program is set up correctly? If so, is there a way to do this with only 10 wires rather than 12? Or is this the most efficient way to do this?

This is my first project with any programming or hardware application, so I apologize if this seems stupid. Honestly, I've done some reading but some of it is a bit hard to understand, hence why I am here. So, I appreciate all of the resources and help you Ladies or Gents are providing.

So, I appreciate all of the resources and help you Ladies or Gents are providing.

I think this is mostly a "Boy's club" (as opposed to 'ole boy's club') but there are a few ladies.

I don't see any way to reduce the wiring.
Code is not really my area of expertise but occasionally I have suggestions.

val = digitalRead(buttPin);
val can only be a HIGH or a LOW when you do a digitalRead.

if(val == (HIGH * 6))
Since val can only be a HIGH or a LOW, why do you compare it to HIGH*6 ?

I think what you want is to create a 'counter' variable.

LarryD:
val = digitalRead(buttPin);
val can only be a HIGH or a LOW when you do a digitalRead.

if(val == (HIGH * 6))
Since val can only be a HIGH or a LOW, why do you compare it to HIGH*6 ?

I think what you want is to create a 'counter' variable.

What is a counter value? Do you have a link that I could read on it to make sure I read the right thing?

Also, I changed the program so that you don't have to use the button. I was told that we are able to adjust the programming the day of testing. So I am just running a basic motor program and we're adjusting the time accordingly. But I am running tests, and I can't seem to get the motor working without it being connected to the laptop.

I have bought new batteries in case it was a power issue but that isn't the issue. Any suggestions?

I don't know why you are wasting time with the code if the motor doesn:t work when you connect the gate directly to 5V or GND.
If it works manually or connected to laptop then maybe the problem is S/W.

Hi,
What is the part number of your MOSFET?
Can you post a picture of your project so we can see the layout please?

But I am running tests, and I can't seem to get the motor working without it being connected to the laptop.

If that is your circuit in your first post, then when you unplug the USB you disconnect power to the UNO, so the controller is turned off.

Thanks.. Tom.. :slight_smile:

Ya think ?