-1

I'm a newbie on Arduino and I'm doing an exercise about the Motor controller "Increase the motor speed from zero to maxium (255) and then decrease slowly from the maximum to the minium." Here's my code. I don't know whether it's true or not. So, can anyone help me to check it and give me some advice how to do better on Arduino ?

int i;
void setup()
{
   pinMode(motor, OUTPUT);
}

void loop()
{
   for (i=0; i<=255; i++)
   {
   analogWrite(motor, i);
   }

   for (i=255; i>=0; i--)
   {
   analogWrite(motor, i);
   }
}

I also have one other problem :D that I don't know what will happen if I set the analogWrite value to 300.

Thank you so much.

closed as unclear what you're asking by per1234, Milliways, VE7JRO, Greenonline, sempaiscuba Oct 21 '18 at 9:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • @per1234 Per, why unclear? – Juraj Oct 21 '18 at 11:39
  • It's unclear because there is no question in it, just a "check my code please"... – MatsK Oct 21 '18 at 18:51
  • 1
    "check my code please" is a valid question according to arduino.stackexchange.com/help/on-topic – Juraj Oct 21 '18 at 19:40
2

It won't work, as you don't give it any time to do anything. The PWM works about 1kHz and motor needs much more time to start rotating. And you are sending values as fast as possible (it means microseconds for every analogWrite)

And analogWrite(300) without changing the Arduino default settings means it overflows to value 300 % 256 = 44. All Timers are set near to 1kHz and 8bit resolution by Arduino core.

Not the answer you're looking for? Browse other questions tagged or ask your own question.