what is the problem in this motor control arduino code

hello friend,

I am working on a DC motor control application, I need to start motor clock wise on pressing a button which send '3' to arduino and if I again the same button then motor should rotate anti clock wise. But when i run the following code and press button only one time then motor rotates clock and anti clock wise infinity. please help.
arduino code is as follow

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); //RX, TX
int dataFromBT;
boolean toggle;
void setup()
{
mySerial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

}

void loop()
{
if (mySerial.available())
dataFromBT = mySerial.read();

if (dataFromBT == '3')
{
if(toggle)
{
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2,LOW);
}
if(!toggle)
{
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3,LOW);
}
toggle=!toggle;
}

}

Once your character is '3', it never stops being '3', so repeats every time through loop().

Please use code tags.

thanks for your fast reply, actually I am new to Arduino prog. and don't know expert programming, can you please help in writing a sample code. please.......

can you please help in writing a sample code

Sure. What should it do?

It isn't rocket science to set dataFromBT to 0 after you've used the value that you read.

It isn't rocket science to post your code properly.

It isn't rocket science to figure out that one period is enough.

Or you could put all your movement code inside the Serial. available condition with an extra pair of {} braces.

thanks, not tried yet. seeking some more solutions

Hi friends,
working on an interesting project of switching and operating DC motors through my android device using bluetooth.
In my android application there are two buttons which sends '3' and '4' respectively as follows

Button1 >>> sends '3' on every press to arduino
Button2 >>> sends '4' on every press to arduino

I have written and tested a code only for single button as follow to control a single DC motor to rotate clock wise and anti clock wise on pressing Button1 again and again i.e. on first press motor rotates clock wise and on second press motor should rotate anti clock wise.

here problem is on a single press of Button1 DC motor start rotating clock and anti clock wise infinitely.

any one please help, it would be a great help

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
int dataFromBT;
boolean toggle;
void setup()
{
mySerial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

}

void loop()
{
if (mySerial.available())
dataFromBT = mySerial.read();

if (dataFromBT == '3')
{
if(toggle)
{
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2,LOW);
}
if(!toggle)
{
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3,LOW);
}
toggle=!toggle;
}

}

seeking some more solutions

If you haven't tried the perfectly workable ones suggested, why bother with others?

ok.... sure I will try. my arduino board is fitted in a box hard to eject and takes time thats why I was seeking others also so that problem could be rectify in a single board ejection....... but if you are sure then I will definitely try it.
thanks AWOL

I said you can fix your current problem with a single pair of braces, but you had to repost the same question (again badly) in a new thread, which I then had to merge with this thread to save other people wasting their time answering a question that already had a solution, thereby wasting my time.

I'm out.

Sorry AWOL,
I think the problem was not mentioned in a right way thats why I just repost the message,
thanks for your kind support...

Hi AWOL,
it works, thanks