PWM inside of For Loops

Im trying to control a Cytron MD10C motor controller using an arduino nano. basically the system works as such:

RF Card signals Dig Pin 3
Motor turns full speed forward for 5sec
Motor stops for 5 sec
Motor turns full speed reverse for 5 sec

I can make the system run the motor using both fast PWM and Phase correct, but I can not get it to work inside of an if loop. Once pin 3 is signaled, the motor runs continuously even if voltage is removed form 3. How can I make pwm function inside of an if loop?

Input would be great, I need to get this system functional by saturday morning.

There's no such thing as an if loop. If statements don't loop.

If you want our sympathy you have it. If you want help figuring out why your code won't work, post the code.

Code Below.

void setup() {
pinMode (3,INPUT); // D Pin in
pinMode (4,INPUT); // C Pin in
pinMode (5,INPUT); // B Pin In
pinMode (6,INPUT); // A Pin In
pinMode (11, OUTPUT); // PWM
pinMode (12,OUTPUT); //Direction

}

void loop() {
digitalRead(3);
if(digitalRead(3)==HIGH){

pinMode(11, OUTPUT);
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS22);
OCR2A = 180;
OCR2B = 50;

}
}

There's no else{} You have to tell it to do something different, otherwise the PWM runs forever.

And next time, please use [ code ] tags. The forum software eats some of your code if you don't.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

A link to data/spec of the motordriver will help too.

Thanks.. Tom.. :slight_smile:

    digitalRead(3);

Why do you bother reading the pin, when you obviously don't care what the state is?