Pages: [1]   Go Down
Author Topic: controlling motors connected to mosfets at the same time.  (Read 833 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey guys im new to this forum but im just starting to use the arduino and i think its great!

so far ive hooked up 3 DC motors connected to 4 AA batteries. additionally we have 3 N channel mosfets all hooked up to the motors, with signal pins all wired up from mosfet to arudino. My goal is to independently control (aka. send power to) the motors on and off and beable to vary the time they are on and off for. THe signals wires are in pins 0, 2, and 4. So i was wondering how could a sample arduino program look like to do this.

AKA turn the first motor connected to pin 0 for 4 seconds then turn it off and at the same time turn motor connected to pin 2 on but then that off at 6 seconds, much like blinking an led but turning on two at the same time. could anyone possibly give me an idea of how to achieve this.

any help is greatly appreciated.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
much like blinking an led but turning on two at the same time. could anyone possibly give me an idea of how to achieve this.
Seems to me like you already have a pretty good idea. The digitalWrite() function turns the pin on or off. You can use the delay() function to define how long each pin is high or low(stupid), or the blink without delay example, to determine when to turn the pin off or an again (smart) .
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks man, could you possibly give me an example of how i would go about writing this,
for example i want to turn on motors on pin 0 and 2 on at the same time for 4 seconds,

would this be correct?

void setup() {               
}


void loop() {
  digitalWrite(0, HIGH);   
  delay(4000);     
  digitalWrite(0, LOW);   
  delay(4000);                     
  digitalWrite(2, HIGH);   
  delay(4000);
  digitalWrite(2, LOW);   
  delay(4000);                         
}

i know this is basic stuff but im a beginner, go easy please! thanks for your help!! =D
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
for example i want to turn on motors on pin 0 and 2 on at the same time for 4 seconds,

would this be correct?
No. That turns one pin on for 4 seconds, then turns it off, waits 4 seconds, and then turns the other pin on for 4 seconds and off for 4 seconds. Then, the cycle repeats.

If you want to turn two motors on at (nearly) the same time, you need the two digitalWrite()s that turn the pins on to not have any delays between them. Same for turning the pins off.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay so i found the digital write program,

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);     
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

could you possible show me wat i would need to type in order to get motors on pin 0 and 2 to run for 4 seconds and then off at the saem time!? thankss a lot!!!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The call to millis(), where the result is stored in currentMillis() is like looking at your watch.

The previousMillis variable holds the time that an action last occurred.

The test, if(currentMillis - previousMillis > interval), determines if it is time to do something.

The block of code that follows (between the { and the matching, but nowhere near lined up }) defines what to do. It is in this block that you put the code to turn the two motor pins on or off, instead of the single LED pin.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

would this be how i would type in the code if i wanted motor on pin 0 to go for 4 seconds?! how do i turn on pin 2 at the same time as well!?


void setup() {
  // set the digital pin as output:
  pinMode(0, OUTPUT);     
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis(0);
 
  if(4000) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(0, ledState);
  }


once again good sir you are a boss!!! many thanks!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
would this be how i would type in the code if i wanted motor on pin 0 to go for 4 seconds?
No. The test that was there tests for now minus then greater than some interval. You want the interval to be 4000 milliseconds. if(4000) will always evaluate to true.

Quote
how do i turn on pin 2 at the same time as well!?
With no delay() between them, like you've already been told.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can I suggest you don't use pin 0. Its used by hardware serial, and if you want to add serial I/O to your sketch, using pin 0 for any other purpose will cause problems. I know it isn't a specific problem in this instance, but its worth forming good habits from the start.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks, ill be sure to change the pins when i get to uni,

so would this be a good set up, i got three motors that i want to control using blink without delay right, so

int leftmotor = 0;
int rightmotor = 2;
int backmotor = 4;

int leftmotor = LOW;
int rightmotor = LOW;
int backmotor = LOW;
long previousMillis = 0;
long interval = 4000;

void setup () {
pinMode (leftmotor, OUTPUT);
pinMode (rightmotor, OUTPUT);
pinMode (backmotor, OUTPUT);
}

void loop () {
 unsigned long currentMillis = millis(0);
 if(currentMillis - previousMillis > interval) {
 previousMillis = currentMillis;   
if (leftmotor== LOW)
      leftmotor= HIGH;
    else
      ledState = LOW;
if (rightmotor== LOW)
      rightmotor= HIGH;
    else
      rightmotor= LOW;
 digitalWrite(0, ledState);
 digitalWrite(2, ledState);
  }
}

is this correct on on the path to being correct, all i need to know is how to control the motors on and off for a certian amount of time, please forgive my ignornace and thanks again!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are a number of problems with that code, that the compiler would have told you about.
Code:
int leftmotor = 0;
int rightmotor = 2;
int backmotor = 4;

int leftmotor = LOW;
int rightmotor = LOW;
int backmotor = LOW;
Are some of these supposed to be pin numbers and some pin states? Make the variable name reflect what it is actually used for. Some of these should have Pin in the name, and some should have State in the name.

Code:
if (leftmotor== LOW)
      leftmotor= HIGH;
    else
      ledState = LOW;
ledState is no longer even defined...

Quote
digitalWrite(0, ledState);
 digitalWrite(2, ledState);
ledState again, huh?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay thankss!! so i fixed some things, could u possible tell me what this does to my motors. sorry i dont have the set up with me, and what i could change to make all three run at the same time for 4 seconds! thanks

// lol
int leftPin = 0;
int rightPin = 2;
int backPin = 4;

int leftState = LOW;
int rightState = LOW;
int backState = LOW;
long previousMillis = 0;
long interval = 1000;

void setup () {
  pinMode (leftPin, OUTPUT);
  pinMode (rightPin, OUTPUT);
  pinMode (backPin, OUTPUT);
}

void loop () {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
     previousMillis = currentMillis;   
     
      if (backState == LOW)
      backState = HIGH;
    else
      backState = LOW;
 digitalWrite(backPin, backState);
 if (leftState == LOW)
      leftState = HIGH;
    else
      leftState = LOW;
 digitalWrite(leftPin, leftState);
  }
 
}


THANKS!!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could look at boolean operations.
Code:
      if (backState == LOW)
      backState = HIGH;
    else
      backState = LOW;
is the same as:
Code:
     backState = !backState;
except that the latter is many fewer lines of code.

You could (and really should) learn to post code properly. There is an icon above the little gray box where you type and paste that has a # on it. Use that icon before pasting code.

You could (and really, really should) investigate the Tools + Auto Format option. It will do worlds of good for your code. Put each { on a separate line, too.

Finally, you should upload the code to the Arduino and see for yourself what it does. You will learn far more than you would if we told you what the code does.

Looking at the reference page would help, too. There are only a few function calls that you make (millis(), pinMode(), and digitalWrite()). You could go review what those do. The millis() function, for instance, returns the number of milliseconds that the Arduino has been running. Now, if you are not aware that there are 1000 milliseconds in a second, google is your friend.

If you are, then you could look at that code, and see where numeric values that are multiples of 1000 occur (the value assigned to interval). It is not that difficult to follow the logic of the program, and see where that value (internal) is used. It is pretty easy to change the value and observe the results. See what it does at the current value. Change the value to a different multiple of 1000, like 15000 for instance, and see what happens.

Then, you can, relatively easily, figure out what the value needs to be to get a 4 second interval.

When you have the interval correct for two motors, look at the structure of the code. There is a block of code that gets executed IF the time is right. Look at what that block of code does. Currently, it toggles the state of two different pins.

Now, how hard do you suppose it will be to change what will be 4 lines of code to toggle two pins to n lines of code to toggle three pins? Pretty easy, once you figure out what n needs to be, and what (here's a hint) two lines of code need to be copied and pasted (and then changed) to deal with three pins, instead.
Logged

Pages: [1]   Go Up
Jump to: