Motor control shield for Arduino Duemilanove

Sorry to start another Topic on this, but it get's really annoing. I simply cannot control the motor's direction.

I am using something like this : I will post it in the next message due to Forum rules.

I am inputting into the Serial Monitor the desired values but simply cannot make the motors (two motors) to spin in both directions (separately or both at the same time).

This is intended to be the control of a RC tank.

The source code is:

int dirA = 12; int dirB = 13; int speedA = 10; int speedB = 11; byte ad,bd,as,bs;

void setup() { //call this once at the beginning pinMode (dirA, OUTPUT); pinMode (dirB, OUTPUT); pinMode (speedA, OUTPUT); pinMode (speedB, OUTPUT); Serial.begin(115200); }

void loop() { //main loop if (Serial.available() > 2) { //if there is anything on the serial port, read it ad = Serial.read(); //val direction A bd = Serial.read(); //val direction B as = Serial.read(); //speed A bs = Serial.read(); //speed B Serial.println(ad,DEC); Serial.println(bd,DEC); Serial.println(as,DEC); Serial.println(bs,DEC); if(ad = 48) digitalWrite(dirA,LOW); if(ad = 49) digitalWrite(dirA,HIGH); if(bd = 48) digitalWrite(dirB,LOW); if(bd = 49) digitalWrite(dirB,HIGH); if(as >50) analogWrite(speedA,as); else digitalWrite(speedA,LOW); if (bs > 50) analogWrite(speedB,bs); else digitalWrite(speedB,LOW); ad=0; bd=0; as=0; bs=0; }

}

Can someone tell me if there is something wrong with the code? Or simply i have connected the wrong pins to the motors. I connected them to the 4 pins like in the picture.

Thank you in advantage!

The thing that I have looks like this on: http://blushingboy.net/p/motorShieldV3/page/Example-DC-motor/

if(ad = 48) digitalWrite(dirA,LOW);

Don't you want to be using == (the equality operator) instead of = (the assignment operator)?

As well as what PaulS said, I'd also be inclined to write it as "ad == '0'", etc.

Also, you're checking to see if you've got at least three characters available, then reading four...

You test to see if 3 or more characters are waiting and then go on to read 4 - that’s likely to give you problems.

 if (Serial.available() > 2) { //if there is anything on the serial port, read it
       ad = Serial.read(); //val direction A
       bd = Serial.read(); //val direction B
       as = Serial.read(); //speed A
       bs = Serial.read(); //speed

One final thing to note. If you are entering speed as a multi-digit number (like 13 or 243), you are only reading one digit. You are then using that character as the speed.

Is this really what you expected?

Yes, Paul you are correct.

An example of input data will be like this:

11cc -- it takes takes ASCII code of the input. After printing them the data that was read is correct. This will start the 2 motors in the same direction.

I have chosen this due to the fact that i will collect data from an IPhone through OSC.

Regarding my error, it is a stupid mistake i simply don't know how it got past me. Regardless of this the motors worked, and this is strange because they shouldn't.

Thank you very much for the help!

After printing them the data that was read is correct

If you were using the serial monitor to test, then it probably would always work assuming you always typed four charaters, because the characters are all sent at once, only when the return key is hit.

However, if you'd used something like hyperterm, it may well not have worked reliably, because then the characters are sent as they are typed, so your sketch would have received three characters, and then read them out, and the fourth would have been 0xFF.

I used serial monitor from the Arduino software. Anyway i hope when i will get home my vehicle will move :D

One more question:

The combination of pins that are used for direction and speed in this code is it specific or i can use any other combination of pins?

The current combination is 10-12(A) 11-13(B).

The combination of pins that are used for direction and speed in this code is it specific or i can use any other combination of pins?

Depends. If you are using a motor driver that is not a shield, then the pins can be changed. If the motor driver is a shield, the pins will be hard-wired.

Thank you for the help!

I really appreciate the helping hand. My tank is moving accordingly now.

My tank is moving accordingly now.

Time for a you tube video.

what did ur code end up with? i cant make it out from all the comments?

int dirA = 12; int dirB = 13; int speedA = 10; int speedB = 11; byte ad,bd,as,bs;

void setup() { //call this once at the beginning pinMode (dirA, OUTPUT); pinMode (dirB, OUTPUT); pinMode (speedA, OUTPUT); pinMode (speedB, OUTPUT); Serial.begin(115200); }

void loop() { //main loop if (Serial.available() > 2) { //if there is anything on the serial port, read it ad = Serial.read(); //val direction A bd = Serial.read(); //val direction B as = Serial.read(); //speed A bs = Serial.read(); //speed B

Serial.println(ad,DEC); //prints out the variables where i saved the direction and speed of each motor Serial.println(bd,DEC); Serial.println(as,DEC); Serial.println(bs,DEC);

if(ad == 48) digitalWrite(dirA,LOW); // 48 and 49 is the ASCII code for 0 and 1 if(ad == 49) digitalWrite(dirA,HIGH); // if is 1 it goes forward if 0 backward if(bd == 48) digitalWrite(dirB,LOW); if(bd == 49) digitalWrite(dirB,HIGH);

if(as >50) analogWrite(speedA,as); // if the speed value if above 50 the tank will move else it will stand still else digitalWrite(speedA,LOW); if (bs > 50) analogWrite(speedB,bs); else digitalWrite(speedB,LOW);

ad=0; // clearing all the data saved in those variables for a fresh read bd=0; as=0; bs=0; }

}

Notice that i have chosen to save the incoming data into auxiliary variables. I have chosen this because my raw data will come from an Iphone via an ASUS wl500. the raw data looks like this: 1 0 123 0 ---- this a right turn (or left) 1 1 255 255 this is full speed ahead.. and so on.

By entering manually the data with the serial monitor it will take the ASCII code of what i'm typing. For example: 11zz i will mean full speed ahead with the speed of the ascii code of 'z' (1 1 122 122).

You're still reading too much data - best fix it now before it bites you on the ass. You're also reading single characters, so that '255' stuff won't work.

The range of values for analogWrite are from 0 to 255. While z is the largest ascii value you can pass as a single character, you really should look at a more robust method of passing the data, as strings, so you have finer control over the speed. Right now, you can't actually make the robot stop, for instance.

You could be passing it a string like -200,250 where the sign of the value indicated direction, and the value could make the motors run full speed forward, or reverse, or stop (0,0).

It's really not that much more difficult to do.

You’re still reading too much data - best fix it now before it bites you on the ass.
You’re also reading single characters, so that ‘255’ stuff won’t work.

What do you mean by this?

The thing is that i cannot send through serial monitor the max speed 255 because of the limitations of the keyboard :slight_smile:

BUT: the raw data that will come from the iphone via an ASUS WL500g will have this format : A B X Y. This format is transmitted serial to the board through a usb cable by a script. (i’m running openWRT on the router).

A: 0 or 1
B: 0 or 1
X: 0-255
Y: 0-255

P.S.: The thing with the ASCII code is simply for testing purposes only.

I mean, you check for at least three characters, then read four. That's what will bite you.

I mean, you check for at least three characters, then read four. That's what will bite you.

I understand now. The thing is that i will send 4 values at any moment so that won't be any problem. Thanks for the tip though. I will modify it just to be on the safe side.

how did u fix this issue,
Sorry for the noob question, i am new to arduino’s and programming!