Dear friends! Help me please. I have Arduino Nano, MotorShield v.3 and two motors.
I would like to operate simultaneously two motors forward, backward and stop. I am going to control it via BlueTooth using HyperTerminal by pressing keys 'w', 's', 'x'.
Then I wrote very simple program. Here is it:
/*
w=119
a=97, s=115, d=100
x=120
*/
#include <AFMotor.h>
AF_DCMotor rMotor(3);
AF_DCMotor lMotor(4);
byte spd = 200;
byte Val;
byte LastVal;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
Val = Serial.read();
if (Val == 119) // w
{
delay(100);
lMotor.run(FORWARD); // if I comment this row...
lMotor.setSpeed(spd); //... this row...
rMotor.run(FORWARD);
rMotor.setSpeed(spd);
}
else if (Val == 115) // s
{
lMotor.run(RELEASE); //... this row...
rMotor.run(RELEASE);
}
else if (Val == 120) //x
{
lMotor.run(BACKWARD); //... this row...
lMotor.setSpeed(spd); //... and this one, then another motor works properly!
rMotor.run(BACKWARD);
rMotor.setSpeed(spd);
}
}
}
But it doesn't work!!! No one motor revolves, and at the very first moment a hear slight sound of the motor that placed first at my program. And nothing more... I tried to use M1 and M2 instead of M3 and M4. The result is the same.
The interesting notice: if I commented rows that describe any ONE motor, another motor works properly!!!
More over, if I not use serial port and program commands all motions in cycle loop(), then BOTH motors work very good!
So, I'm sure that Arduino, MotorShild and two motors are well. But it is something wrong in my program. I have tested a lot of variants - it was in vain ![]()
I suppose that my mistake is connected with wrong handling of serial data. So, please, give me advice where to dig.
Many Thanks.