Skip navigation

Category Archives: Code

I’ve been making a fair few forms with the Laravel form-builder using model-binding. A few times I’ve had to make the user select a value for a one-to-many relation.

What’s the problem with model-binding and select-boxes?

In this example we’ll go with the status for a key (‘on loan’,’ at home’, or ‘broken’). The natural choice is a select-box, where the current state should already be chosen if we are editing the key.
The immediate solution is to just use  the ‘state’-property for the model-binding

 {{Form::select('keystatus', $keyStatus, null)}}

However, if you send a collection of models to a select box, you will end up with a weird box:

A botched select-box with json-representation of models - this won't work with model-binding.

A very unusable select-box

As you can see, this is the JSON-representation of the models – this won’t work.

The solution

This first obstacle is solved through a simple foreach-loop:

$statuses = Keystatus::all();
foreach($statuses AS $status){
$statusList[$status->id] = $status->status;

This is taken from my composer-file. As you can see, I get a nice array with the id for key and whatever text I want as the value.

Next step is to put it in the select-box and make the Laravel model-binding work it’s magic.

The tricky thing is, that the status on my key is an object, and doesn’t match whatever is in my array. Instead I put the status_id as the name of my select-box. This way the value in my status_id-column will match one of the keys in the array:

{{Form::select('status_id', $key, null)}}

Don’t forget to set the third parameter (default chosen value) to null. This allows the model-binding to work its magic. The last thing to do, is to adapt my controller to either save the id directly in the status_id-property, or load the status using Status::find($input[“status_id”]); and then associate that to the key:

//Either do this
$key->keystatus_id = $input["keystatus_id"];

//Or do this:
$state = KeysStatus::find($input["keystatus_id"]);

Personally I prefer the first method, as it save me a few calls to the database.

So, for a while, I’ve been wanting to getting into time-lapse photography. I have looked at intervalometers, but felt they were pricy. At first, I tried doing it manually, taking a picture every 30 seconds, but that wasn’t ideal. The solution was obvious – after all I’m a tinkerer and a geek, so what better solution than making an Arduino intervalometer myself?

I started out by dividing this project into two parts: Hardware and Software; I’d need some kind of way to get the Arduino to trigger the camera, and I needed a way to set up the interval, number of pictures etc, on the arduino. Like the project, my write-up of my making the Arduino intervalometer has been divied into two parts.

I started out getting the hardware part of my arduino intervalometer going – after all, if I couldn’t get the Arduino to trigger the camera-shutter, there wasn’t much point to coding the arduino.

Don’t wait to read about my arduino camera-trigger!

I have divided the project into 4 parts, part 1 is how to trigger the camera, it is fairly simple, but must be done correctly, to make sure you don’t fry your camera.

Part 2 is setting up the display for the intervalometer, to make sure, you can see what you’re doing when setting up the shoot.

Part 3 is wiring the buttons for the control

Part 4, the final part, is programming the arduino, making a menu the  most efficient way possible.

The parts will be put online as they are written.

Using an Arduino and 3 pins to control 5 LED’s

So, I’ve been sitting on two shift-registers I got for my Arduino for a few weeks, without getting around to actually doing anything with them. I finally decided enough was enough, and I cooked up a binary counter which counted to 31. All I used was a shift-register, 3 I/O-pins, and a handfull of 1kΩ resistors. Using the provided example in the Arduino-IDE I gobbled together my own Arduino sketch which first cycles through the LED’s and then counts to 31 over and over. Currently I only have the video, but I’ll add the source code and a diagram later on, when I get home to the computer with the sketch on it.

The shift-register I got for my Arduino

The shift-registers

What is a shift-register?

The advantage to using a shift register compared to not using a shift-register is, that you can “extend” your 10 I/O pins on the Arduino to a practically unlimited number. You will be using only 3 pins on the Arduino (apart from 5V and GND). The three pins are used for a latch-pins, a timer pin and a data-pin. The shift-register takes a serial signal, and sends it to the 8 out-pins on the shift-register, which then outputs them, once the latch-pin goes to ‘HIGH’. If you need to control more than 8 outputs, you connect another shift-register to the overflow pin on the shift-register, and the remaining bits are sent to the extra shift-register and so on.

For example, if I send binary 153 (10011001) to the shift-register, the pins 8, 5,4, and 1, will be set to ‘HIGH’ once the shift-register gets the all-ok signal (the latch-pin going to ‘HIGH’). In my example I only used 5 LED’s, so the highest number I could show, was 11111 (16+8+4+2+1 = 31).

What does my arduino project do?

My counter/gadget starts out by cycling the LEDs a la KITT (of Knight Rider fame). Once that is done, the counter starts counting in binary. Once the highest available number is reached, it resets the counter and starts over.

Basically the sketch is just a bunch of for-loops. At the top of the sketch I define the number of LEDs connected to the shift-register. Throughout the sketch, I refer to that number when setting the range of the loops. The Arduino sends the appropriate number, formatted as binary, to the shift-register. When the number has been received, the latch-pin is set to HIGH and the shift-register sets the pins as instructed.

Where to go from here?

The possibilities of using a shift-register with an Arduino is almost limitless. Just the fact that it frees up I/O pins makes it a great tool for your projects. The next step for me, would be to either a) get it to work with an LCD, or b) get my hands on a shift-out register. That way I can do the same thing, just for inputs. Currently I have an idea of making a intervalometer with a menu-system, which’ll require at least 4 input-pins.

Fork me on GitHub