Saturday, 24 January 2015

Simple hacks for Project 5 in the Arduino Uno Starter Kit

As you can see from this thread, there are some minor obstacles to overcome when wiring up project 5 in the arduino starter kit. I thought I'd give it a go and see if it's as annoying as this guy makes out.

This project uses the following components:

- 2 100uF decoupling capacitors
- 1 servo motor
- 1 potentiometer
- a few wires

Constructing the circuit: 


- hook up the potentiometer with 1 of the capacitors
- and the motor with the other capactior

Program it to read from the potentiometer and move the motor (which has a 180 degree range) accordingly.


Tacking the problems in the 'official' instructions


1) "The base of the potentiometer is too wide to fit as per the book diagram. It stretches 5 holes across, in the book the diagram shows it as 2. I cannot make the circuit in the book."

Yeah, ok, I'll give him this. It's a bit annoying to order a starter kit with a solderless breadboard then have components that are clearly meant to be soldered on.

My solution: 3 jumper wires (2 tiny ones and 1 slightly longer one) and some blue tac. (I didn't say this would be pretty)

I looped the jumper wires around each of the pins and secured it with blue tac. Jobs a good 'un.



2) The header short end is too short to fit into the servo female end.

Someone on the thread suggests taking out pliers and forcing the plastic off to make these male header pins the right size.

My solution: 3 short jumper wires

I just ignored the male header pins entirely and stuck three wires in there.



3) "The potentiometer 'base' just doesn't fit well into the board at all and whilst the circuit I've made works, it's all over the place unless I press down very very hard on the base. "

As mentioned above I didn't attach the potentiometer directly.

4) "I also discovered that the white leads in this mouser kit actually vary in length"

Unable to reproduce this problem. :-)

5) "In the book circuit the leads are from the top down black, red, white, but on the supplied servo the order is different, black, white, red. "

Is this really a problem? For a servo motor with arduino, attach the white wire to a PWM enabled digital pin (the example in the instructions it says attach to ~9 but it doesn't matter which), the black wire to ground and the red to 5V.

My additional problem #6

I didn't have a screwdriver tiny enough screw the arm into the motor, but I found that a pair of tweezers did the job.

My final circuit



Simple Arduino PWM Example with Cathode RGB LED

Cathode RGB LED



These LEDs have 4 prongs. The longest prong is the cathode, which you should hook up to ground.

The other 3 prongs are attached to separate elements in the head of the diode and these elements produce red green and blue light separately.

Our aim: To manipulate these RGB elements independently to produce different colours.

Digitally setting RGB elements on and off


Create this circuit:



Features of the circuit:
- the red prong of the LED is connected to digital pin 11.
- the blue prong of the LED is connected to digital pin 10.
- the blue prong of the LED is connected to digital pin 9.
- appropriate resistors are in place to ensure the LED doesn't get too many Volts which would cause it to blow. (Choosing the resistor is not covered in this post.)

DigitalWrite()


We can switch on/off the red, green and blue components independently using digitalWrite().



Mixing RGB


My arduino outputs 5V. However, I want to be able to reduce the voltage flowing through my LED to get colours other than the set above. For example to produce yellow, I want a bit of red and lots of green.

This can be achieved using Pulse Width Moderation.

Voltage going through the circuit can be switched on and off extremely quickly to effectively give voltage between zero and 5V.

- the more time it spends in HIGH the greater the effective voltage
- the more time in LOW the lower the effective voltage.

Because we are connected to the three separate pins (9, 10, 11) we can apply this technique independently across R, G and B.

To code this we use analogWrite().

AnalogWrite()

You will notice that the three pins (9, 10, 11) to which we are connected are marked with a ~. This means that it is possible to do PWM when connected to them.

The following code fades the LED from RED - BLUE - RED - ... .




Monday, 12 January 2015

brew cask

Most developers working on Mac will use homebrew to install packages. From the homebrew documentation on github:

"Our policy is that formulae in the core repository (Homebrew/homebrew) must be built from source. Binary-only formulae should go to Homebrew/homebrew-binary."

I imagine this is because it's safer to compile from source because anyone could put anything in a executable. However there isn't a great deal here in this homebrew-binary directory. In practice, for binaries on mac people generally use cask.

To install cask:

        brew install caskroom/cask/brew-cask

Install binary applications with cask:

        brew cask install visualvm
        brew cask install google-chrome

Cask is implemented as an external command in homebrew. 

Get syntax highlighting and formatting for Scala in Vim


There are a few copies of some .vim files floating around the internet which provide syntax highlighting and nice indentation for .scala and .sbt files. I have put them onto my github and here is a one liner to download the scripts and store them in the the vim configuration directory so they are picked up when you use vim: 

mkdir -p ~/.vim/{ftdetect,indent,syntax} && for d in ftdetect indent syntax ; do curl -o ~/.vim/$d/scala.vim https://raw.githubusercontent.com/apojha/scala-vim-support/master/$d/scala.vim ; done

Note that you must have 'syntax on' in your .vimrc file (or just ':syntax on' when your scala file is open in vim but that might get tedious if you are editing a lot of files in which case go use something sensible like IntelliJ. . :-) )

Monday, 5 January 2015

Zero to running memcached in 10 minutes (if you have a good download speed)

In two weeks I am moving to a Scala job. Someone suggested that a nice way to get started with a new language is to implement a 'mini memcached'. Open a socket, send some key/value pairs, store in a hash map: the idea is it's good to do a bit of network programming and get exposed to some libraries (collections, concurrency etc). I haven't quite figured out the spec for my mini memcached but to start, I though I'd try sending some stuff to a real instance of memcached and then swap it out for my mini version.

Installing Memcached on Ubuntu


sudo apt-get install memcached

After doing this the memcached service was running.

When I did this, memcached was not running on all network interfaces by default. Therefore I changed the memcached config file:

sudo sed -i 's/127.0.0.1/0.0.0.0/g' /etc/memcached.conf

Then I restarted the service:

sudo service memcached restart

Telnet to memcached to store a key/value pair


alexandra$ telnet 192.168.2.2 11211
Trying 192.168.2.2...
Connected to 192.168.2.2.
Escape character is '^]'.
set keyname 0 60 2
hi
STORED
get keyname
VALUE keyname 0 2
hi

END

If anything weird is happening, check the log:

 tail /var/log/memcached.log 

Vagrant VM running memcached


https://github.com/apojha/vagrant-vms/tree/master/memcached

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning...