Close

auto dice

A project log for weekend novelty projects

minimal advanced planning. built from stuff i already have around the shop. typically not very useful. generally completed in one weekend.

zakqwyzakqwy 07/30/2016 at 22:020 Comments

Time for more tedious FR4 carving and soldering. This project is my take on a classic kit (see: here, here, here, here, etc). Like many of the other options available, my build uses the uncertainty of button press time to generate a random roll; as long as the pushbutton is held down, the microcontroller rapidly runs through all available options before stopping when the button is released. I think the two-dice-in-one setup is ideal--great for Catan, Monopoly, etc.

BOM:

The circuit is pretty simple; the LEDs are arranged in a 7x2 matrix, which seemed reasonable for dice and made the code simple. Even better, the seven anodes run into PORTA while the two cathodes are wired to PORTB. I included a hardware on-off switch to avoid battery drain issues, although I suppose good use of the watchdog timer could render this unnecessary. I dunno, I like hardware switches. The mess of wiring and stuff on the front is secured with a liberal coat of spray polyurethane.

If you _really_ want to know how I put this together.. I actually filmed the whole thing, including an unnecessary amount of running commentary. The build took nearly 4 hours but a fair bit of that is explaining parts, tools, technique, etc. Also, the audio for the first 12 minutes or so stinks, as I forgot to open the back of my GoPro. You've been warned...

Firmware:

/*
auto dice by Zach Fredin, July 2016 
zachary.fredin@gmail.com

Released under the terms of the MIT License.

The MIT License (MIT)
Copyright (c) 2016 by Zach Fredin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.	

Uses ATtiny44A microcontroller, because that's what I had on hand.
Two dice arranged in a 7 (anode) x 2 (cathode) matrix. Uses 580 nm 0402 LEDs with 150 ohm resistors on each.
Includes a 2-CR2032 + 5VDC LDO power supply with a hardware switch. 
Seven anodes on PORTA [0,1,2,3,4,5,6]. Two cathodes on PORTB [0,1]. Switch (pulled down) on PORTB [2]. 

Dice diagram:

PA2       PA0

PA4  PA3  PA1

PA6       PA5

*/

#include <avr/io.h>

void updateDice(uint8_t die, uint8_t val) {
	PORTA = 0;
	if (die == 0) { //left
		PORTB |= (1<<PB1);
		PORTB &= ~(1<<PB0);
			
		die = 1;
	}
	else { //right
		PORTB |= (1<<PB0);
		PORTB &= ~(1<<PB1);
		die = 0;
	}
	switch (val) {
		case 1:
			PORTA |= (1<<PA3);
			break;
		case 2:
			PORTA |= ((1<<PA6) | (1<<PA0));
			break;
		case 3:
			PORTA |= ((1<<PA6) | (1<<PA3) | (1<<PA0));
			break;
		case 4:
			PORTA |= ((1<<PA2) | (1<<PA0) | (1<<PA6) | (1<<PA5));
			break;
		case 5:
			PORTA |= ((1<<PA2) | (1<<PA0) | (1<<PA3) | (1<<PA6) | (1<<PA5));
			break;
		case 6:	
			PORTA |= ((1<<PA2) | (1<<PA0) | (1<<PA4) | (1<<PA1) | (1<<PA6) | (1<<PA5));
			break;
	}
}

void SystemInit(void) {
	DDRA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3) | (1<<PA4) | (1<<PA5) | (1<<PA6));
	PORTA = 0;
	DDRB |= ((1<<PB0) | (1<<PB1));
	DDRB &= ~(1<<PB2); //button
}

int main(void) {
	uint8_t die = 0;
	uint8_t d0 = 5;
	uint8_t d1 = 5;
	SystemInit();
	for(;;) {
		if (PINB & (1<<PB2)) {
			PORTB &= ~((1<<PB0) | (1<<PB1));
			PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3) | (1<<PA4) | (1<<PA5) | (1<<PA6));
			d0++;
			if (d0 == 7) {
				d0 = 1;
				d1++;
				if (d1 == 7) {
					d1 = 1;
				}
			}
		}
		if (die == 0) {
			updateDice(die, d0);
			die = 1;
		}
		else {
			updateDice(die, d1);
			die = 0;
		}
	}
}

Discussions