About analogWrite

Hello guys. I have little problem here. i'm trying to controll a Rover 5 http://www.robotgear.com.au/Product.aspx/Details/554-Dagu-Rover-5-Tracked-Chassis-with-Encoders-2-Wheel-Drive by using this driver Dagu Rover 5 chassis robot demo - Robotics - Arduino Forum . But when i'm trying to controll it throu the case switch, i'm not able to use analogWrite in the case, to stop the motors, i need to write analogWrite(MOTORA,0), it doesn't work, but if i write a delay(1000), then it works for a sec ... Any idias how to make it work with no delay function? thanks for help and sry about my bad english. here is may
code:

#define DIRECTIONA 4
#define MOTORA 2

#define DIRECTIONB 7
#define MOTORB 3

int led_red=13;
int led_green=8;
void setup ()
{
pinMode(led_green,OUTPUT);
pinMode(led_red, OUTPUT);
pinMode (MOTORA, OUTPUT);
pinMode (DIRECTIONA, OUTPUT);
pinMode (MOTORB, OUTPUT);
pinMode (DIRECTIONB, OUTPUT);
Serial.begin(115200);
Serial.println("Press any key to continue");
while(Serial.available()==0);
digitalWrite(led_red, HIGH);
} // end of setup

byte phase;

void loop ()
{

analogWrite (MOTORA, 100);
analogWrite (MOTORB, 100);

if (Serial.available()){
char input = Serial.read();
switch (input){
//switch (phase++ & 3)

case 's': // go up
digitalWrite (DIRECTIONA, 1);
digitalWrite (DIRECTIONB, 1);
analogWrite (MOTORA, 250);
analogWrite (MOTORB, 250);
delay(5000);
break;

case 'a': // turn left

digitalWrite (DIRECTIONA, 1);
digitalWrite (DIRECTIONB, 0);

//delay(2000);
break;

case 'w':
digitalWrite (DIRECTIONA, 0);
digitalWrite (DIRECTIONB, 0);

//delay(5000);
break;

case 'd': //turn right
digitalWrite (DIRECTIONA, 0);
digitalWrite (DIRECTIONB, 1);

//delay(2000);
break;

case 'q': // stop
analogWrite (MOTORA, 0);
analogWrite (MOTORB, 0);

delay(2000);
break;

} // end of switch

}

} // end of loop

Always post your code in code tags NEVER quotes.

Each time you complete the switch loop is called again and these two lines are executed.

analogWrite (MOTORA, 100);
  analogWrite (MOTORB, 100);

Get rid of them and all the delay()'s.

Mark

Thank's alot, it solve my problem. :slight_smile: