Evolution of a Blog

This blog has evolved as I have as a maker. It starts at the beginning of my journey where I began to re-tread my tires in the useful lore of micro electronics and the open-source software that can drive them. While building solutions around micro-electronics are still an occasional topic my more recent focus has been on the 3D Printing side of making.

Wednesday, May 8, 2013

Arduino Convert Long to Char Array and Back

I wanted to log data from an Arduino to an SD Card in the most space and time efficient manner possible.   To me this meant storing a four byte long in four bytes.  Bit shifting has always made my head hurt so I turned to Google.   I found the below (in a  couple places) but it did not seem to work:


unsigned long int longInt = 1234567890;
    unsigned char byteArray[4];
              
    // convert from an unsigned long int to a 4-byte array
    byteArray[0] = (int)((longInt >> 24) & 0xFF) ;
    byteArray[1] = (int)((longInt >> 16) & 0xFF) ;
    byteArray[2] = (int)((longInt >> 8) & 0XFF);
    byteArray[3] = (int)((longInt & 0XFF));
 
    unsigned long int anotherLongInt;
 


    anotherLongInt = ( (byteArray[0] << 24) 
                   + (byteArray[1] << 16) 
                   + (byteArray[2] << 8) 
                   + (byteArray[3] ) );


At least until AWOL on the Arduino forum suggested the below:

anotherLongInt = ( ((unsigned long)byteArray[0] << 24) 
                   + ((unsigned long)byteArray[1] << 16) 
                   + ((unsigned long)byteArray[2] << 8) 
                   + ((unsigned long)byteArray[3] ) )


Eureka!  I can now store two longs in eight bytes with a couple of distinct advantages:
  1. Result is fixed size which means I can seek into my file looking for specific entries
  2. Size on disk would generally be less than storing a string representation (unless you are only doing tiny numbers) and particularly if you wanted to do one of fixed size.
  3. Speedy string operations is not what you bought the Arduino for in the first place!  Saving 1,000 long to char arrays took a bit more than a second while the string version (even without padding to fixed size) took more than 10 seconds.
 I have no idea why AWOL's suggestion worked but I am happy to move on.

Monday, May 6, 2013

Simple Arduino Serial Communications Example

I had done a little serial work with the Arduino and my 'Bot but wanted to try another example.   In this case I have a sketch that  has the Arduino being the  receiver of a one way ASCII data stream.   The data stream uses two sync chars followed by two characters that will make up a two char integer. 

A real implementation would probably use character 127 but I am using "%" for illustrative purposes.  A real implementation would also recognize that the second of two chars of data could be a 127 and therefore you could see two sync characters where one of them is really data.

%%GF%%HG%34%%4%%%HH
   
First payload       - GF
Second payload      - HG
Garbage collected   - %34
Third payload       - 4%
Fourth payload      - HH

%%GF %%HG %34 %%4% %%HH

Here is a link to the sample code.

Saturday, May 4, 2013

Simple Vertical Menu for Arduino LCD Shield

Decided that I needed a real simple menu using the buttons on the Arduino LCD Shield.   I only need a simple menu so am just using the up and down buttons with the select button for making a choice.

Here is the code.   The key working bits are shown by the below snippets.  First define the character arrays that will comprise your menu items.  They need to be no more than fifteen characters long.   Remember to terminate the list with an empty entry!

Note the use of the compile option PROGMEM which moves the menu items to program storage and saves short supply RAM.

// Enable storage of literals in program space rather than RAM
#include <avr/pgmspace.h>

 

 
   // The menu entries list needs to end with a blank entry and each item
    // can only be 15 characters long:
    //                      [               ]
    PROGMEM char* menu1[]={ "Menu 1, Item 0",
                            "Menu 1, Item 1",
                            "Menu 1, Item 2",
                            "Ascii Table   ",
                            ""};

Then invoke the menu as shown below:
    int selection = menuSelection(menu1);

If I find that I need more functionality I may add some to this routine later but at the moment all I need is a simple vertical menu.

Friday, May 3, 2013

Gertboard - What For?

With a collection of four RPi's already in hand (five if you count the fried one) it seemed only natural that I add a Gertboard.   In reading about it I am not sure why I would want a Gertboard as opposed to buying a less expensive Arduino and then integrating it with the RPi but I am interested enough to buy one to play with for a while.
So there it is.  I have downloaded my first sketch to the onboard Arduino and am not sure what to do next!   Getting it up and running was pretty easy thanks to Gordon Henderson and these posts re configuring the Gertboard for use of the Arduino.

The one thing that did cause some momentary angst was solved when I read some comments on Gordon's blog regarding a jumper on the Getboard.   Namely the one that bridges power from the RPi to the Gertboard!   The one in the Gertboard manual that is decribed as such: "There is one jumper that should be in place at all times on the board: the one connecting pins 1 and 2 in header J7".   Now, if I had only read the manual.

I have to ask though.   If it should be in place at all times...why is it a jumper?   Or why not have the board come with one already in place?

Thursday, May 2, 2013

Arduino and Memory

To date I have not done very complicated work on the Arduino itself as my interface library allows me to offload any heavy duty processing to the host RPi.   I am now working on something that would run solely on the Arduino and am being forced to come to terms with the limitations of a micro controller environment.
  • Program logic has to fit within the Arduino's flash memory which static (burned when you sketch is uploaded)
  • There is EEProm memory available for sketches to use to store things that are needed across sessions.   This is in very short supply but in a microcontroller environment how much should you need anyway?
  • Most importantly, however, is the random access memory, or RAM, needed for a program during it's execution.   This is in relatively short supply especially if you have any desire to use arrays.
Here is a table from the Arduino Playground that shows the constraints that must be observed:

ATMega168ATMega328PATmega1280ATmega2560
Flash
(1 Kbyte used
for bootloader)
16 KBytes32 KBytes128 KBytes256 KBytes
SRAM1024 bytes2048 bytes8 KBytes8 KBytes
EEPROM512 bytes1024 bytes4 KBytes4 KBytes

My development environment is an Uno (ATMega168) so it is pretty constrained.  In a test case a sketch with two integer arrays (a voltage reading and a matching time stamp) maxed out at 400 entries.  On my ATmega1280 that is driving the 'Bot the top limit was 1400 entries.   I have a Gert Board on order and it uses the one in between (ATMega328).

This was not a realistic case as any reasonably complicated program is going to use more RAM than my example.   There are ways of maximizing RAM.   One is simply to be cautious with everything and especially with buffers that can be reused and character strings that can be abbreviated.   Static information can also be stored in program memory using PROGMEM (here is an example).

One last comment on the Arduino and Memory.   How did I first realize that I was having problems with memory management???   Sketches either did not run at all or when they ran they were corrupted and doing REALLY weird things.  Drove me a little bit banana's.

Tuesday, April 30, 2013

Save State in Arduino EEProm Across Resets

I am back to playing with the Arduino and am remembering just how uncomfortable I was working in "C" so long ago.   'Def a brain teaser for me compared to Python or PHP.

So anyway, here is an implementation of a couple routines with one that saves a string in the onboard EEProm on the Arduino and then a second that reads it back.   In my next iteration I will add a CSV parser such that if you put two distinct variables into the EEProm, separated by commas, you will get an array of two variables back from the read.

Here is what the serial console displays when this sketch is run:
--Starting
--String to Save on EEProm
2.222,4.44
--String Retrieved from EEProm
2.222,4.44
--All Done

Here is a LINK to the sketch itself.  There is really not that much to this example but the time it took me to relearn a little about array handling in "C" made this more of a challenge than I expected (for my old brain).

Note that I wrote my own floating to string conversion function as I was not thrilled with the way that dtostrf works.   Even this trivial routine took my old brain more than a couple minutes to write...as we hear the clanking of gears shifting.  --Updated to reflect that even after all that I had an error in the code that I just corrected.   I am struggling with working at the pointer level!

Another update - Found this posting on the Arduino Forum with a nice technique for using the EEProm memory to store anything with one operation.

Monday, April 29, 2013

Raspberry Pi Data Collector - Part 1

I follow the stock market with some interest and in particular wonder about the relationship between option trading volume and price moves.   I want to be able to track option movements across time and, other than paying for a service, have not found an easy way to get this data online.   Google Finance used to have an API that would access option chains but they discontinued the API.  The Yahoo Finance API might be able to do it but I could not figure out how so I gave up and decided to write something myself.

Since I have a RPi that is running all the time anyway (TvHeadend) I decided to write some Python script to do a capture on a nightly basis from that platform.  More about this later.