While Xan is having fun merging the code bases for T-Hex, I thought I would play a little more with the Arduino code. Up till now I have only tried to make the code work with an Arduino Mega as it has lots of IO pins and lots of Uarts and the like. But I wondered, could this work with a normal Arduino? I quickly decided to only try to make it work with the newer Arduinos that are Atmega328 based Arduinos. The 328 gives me 32K code space and 2K data. The 32K for program space appeared to be enough so I started and I ordered an Arduino UNO.
On the Mega I relied on the multiple UARTS (1 for USB, 2nd for SSC32, 3rd XBee if I built for XBee). With the UNO I had 1 (USB). So I first looked at using SoftSerial library for SSC-32, but it’s documentation said good up to 9600 baud (ick). But I found a contributed Library NewSoftSerial, which appears to work at much higher speeds. In addition it use the hardware Pin Change interrupt to start inputs and it caches the characters received (
) So I went with that.
Very quickly I was able to get the code to compile and link, but when I tried to run it, it died very quickly and randomly. using avr-objdump to dump the executable, I found I was exceeding the 2K data. With the AVR GCC compiler it turns out that defining data tables as const static did not put the table into code space like it did for H8 compiler… So I converted to using the PROGMEM keyword and updated each of the usages for these tables to get it from the program space. I actually updated them twice as the first way made it look less readable. Example: change
cCoxaMin1[LegIndex] to pgm_read_word(cCoxaMin1 + LegIndex) [This is the way the documents for PROGMEM showed how to do this…]
Which to some could be confusing as the compiler knew that cCaxMin1 was an array of words so when it added LegIndex to it, it actually added 2*LegIndex… , but it turns out, you can also do it like: pgm_read_word(&cCoxaMin1[LegIndex])
Which I like better… This cut down the memory back to something that works!
The Mega has 8K of data, so I could be sloppy and when doing SSC or Debug output I would call a function with an 80 byte buffer which did sprintf and then output to the device. I wanted to get away from this, so I also included the Streaming utility library, which allowed you to not have to type as much:
That is if before in C you were to do something like:
printf(“XBee DL: %x\n\r”, wDL);
In Arduino you might do:
DBGSerial.write("XBee DL: ");
DBGSerial.println(wDL, HEX);
With Streaming you can do:
DBGSerial << "XBee DL: " << _HEX(wDL) << endl;
So today I connected up the Ardino UNO up to my CHR-3 with a PS2 and the SSC-32 at 115200 and it appears to work. I have not tried it yet with XBee and I have not tried this updated code back on MEGA yet. Lots more testing needs to be done, like running a sequence appears to run the sequence and either resets or turns hex off… Will debug…
But in case anyone wants to play along, I have uploaded the archived version (build 22 of Arduino).
Warning: This is a work in progress…
Arduino_Hex-110416a.zip (37 KB)