Motor Shield Question

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
paintlax
 
Posts: 21
Joined: Sat May 03, 2014 11:00 pm

Motor Shield Question

Post by paintlax »

I am using a DC motor with the motor shield and had a quick question. What would happen if you set setSpeed() to a negative number? Would this cause the motor to run in the opposite direction of the run(FORWARD) run(BACKWARD) or will it just act as if setSpeed(0).

Thank you.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Motor Shield Question

Post by adafruit_support_mike »

The inputs aren't sign-sensitive.

The most likely answer is that the compiler would complain before ever letting you load the code into the Arduino. The speed parameters are declared as unsigned integers, so trying to pass a signed integer would probably generate a warning, and might kill the whole compile process.

If you did manage to get the value through, you'd see strange motor behavior. Small negative numbers would run the motor almost at full blast, while larger negatives would drop it to about half speed.

The key idea is that computers represent the signed integer -1 as 0xff (assuming you're using an 8-bit variable). The highest bit in the number is treated as the negative sign, so values from 0 to 127 (0x00 to 0x7f) are treated normally, but values from 128 to 255 (0x80 to 0xff) are treated as negative numbers. Since adding 1 to -1 produces 0, we represent -1 with 0xff (0xff + 1 = 0x00 plus an overflow bit). That way you can add positive and negative numbers together and get the right answer.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”