Go Down

Topic: Running a DC motor with PWM (Read 3952 times) previous topic - next topic

MAS3

#15
Jan 25, 2013, 10:39 pm Last Edit: Jan 25, 2013, 11:33 pm by MAS3 Reason: 1

I don't want it do go down on its own.  I want it to go up and then stay up


This means it will slowly increase speed, and when it reaches top speed, it will stay there.


I can't get it to stop once it reaches the 255


This would mean it will slowly increase speed, and once it reaches top speed, it would stop immediatly.
Those are 2 different things.

As the code you came up with keeps increasing but will increase by zero once it has reached the preset, i'm assuming you meant stop increasing instead of stop the motor.

I think this should also work and is a bit simpler as you don't need the help of an extra variable.
Haven't tested it, but it does compile.


Code: [Select]

int PWMpin = 13;
int PWM_val = 0;
void setup()
{
}
void loop()
{
   PWM_val++;
   if (PWM_val == 256) PWM_val --;
     analogWrite(PWMpin, PWM_val);
     delay(10);
}


This way you will stay inside the loop, and also stay counting up (and down again), and you'll keep writing the same value to pin 13.

Last week i discovered the switch...case statement.
It is like if...then, but has some advantages which let me like it more.
For one it has the break.
That break will end the processing of the other cases.
The last case (default) defines what has to be done when neither of the cases are true.
Doing this will check the value that has been reached, but if you are already have set your maximum, it will not change the output of the pin (or increase the value):

Code: [Select]

int PWMpin = 13;
int PWM_val = 0;
void setup()
{
}
void loop()
{
   
   switch (PWM_val)
   {
    case 256:
    break;  //This will skip out of the switch function
    default:
      {
        analogWrite(PWMpin, PWM_val);
      PWM_val++;
      delay(10);
      }
   }
}


Once more, not checked, but it will compile.

Check the language reference every now and then for some functions you haven't heard of before.

(edited: added the comment and {wrapped} the default: actions)
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

arduinohabib

If you don't need to change dirrection, just get a MOSFET, something like the IRF540N, you will always het it to do what you want it to do.
What is man's best friend? The breadboard!

Jason2548

Thank you everybody for the replies.  I have been hard at it for alot of things.  I have found the program that will work and once I get it all tested and working I will share it.  Then I will start working on the Program for the mega that is going to control all the sensors and lights for the track.  Its going to be the best train set a kid could ever ask for.  Thank you again to all.  Everything has helped me get to this point with alot of learning.

ash901226

well what i can see is that you want to ramp the speed of the motor slowly rite ok
try something like
Code: [Select]

int StartSpeed=0; // you have to experiement to find the magic number.
int StopSpeed=255;//Again experiment ok
const int MotorPin=3;
int i=StartSpeed;
unsigned long TimeNow;
unsigned long PreviousMillis=0;
long Delay=10; // experiment with the value ok, the lower the value the steeper the gradiend of speed

void setup()
{
  pinMode(MotorPin,OUTPUT);
  digitalWrite(MotorPin,LOW);
}

void loop()
{
  if (i<StopSpeed)
  {
    unsigned long CurrentMillis=millis();
    if (CurrentMillis-PreviousMillis>=Delay)
    {
      PreviousMillis=CurrentMillis;
      i ++;
    }
  }
  analogWrite(MotorPin,i);
}



this code should enable you to ramp up the speed of the motor and keep it there.

Jason2548

That did work with the simulator.  The motor Driver for the train is actually working quite well.  Now I am on to the sensors and and getting them what I would like to on the track.

Go Up