Arduino to Shield Interconnect

This one is a quickie
In one of the mailinglists I am subcribed the members were discussing if different Arduino shields are compatible with various Arduinos. I had years ago found a solution to this and want to share it, as it is simpler than anything else I have read about and i think it’s a pretty elegant solution too. It also spares you the breadboard aproach, that I personally never liked much.

So when you have a matching pair of an Arduino clone and a Arduino shield then you can just simply plug them in like this:
Shown here with a Digispark and the fitting Charlieplex Digishield. That is pretty straightforward.

But what can you do when you come across some incompatible boards like this ? Shown is a cheap ethernet shield with a Arduino Nano v3.0

This is what I came up with : I am using these wires, called “Dupont Line” or more accurately “Dupont Line 1P-1P”. With these you can easily connect each pin 1 by 1, just as you need them. See this:

I had a pretty hard time to find out what they are called (due to me not being a native english speaker) and even posting pictures in forums did not help. It is pretty vexing to know that something exists and is being sold, but not knowing how to order it. Now I know what to search for, the usual “chinese webshops” seem to be full of them.

They all have seperate connectors on both ends so you can freely attach them to any empty pin header. They come as a fully connected flat ribbon cable. So instead of separating them completely, you can also just leave them in groups to better distingish the signal they carry, since the colorspace is somewhat limited with only 10 colors.

Sooner or later though you will come across this problem: One side, eihter the shield or the Arduino will have sockets instead of pinheaders. But of course there is a solution to that one too. I use some spare pinheaders for the job, like this:

 

 

 

 

I simply press the metal pins to the middle of the pin header, that way I can i.e. connect two cables. This also works with the sockets on the PCBs. But the connection is not very stable. The lenght of the pins is just enough for the cables to hold on. So they fall of pretty easily. A better solution is to completely remove the plastic part of the pin header and stick the metal part into the end of the cable. Later when you don’t need it anymore, you can just remove it by pulling out. Put the little pins in a bag or they will get lost too easily. I also tried just simple uninsulated wires but they fall off to easily so keep using the pinheader-pins.

This way I can connect all my controller boards to all of my add on shields no matter what type they are. See a selection below :
I think this way of connecting shields with boards is a little bit more office-desk friendly than using a breadboard, which always seemed more “workbenchy” to me…

Thanks for reading! Please leave a comment below if you liked this.

Constant to Field Name

Any given class may have some predefined Constant values. In java these are declared as public static final fields. One such example is Color. You can call something like Color.RED and use that to set your brush so that it is colored in red. But sometimes you will need the opposite direction. If you get the number 5 you may want to know which predefined color that is or you might be listening for KeyEvents and get the value 116 and you don’t want to guess which key your user just pressed. In my specific case I get a 6 as a result to BufferedImage.getType(). That does not tell me much. What I need is a human readable description of the image type.

One way is to compare this to all known fields of BufferedImage with a simple case  switch list (for my case only). This might even be faster but it is not a nice solution and everytime you do this you will need to create a specific solution for just that type of object. Through some internet research I came up with this more general solution :

public String constantToFieldName(Object obj, int constant) throws Exception {
    for (Field f : obj.getClass().getDeclaredFields()){
        int mod = f.getModifiers();
        if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod)) {
            if ( Integer.parseInt(f.get(null).toString()) == constant ){
                return f.getName();
            }
        }        
    }
    throw new Exception ("Could not find within given fields");
}

This solution uses a little bit of Java Reflection to get a list of all fields for the given object’s class and look in them for those that seem to be constant values. From these Values it is compared one-by-one. As I said this will not be a fast solution. Reflection alone will take it’s couple of microseconds. But for debuging my code without creating new lists everytime it is a pretty handy solution. For me this got a fixed part of my toolbox.

Of coursemy solution is also limited to integer fields only, but changing that to cover other types is easy. And also there is always the list of all known constant fields (for the java API at least) at :
http://docs.oracle.com/javase/7/docs/api/constant-values.html

Colored text in Batch script

I ‘ve just written a litte batch script that connects the adb to my tablet via wlan. But the script does not always end with success since wlan is always a little shaky. So I needed an easy way to see if it did connect or not. Coloring is the first thing that comes to mind. Green, all ok; Red, smt went wrong. Well guess what, Batch / Dos does not allow colored text by default…

After googling a litte bit around I decided to write this following litte tool. It is in c#. I guess this is also my first c# programm actually. Or is it ? I can’t remember if I ever worked c# before, well whatever.

It takes an arbitrary amount of arguments, but at least 3. First is the foreground color, second is the background color, and from the third on it is the colored text. So if you want “something” in red, you just call:

col Red Black something

You want to emphasise it even more ? Just call:

col Black Red something some thing

I needed to name this just “col” instead of “color” since that is a DOS variable that sets the color of the whole console, maybe that would have also sufficed to show success or failure, but this way it was more time consuming 🙂 I just love to procrastrinate, don’t I ?

Well here is the code :

using System;

namespace Colors
{
    class MyColor
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                System.Console.WriteLine("\n    Colors by Burak\n");
                System.Console.WriteLine("Usage:");
                System.Console.WriteLine("color [ForegroundColor] [BackgroundColor] [text]");
                System.Console.WriteLine("\n All parameters needed ! \n\n Choose from: Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White \n\n");
                Environment.Exit(0);
            }

            Type type = typeof(ConsoleColor);

            System.Console.ForegroundColor = (ConsoleColor)Enum.Parse(type, args[0]);
            System.Console.BackgroundColor = (ConsoleColor)Enum.Parse(type, args[1]);

            for (int i = 2; i< args.Length-1 ; i++)
            {
                Console.Write(args[i]+" ");
            }
            Console.Write(args[args.Length-1]);
            Console.ResetColor();

        }
    }
}

Well, so far so good.. The reason I am actually writing this is that I learned that for simple C# code snipplets like this one, you do not need a big complier or an SDK or fancy tools. Just the Command Prompt is enough!

Within the .NET Framework is a nice little compiler called csc. I guess it stands for “C Sharp Compiler”. So if you put the above code in a text file and open a Command Prompt in that folder (I’ll go back to this later) you can rename the text file to a more accurate name with something like this:

ren Untitled.txt col.cs

Since this is a C-sharp file .cs is better. And to go ahead and compile it you can call:

c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe col.cs

So, what is this ?
In every .NET Framework you install, also a couple of tools are included. One of those is csc.exe. Of course the .exe is optional while calling it and instead of the v4.0.30319 you can use any other version you have installed. If there are Commands in your source that are not supported, it will throw an error, in that case use a higher version number. You can use Autocompletion to <tab> through all the versions availabe to you, type everything up to the version number type a single v and then use <tab>.

This should output a col.exe that works in any .NET preinstalled computer which has at least one compatible version.

Back to the “open a Command Prompt in that folder” :
I have very often had the need to to something in the Prompt, while or after shifting around files with a Windows Explorer window. All the time I did think the only way to get quickly into that folder is to type in the name with a cd command. I just recently discovered that this is acutally not true, you can simply do a right click and open a Command Prompt in exactly the folder you are in at that moment. You just need to hold the <Shift> key ! Knowing this will save me some time in the future, so I wanted to keep it written somewhere.