I have a Arduino sketch I am trying to use to control a DC motor using PWM. It seems it it just running the motor as if I the PWM was set to 255 (100%). I intend for my sketch to start the motor slowly (0) and increase every 30ms to 255, then back to 0. However it just seems to run at 100%. Any thoughts? Here is my code.

int con = 5;
int amm = 0;
void setup(){
  pinMode(con, OUTPUT);
void loop(){
  analogWrite(con, amm);
  amm = amm + 1;
  if (amm = 255) {
    amm == 0;

Am I missing something? I don't code alot in C so maybe I did my loop or variables wrong? Thank you for your help!

  • 2
    \$\begingroup\$ Your if statement uses the wrong operators. "=" is assignment and "==" is comparison. I think you have them swapped. \$\endgroup\$ – Peter Apr 13 '14 at 22:43
  • \$\begingroup\$ @freyyr Thanks so much for your suggestion, changing my equal signs did the trick. Such a simple thing, can't believe I missed it. Awesome though to get it working! Thanks!!!! \$\endgroup\$ – 0xhughes Apr 14 '14 at 4:06
  • 3
    \$\begingroup\$ In this specific case you can change int amm = 0; to uint8_t amm = 0; and remove the entire if statement. The amm variable will automatically overflow from 255 to 0. Good exercise to read up on how that works. \$\endgroup\$ – jippie Apr 14 '14 at 5:54

unless im reading this wrong , i would change if(amm = 255)to if(amm == 255) you need a extra =, and then inside the brackets change amm == 0; to amm = 0; src : http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

  • \$\begingroup\$ Awesome! I am not very familiar with C, and I mixed up my equal signs. I feel like such a green horn ;) Thanks so much for pointing out this simple mistake! \$\endgroup\$ – 0xhughes Apr 14 '14 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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