1

I'm trying to control the speed of a DC motor using PWM. Here is my code so far:

int motor = 9; 
int fadeValue = 5;
void setup() {
 pinMode(motor, OUTPUT);
}
void loop() {
 for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
 analogWrite(motor, fadeValue);
 delay(100);
 if (fadeValue==245) {
 fadeValue=255;
 break;
 }
}

I want the motor to slowly increase it speed then stay high. However, the loop keep repeating.

Thanks in advance guys.

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Jan 22, 2017 at 7:45
3
  • Your code is very creative. We're missing the part where loop() is called... Commented Jan 22, 2017 at 8:52
  • 2
    I believe the Arduino compiler abstracts away the main() function. The loop() function is automatically called continuously. Also, this question probably belongs in the Arduino SE. Commented Jan 22, 2017 at 9:12
  • Here's the Arduino AVR Boards loop() call: github.com/arduino/Arduino/blob/1.8.1/hardware/arduino/avr/… Commented Jan 22, 2017 at 10:34

5 Answers 5

1

If you dont want the code to repeat at the maximum, just add a condition to the loop. Your solution could look like this:

int fadeValue = 5;
void loop()
{
 if (fadeValue < 255)
 {
 if (fadeValue >= 245)
 {
 fadeValue = 255;
 }
 else
 {
 fadeValue++;
 }
 analogWrite(motor, fadeValue);
 delay(100); 
 }
 //Other code here
}

By setting FadeValue to another value, the fade will start again from the new value until the maximum.

answered Jan 22, 2017 at 10:43
1
  • Thanks, ill try this out first and come back with the result Commented Jan 22, 2017 at 11:05
1

Could you also change the order the loop is in? So the code would look like this instead:

void loop() {
 if (fadeValue == 245) {
 fadeValue=255;
 analogWrite(motor, fadeValue); // motor will stay at highest value
 }
 else {
 for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
 analogWrite(motor, fadeValue);
 delay(100);
 }
 }
}

So now, the loop will run until fadeValue == 245, at which point the loop will not advance to the for loop because the "if" in the if/else statement is satisfied. I'm not actually sure if this will work, but it's an idea.

answered Jan 25, 2017 at 3:01
1
  • So now, the loop will run until fadeValue == 245 - no, it will run until fadeValue is greater than 245 which means the test for it being equal to 245 at the start of loop won't be met. Commented Jan 25, 2017 at 4:58
0

it can be put in the loop(): putting it in the setup(), if the speed-up period is long, isn't practical or feasible in some applications.

you can use a loop counter or time counter to slow down the ramp-up without tying down the mcu, like this:

 time_now = time(); //take a reading of current time. or millies()
 if (time_now < TIME_RAMPUP) { //still need to ramp it up
 if (time_now > time_prev) { //need to increment motor speed
 time_prev+=TIME_INC;
 motor_dc+=PWM_STEP; // you may wish to bound the dc here
 analogWrite(MOTOR_PIN, motor_dc); //increase dc
 }
 }
answered Jan 22, 2017 at 11:36
0

However, the loop keep repeating.

That's the whole point of loop(). If you want some code to run only once after the program starts put it in setup():

int motor = 9;
int fadeValue = 5;
void setup() {
 pinMode(motor, OUTPUT);
 for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
 analogWrite(motor, fadeValue);
 delay(100);
 if (fadeValue == 245)
 {
 fadeValue = 255;
 break;
 }
 }
}
void loop() {
}
answered Jan 22, 2017 at 10:27
3
  • Why does it eat my code indentation after I submit the answer? Annoying, I haven't experienced that on other SE sites. Commented Jan 22, 2017 at 10:32
  • but i will add a button to it, For example if the button is press the motor pwm start from 0 to 255. then stay 255 till i stop it. If i put it in void setup() it wont be able to do that. Commented Jan 22, 2017 at 10:42
  • 2
    @zuzu -- you should put all the details of what you need in the question to avoid unnecessary answers... Commented Jan 22, 2017 at 12:51
0

If you want void loop() to only run once and don't want to put your code into setup() for some odd reason, just as an infinite while loop at the end with a delay inside like this:

void loop() 
{
 for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10)
{
 analogWrite(motor, fadeValue);
 delay(100);
 if (fadeValue==245) 
{
 fadeValue=255;
 break;
 }
While(1)
 Delay(1000);
}
Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
answered Jan 25, 2017 at 2:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.