This is a continuation from part 3. if you haven’t read it yet, you can here.
In order to prevent the Jeep from popping a wheelie, I had to find a way to slowly speed it up to speed desired, instead of just jumping immediately to that speed. So, I quickly changed a few lines of code. AllĀ I did was make a new variable that stored the power of the jeep that would increase or decrease to the speed desired. Then I would check if the power was within 10 points of the desired speed, and stop increasing or decreasing it once it was. The reason I checked if it was within at least 10 points is because I was increasing it by 10 at a time, because increasing by one at a time took too long to speed up.
I tested the code, and everything worked fine, until I went to full speed. The Jeep would speed up faster and faster, until it reached full speed forward, and then it would go full speed reverse. If I went to full speed reverse, it would go faster and faster reverse, until it reached full speed, and jumped to full forward.
Why was this happening? I had absolutely no idea. I couldn’t figure out why this was happening. There appeared to be no reason. After an hour on messing around and finding no answers, I finally found out what was going wrong.
I’ve explained before that the largest and smallest numbers that the controller would output are 32767 and -32767, which also happen to be the 16-bit signed integer limit. What does that mean? Before I can explain that, you first need to know how computers do math.
Computers store data as electrical signals, where a signal can be either on, or off. Those are the only two pieces of data a computer can use, a one (on) or a zero (off). This is base 2. Most of the world counts in base 10. You count from zero to nine, and then loop around back to zero and carry the one: 0,1,2,3,4,5,6,7,8,9,10. But because computers can only count from zero to one, they are forced to use base 2.
Base 2 is exactly like base 10, but instead of counting up to 9 and then looping back, you count to 1 and then loop back. This is how you count to 10 in base 2 with the regular (base 10) numbers beside them. 0 (0), 1 (1), 10 (2), 11 (3), 100 (4), 101 (5), 110 (6), 111 (7), 1000 (8), 1001 (9), 1010 (10). This is how computers count numbers and it’s called binary, which means the same thing as base 2.
Computers store data in bits and bytes; a bit is a single digit (like 1 or 0) and a byte is 8 bits (like 00000000 or 10101010 or 00000011). The largest number a single byte can hold is 11111111 or 255. That means that there are 256 values you can store in a byte (when you include 00000000). But what happens if you try to count higher? If you only give the computer one byte to hold a number, but try to count higher than 255, (like if you try to calculate 255 + 1) then the number you will get back is 0. Why is this? Let’s work through it like the computer would:
Input: 11111111 + 00000001
The first thing the computer will do is add together the last 2 bits together. To the computer, 1 + 1 = 10 (In binary). So the computer makes the last digit 0, and carries the 1 up to the next bit; But that’s a 1 too, and 10 + 10 = 100 (remember, in binary). This continues all the way up to the 8th bit, since every single bit is a 1, until it adds the last bit, and then this happens:
- 1111110
- 11111100
- 11111000
- 11110000
- 11100000
- 11000000
- 10000000
- 00000000
The computer carries the 1 all the way up, and if there were 9 bits, the result would be 100000000. But there are only 8, and so the 1 is carried too far and lost because there is literally not anywhere left to put it.
You can also think of this as a number dial. Each time you increment the lowest dial, in increases by 1 until it reaches 9 and turns back to 0. The largest number you can show with 2 number dials is 99, and then if you turn it once more, they both go back to 0.
In order for computers to be able to store larger numbers, we just need to give them more space. In 2 bytes (that’s 16 bits), we can store numbers up to 1111111111111111 (65,535). The more bytes you add, the larger the numbers you can store.
The Arduino that I was using had 16-bit integer limit. All that means is that it could hold 16 bits (2 bytes) of data. The problem with the Arduino, and with all computers, is that they can only add. That’s right. Computers can’t have negative numbers, because what would that mean? You can’t have less than no electricity. But, obviously, computers must be able to subtract somehow, because if you’ve ever used a calculator, you’ll know that the subtraction function works just fine. So how can computers subtract, if they can only add. The answer is to subtract by adding. I know, that answer doesn’t make any sense, but I’ll explain.
Continue reading in Part 5 here.
0 thoughts on “Remote Control Jeep Community Service Project – Part 4”