📜 ⬆️ ⬇️

Dice at Attiny2313

Recently, my friends and I sat down tightly on board games, and shortly before that I decided to try programming under microcontrollers. Alternating the board games with the dance around the microcontroller (I was playing all this time with Attiny2313), the idea was born to put into practice the little knowledge that I gained in the initial stages of working with this MC and make a dice. Approximately the following task was set:


Then I began to formalize the task.

So, it was decided to lay out two dice of 7 points from SMD LEDs. In each cube there are 4 separate groups of LEDs (0 is a point in the middle, 1 is left upper right lower, 2 is upper right lower left, 3 is right in the middle and left in the middle). So everything is great, for control, we will need 8 legs or 1 byte, which means that one port is ideal for this.

Management decided to make it as simple as possible, at start only one of the cubes is active, when you click on the button, the cube shows a random number, with a long press, the mode switches.
')
No sooner said than done. I bought SMD components, by the way, this was my first experience with SMD and I don’t have a hair dryer, I’m not guided in dimensions either. When I took the parts I ordered, I fixed the size of the resistors in a millimeter per half millimeter. The band mate reassured me and said that he also really soldered such solders with an ordinary soldering iron, so I was a little cheered up that I would not have to wait a week until they collected a new order.

I ordered resistance, Attiny2313 in the case for SMD and LEDs; I already had a foil textolite and a clock button.

I threw a scheme in the eagle and made a fee:



Then I started programming. Since the groups of LEDs 8, we use pins PB0-PB7. Create an array with the values ​​for the first cube:

int digs[6]={ 0x01,//1 0x02,//2 0x03,//3 0x06,//4 0x07,//5 0x0E,//6 }; 

For the second, we will not need a separate array, it will suffice to use only the offset to 4 bits.

Pressing the key is processed in the interrupt, and the long or short press check logic is implemented on the counter, which is incremented by a timer. It also helped to avoid dancing around the bounce of contacts. Just when an interrupt occurs on a button, we translate the variable from false to true and start reading until the value is true. If the key is released before a certain interval, then this is a short press. We launch the die roll and reset all values, otherwise we consider that it is necessary to switch the mode.

 SIGNAL(INT0_vect) { sleepTimer=SLEEP_TIMEOUT; cli(); toggleButton=TRUE; sei(); } SIGNAL(TIMER0_COMPA_vect) { sleepTimer--; if(toggleButton==TRUE){ timer++; } if(timer>40&&timer<250){ if((PIND & (1 << PD2)) !=0){ toggleButton=FALSE; timer=0; shuffleDice(); } }else if(timer>250){ if((PIND & (1 << PD2)) == 0){ setMode(); } toggleButton=FALSE; timer=0; } } 

The result of the work can be seen in the video below:



The code is available by reference (I see no reason to upload it on github).

The plans want to make a normal case with a battery compartment for three tablets and add auto-off.

I will be glad to hear any criticism, as I want to know the mistakes that I made in this project.

Source: https://habr.com/ru/post/253037/


All Articles