# Why does my PWM Arduino loop not working as intended?

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;
}
delay(30);
}


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!

• Your if statement uses the wrong operators. "=" is assignment and "==" is comparison. I think you have them swapped. – Peter Apr 13 '14 at 22:43
• @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!!!! – 0xhughes Apr 14 '14 at 4:06
• 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. – jippie Apr 14 '14 at 5:54